Move game loop into own class.
This commit is contained in:
parent
c4bbe09048
commit
6f6472fd44
40
snake/game_loop.py
Normal file
40
snake/game_loop.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import pygame
|
||||||
|
from game_object import GameObject
|
||||||
|
from window import Window
|
||||||
|
from input_manager import InputManager
|
||||||
|
|
||||||
|
|
||||||
|
class GameLoop:
|
||||||
|
def __init__(self, window: Window, game_objs: list[GameObject], input_manager: InputManager):
|
||||||
|
self.__window = window
|
||||||
|
self.__game_objs = game_objs
|
||||||
|
self.__input_manager = input_manager
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __draw_all(window: Window, game_objs: list[GameObject]) -> None:
|
||||||
|
window.reset()
|
||||||
|
|
||||||
|
for obj in game_objs:
|
||||||
|
window.draw_object(obj)
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __update_all(game_objs: list[GameObject], user_input: int) -> None:
|
||||||
|
for obj in game_objs:
|
||||||
|
obj.update(user_input, game_objs)
|
||||||
|
|
||||||
|
|
||||||
|
def run(self, framerate=15):
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
last_input = None
|
||||||
|
|
||||||
|
while last_input != InputManager.QUIT:
|
||||||
|
last_input = self.__input_manager.process_input()
|
||||||
|
|
||||||
|
GameLoop.__update_all(self.__game_objs, last_input)
|
||||||
|
GameLoop.__draw_all(self.__window, self.__game_objs)
|
||||||
|
|
||||||
|
clock.tick(framerate)
|
||||||
|
pygame.display.flip()
|
||||||
@ -1,4 +1,3 @@
|
|||||||
import pygame
|
|
||||||
from window import Window
|
from window import Window
|
||||||
from input_manager import InputManager
|
from input_manager import InputManager
|
||||||
from snake import Snake
|
from snake import Snake
|
||||||
@ -6,18 +5,7 @@ from game_object import GameObject
|
|||||||
from wall import Wall
|
from wall import Wall
|
||||||
from prey import Prey
|
from prey import Prey
|
||||||
from game_over_screen import GameOverScreen
|
from game_over_screen import GameOverScreen
|
||||||
|
from game_loop import GameLoop
|
||||||
|
|
||||||
def draw_all(window: Window, game_objs: list[GameObject]) -> None:
|
|
||||||
window.reset()
|
|
||||||
|
|
||||||
for obj in game_objs:
|
|
||||||
window.draw_object(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def update_all(game_objs: list[GameObject], user_input: int) -> None:
|
|
||||||
for obj in game_objs:
|
|
||||||
obj.update(user_input, game_objs)
|
|
||||||
|
|
||||||
|
|
||||||
def create_game_objects(window: Window):
|
def create_game_objects(window: Window):
|
||||||
@ -37,19 +25,10 @@ def create_game_objects(window: Window):
|
|||||||
|
|
||||||
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()
|
game_loop = GameLoop(window=window,
|
||||||
game_objs = create_game_objects(window)
|
game_objs=create_game_objects(window),
|
||||||
|
input_manager=InputManager())
|
||||||
|
|
||||||
|
game_loop.run(framerate=15)
|
||||||
|
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
|
||||||
framerate = 15
|
|
||||||
|
|
||||||
last_input = None
|
|
||||||
|
|
||||||
while last_input != InputManager.QUIT:
|
|
||||||
last_input = input_manager.process_input()
|
|
||||||
|
|
||||||
update_all(game_objs, last_input)
|
|
||||||
draw_all(window, game_objs)
|
|
||||||
|
|
||||||
clock.tick(framerate)
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user