In this submit, i wokred on making the game more to how i want it.

I was able to get it to move and start from the center.
My next goal is to move around and pick up "food" for my snake.
This commit is contained in:
mbhatti4
2025-12-13 23:38:01 -05:00
parent 9142ec7bfe
commit e273f1a954
3 changed files with 36 additions and 20 deletions

Binary file not shown.

View File

@@ -1,7 +1,9 @@
from retro.game import Game from retro.game import Game
from player import Player from player import Player
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 = "pink", debug=TRUE) game = Game([player],{"score": 0}, board_size=board_size, color="pink", debug=True,)
game.play()
game.play()

View File

@@ -4,25 +4,39 @@ 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) start_x = board_width // 2
start_y = board_height // 2
self.position = (start_x, start_y)
self.body = [self.position]
self.direction = (1, 0)
def handle_keystroke(self, keystroke, game): def handle_keystroke(self, keystroke, game):
x, y = self.position x, y = self.body[0]
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"):
if keystroke.name == "KEY_LEFT":
new_position = (x - 1, y)
elif keystroke.name == "KEY_RIGHT": if keystroke.name == "KEY_LEFT":
new_position = (x +1, y) self.direction = (-1, 0)
elif keystroke.name == "KEY_RIGHT":
self.direction = (1, 0)
elif keystroke.name == "KEY_UP":
self.direction = (0, -1)
elif keystroke.name == "KEY_DOWN":
self.direction = (0, 1)
elif keystroke.name == "KEY_UP": dx, dy = self.direction
new_position = (x, y-1) new_head = (x + dx, y + dy)
elif keystroke.name == "KEY_DOWN":
new_position = (x, y+1)
if game.on_board(new_position): if not game.on_board(new_head):
if game.is_empty(new_position): game.end()
self.position = new_position return
else:
game.end() if new_head in self.body:
game.end()
return
self.body.insert(0, new_head)
self.body.pop()
self.position = self.body[0]