generated from mwc/project_game
I am actually really proud that i was able to figure out how to make the alien move in every direction randomly.
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
from random import randint
|
|
|
|
|
|
class Alien:
|
|
character = 'A'
|
|
|
|
def __init__(self, position):
|
|
self.position = position
|
|
|
|
def play_turn(self, game):
|
|
|
|
width, height = game.board_size
|
|
|
|
x, y = self.position
|
|
|
|
directions = [
|
|
(-1, 0), # left
|
|
(1, 0), # right
|
|
(0, -1), # up
|
|
(0, 1), # down
|
|
(-1, -1), # up-left
|
|
(-1, 1), # down-left
|
|
(1, -1), # up-right
|
|
(1, 1) # down-right
|
|
]
|
|
|
|
possible_positions = []
|
|
for dx, dy in directions:
|
|
new_x = x + dx
|
|
new_y = y + dy
|
|
|
|
if 0 <= new_x < width and 0 <= new_y < height:
|
|
possible_positions.append((new_x, new_y))
|
|
|
|
if not possible_positions:
|
|
return
|
|
random_index = randint(0, len(possible_positions) - 1)
|
|
new_position = possible_positions[random_index]
|
|
|
|
ship = game.get_agent_by_name('ship')
|
|
|
|
if ship is not None and new_position == ship.position:
|
|
game.end()
|
|
else:
|
|
|
|
self.position = new_position
|