generated from mwc/project_game
Checkpoint 2: Attaching the "asteroid" to the player if the player touches the "asteroid" This part was challenging as there were new commands I learned while trying to incorporate the retro lab componemets. I included the "asteroid sprawner" to get more than on "asteroid" on the screen. The next part is to end the game.
This commit is contained in:
BIN
__pycache__/apple.cpython-313.pyc
Normal file
BIN
__pycache__/apple.cpython-313.pyc
Normal file
Binary file not shown.
BIN
__pycache__/apple_sprawner.cpython-313.pyc
Normal file
BIN
__pycache__/apple_sprawner.cpython-313.pyc
Normal file
Binary file not shown.
Binary file not shown.
46
apple.py
Normal file
46
apple.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
class Apple:
|
||||||
|
character = 'A'
|
||||||
|
|
||||||
|
def __init__(self, position):
|
||||||
|
self.position = position
|
||||||
|
self.attached = False
|
||||||
|
self.offset = (0,0)
|
||||||
|
|
||||||
|
def play_turn(self, game):
|
||||||
|
width, height = game.board_size
|
||||||
|
player = game.get_agent_by_name('player')
|
||||||
|
if self.attached:
|
||||||
|
px, py = player.position
|
||||||
|
ox, oy = self. offset
|
||||||
|
self.position = (px + ox, py + oy)
|
||||||
|
return
|
||||||
|
x, y = self.position
|
||||||
|
if game.turn_number % 2 == 0:
|
||||||
|
new_position= (x +1, y)
|
||||||
|
|
||||||
|
if new_position == player.position:
|
||||||
|
self.attach_to_player(player)
|
||||||
|
elif game.on_board(new_position):
|
||||||
|
self.position = new_position
|
||||||
|
else:
|
||||||
|
game.remove_agent(self)
|
||||||
|
|
||||||
|
def attach_to_player(self, player):
|
||||||
|
self.attached = True
|
||||||
|
index = len(player.attached_apples)
|
||||||
|
self.offset = (-1 - index, 0)
|
||||||
|
player.attached_apples.append(self)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# if y == height - 1:
|
||||||
|
# game.remove_agent(self)
|
||||||
|
# else:
|
||||||
|
# player = game.get_agent_by_name('player')
|
||||||
|
# new_position = (x+1, y)
|
||||||
|
# if new_position == player.position:
|
||||||
|
# game.end()
|
||||||
|
# else:
|
||||||
|
# self.position = new_position
|
||||||
19
apple_sprawner.py
Normal file
19
apple_sprawner.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from random import randint
|
||||||
|
from apple import Apple
|
||||||
|
|
||||||
|
class AppleSpawner:
|
||||||
|
display = False
|
||||||
|
|
||||||
|
def play_turn(self, game):
|
||||||
|
width, height = game.board_size
|
||||||
|
game.state['score'] += 1
|
||||||
|
if self.should_spawn_apple(game.turn_number):
|
||||||
|
x = randint(0,width - 1)
|
||||||
|
y = randint(0, height - 1)
|
||||||
|
|
||||||
|
if game.is_empty((x,y)):
|
||||||
|
apple = Apple((x,y))
|
||||||
|
game.add_agent(apple)
|
||||||
|
|
||||||
|
def should_spawn_apple(self, turn_number):
|
||||||
|
return randint(0, 1000) < turn_number
|
||||||
6
game.py
6
game.py
@@ -1,7 +1,11 @@
|
|||||||
from retro.game import Game
|
from retro.game import Game
|
||||||
from player import Player
|
from player import Player
|
||||||
|
from apple import Apple
|
||||||
|
from apple_sprawner import AppleSpawner
|
||||||
|
|
||||||
board_size = (100, 25)
|
board_size = (100, 25)
|
||||||
player = Player(board_size)
|
player = Player(board_size)
|
||||||
game = Game([player], {"score": 0}, board_size=board_size, color = "white")
|
apple = Apple((board_size[0] // 2, 0))
|
||||||
|
spawner = AppleSpawner()
|
||||||
|
game = Game([player,spawner], {"score": 0}, board_size=board_size, color = "white")
|
||||||
game.play()
|
game.play()
|
||||||
@@ -5,6 +5,7 @@ class Player:
|
|||||||
def __init__(self, board_size):
|
def __init__(self, board_size):
|
||||||
board_width, board_height = board_size
|
board_width, board_height = board_size
|
||||||
self.position = (board_width // 2, board_height - 1)
|
self.position = (board_width // 2, board_height - 1)
|
||||||
|
self.attached_apples = []
|
||||||
|
|
||||||
def handle_keystroke(self, keystroke, game):
|
def handle_keystroke(self, keystroke, game):
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
@@ -22,7 +23,5 @@ class Player:
|
|||||||
new_position = (x, y + 1)
|
new_position = (x, y + 1)
|
||||||
|
|
||||||
if game.on_board(new_position):
|
if game.on_board(new_position):
|
||||||
if game.is_empty(new_position):
|
|
||||||
self.position = new_position
|
self.position = new_position
|
||||||
else:
|
#self.check_self_collision(game)
|
||||||
game.end()
|
|
||||||
Reference in New Issue
Block a user