
[ad_1]
Game Development Stack Exchange is a query and reply website for skilled and impartial recreation builders. It solely takes a minute to enroll.
Anybody can ask a query
Anybody can reply
The finest solutions are voted up and rise to the highest
Asked
Viewed
2 occasions
in snake recreation tips on how to transfer by itself?
def snake_control(self):
# motion: up, down, left, proper
self.proper()
ml="okay"
mr="okay"
mu = 'okay'
md = 'okay'
#
# # keep away from hazard zone
if self.get_position()[0] - 10 < self.safe_zone_min_x:
ml="no"
if self.get_position()[0] + 10 > self.safe_zone_max_x - 10:
mr="no"
if self.get_position()[1] - 10 < self.safe_zone_min_y:
mu = 'no'
if self.get_position()[1] + 10 > self.safe_zone_max_y - 10:
md = 'no'
# # keep away from artifact
if self.get_position()[0] - 10 == self.get_artifact_position()[0] and self.get_position()[1] ==
self.get_artifact_position()[1]:
ml="no"
if self.get_position()[0] + 10 == self.get_artifact_position()[0] and self.get_position()[1] ==
self.get_artifact_position()[1]:
mr="no"
if self.get_position()[0] == self.get_artifact_position()[0] and self.get_position()[1] - 10 ==
self.get_artifact_position()[1]:
mu = 'no'
if self.get_position()[0] == self.get_artifact_position()[0] and self.get_position()[1] + 10 ==
self.get_artifact_position()[1]:
md = 'no'
# # keep away from visited path
for block in self.get_visited_path():
if self.get_position()[0] - 50 == block[0] and self.get_position()[1] == block[1]:
ml="no"
if self.get_position()[0] + 50 == block[0] and self.get_position()[1] == block[1]:
mr="no"
if self.get_position()[0] == block[0] and self.get_position()[1] - 50 == block[1]:
mu = 'no'
if self.get_position()[0] == block[0] and self.get_position()[1] + 50 == block[1]:
md = 'no'
# # keep away from self touching
for block in self.get_body()[1:]:
if self.get_position()[0] - 10 == block[0] and self.get_position()[1] == block[1]:
ml="no"
if self.get_position()[0] + 10 == block[0] and self.get_position()[1] == block[1]:
mr="no"
if self.get_position()[0] == block[0] and self.get_position()[1] - 10 == block[1]:
mu = 'no'
if self.get_position()[0] == block[0] and self.get_position()[1] + 10 == block[1]:
md = 'no'
# # keep away from enemy touching
for block in self.get_enemy_body():
if self.get_position()[0] - 10 == block[0] and self.get_position()[1] == block[1]:
ml="no"
if self.get_position()[0] + 10 == block[0] and self.get_position()[1] == block[1]:
mr="no"
if self.get_position()[0] == block[0] and self.get_position()[1] - 10 == block[1]:
mu = 'no'
if self.get_position()[0] == block[0] and self.get_position()[1] + 10 == block[1]:
md = 'no'
# # prepared to maneuver
ready_move = []
#
if ml == 'okay':
ready_move.append('ml')
if mr == 'okay':
ready_move.append('mr')
if mu == 'okay':
ready_move.append('mu')
if md == 'okay':
ready_move.append('md')
#
ready_move_len = len(ready_move)
rand_idx = 0
if ready_move_len > 1:
rand_idx = random.randint(0, ready_move_len - 1)
#
if ready_move_len > 0:
if ready_move[rand_idx] == 'ml' and ml == 'okay':
self.left()
self.add_visited_path(self.get_position())
if ready_move[rand_idx] == 'mr' and mr == 'okay':
self.proper()
self.add_visited_path(self.get_position())
if ready_move[rand_idx] == 'mu' and mu == 'okay':
self.up()
self.add_visited_path(self.get_position())
if ready_move[rand_idx] == 'md' and md == 'okay':
self.down()
self.add_visited_path(self.get_position())
jjx is a brand new contributor to this website. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
lang-py
[ad_2]