Add walls.

This commit is contained in:
paulusja 2026-04-23 15:15:24 +02:00
parent 5090c09123
commit 102b89f449
3 changed files with 38 additions and 4 deletions

View File

@ -3,6 +3,8 @@ from window import Window
from input_manager import InputManager from input_manager import InputManager
from snake import Snake from snake import Snake
from game_object import GameObject from game_object import GameObject
from wall import Wall
def draw_all(window: Window, game_objs: list[GameObject]) -> None: def draw_all(window: Window, game_objs: list[GameObject]) -> None:
window.reset() window.reset()
@ -16,13 +18,20 @@ def update_all(game_objs: list[GameObject], user_input: int) -> None:
obj.update(user_input) obj.update(user_input)
def create_game_objects(window):
return [Snake(start_position=(400, 300),
length=5,
radius=10,
color=(255, 0, 255)),
Wall(window_size=window.get_size(),
width=5,
color=(255, 0, 0))]
if __name__ == '__main__': if __name__ == '__main__':
window = Window(title='Snake', size=(800, 600), background_color=(0, 128, 0)) window = Window(title='Snake', size=(800, 600), background_color=(0, 128, 0))
input_manager = InputManager() input_manager = InputManager()
game_objs = [Snake(start_position=(400, 300), game_objs = create_game_objects(window)
length=5,
radius=10,
color=(255, 0, 255))]
clock = pygame.time.Clock() clock = pygame.time.Clock()
framerate = 25 framerate = 25

21
snake/wall.py Normal file
View File

@ -0,0 +1,21 @@
import pygame
from game_object import GameObject
class Wall(GameObject):
def __init__(self, window_size, width, color):
top_wall = pygame.rect.Rect(0, 0, window_size[0], width)
left_wall = pygame.rect.Rect(0, 0, width, window_size[1])
bottom_wall = pygame.rect.Rect(0, window_size[1]-width, window_size[0], width)
right_wall = pygame.rect.Rect(window_size[0]-width, 0, width, window_size[1])
self.__walls = [top_wall, left_wall, bottom_wall, right_wall]
self.__color = color
def draw(self, surface):
for wall in self.__walls:
pygame.draw.rect(surface, self.__color, wall)

View File

@ -17,3 +17,7 @@ class Window:
def draw_object(self, game_object: GameObject) -> None: def draw_object(self, game_object: GameObject) -> None:
game_object.draw(self.__surface) game_object.draw(self.__surface)
def get_size(self):
return self.__surface.get_size()