Files
project_game/player.py
mbhatti4 e273f1a954 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.
2025-12-13 23:38:01 -05:00

43 lines
946 B
Python

class Player:
name = "player"
character = 'S'
def __init__(self, board_size):
board_width, board_height = board_size
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):
x, y = self.body[0]
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"):
if keystroke.name == "KEY_LEFT":
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)
dx, dy = self.direction
new_head = (x + dx, y + dy)
if not game.on_board(new_head):
game.end()
return
if new_head in self.body:
game.end()
return
self.body.insert(0, new_head)
self.body.pop()
self.position = self.body[0]