-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
65 lines (48 loc) · 2 KB
/
Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame
from IGame import IGame
class Game(IGame):
"""
This class will use the Game Loop Pattern and implement IGame Abstract Class (Interface)
"""
def __init__(self):
super().__init__()
def processInput(self):
self.snake.moveCommandX = 0
self.snake.moveCommandY = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
break
# TODO REFACTOR THIS CODE
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
break
elif event.key == pygame.K_UP:
self.snake.pivotBehaviourY(-10, "UP")
elif event.key == pygame.K_DOWN:
self.snake.pivotBehaviourY(10, "DOWN")
elif event.key == pygame.K_LEFT:
self.snake.pivotBehaviourX(-10, "LEFT")
elif event.key == pygame.K_RIGHT:
self.snake.pivotBehaviourX(10, "RIGHT")
def update(self):
# self.snake.applyMovementInertia()
# print(self.snake.snake_[0], self.food_position)
if self.snake.foodWasEaten(self.food_position):
self.food_position = self.food.generate_food(self.boundaries)
self.snake.grow()
if self.snake.isCollision(self.boundaries):
self.running = False
self.snake.applyMovementInertia()
def render(self):
self.window.fill((0, 0, 0))
pygame.draw.rect(self.window, self.PURPLE, (5, 5, 630, 5))
pygame.draw.rect(self.window, self.PURPLE, (5, 5, 5, 470))
pygame.draw.rect(self.window, self.PURPLE, (5, 470, 630, 5))
pygame.draw.rect(self.window, self.PURPLE, (630, 5, 5, 470))
self.snake.display_long_snake(self.window, self.PURPLE)
self.food.display(self.window, self.LIGHT_ORANGE)
pygame.display.update()
def run(self):
super().run()