-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (54 loc) · 2.31 KB
/
main.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
66
67
68
69
70
71
from SnakeGame import SnakeGame
from enums.PlaygroundTile import PlaygroundTile
import pygame
import sys
game = SnakeGame(10, 10)
game.startGame()
matrix = game.playground.getPlaygroundMatrix()
pygame.init()
display = pygame.display.set_mode((len(matrix) * 20,
len(matrix[0]) * 20))
clock = pygame.time.Clock()
dt = 0
MOVEEVENT = pygame.USEREVENT + 1
wieoft = int(1000 / 7)
pygame.time.set_timer(MOVEEVENT, wieoft)
def draw(matrix):
for row in range(len(matrix)):
for col in range(len(matrix[0])):
# width - height - endwidth - endheight
if matrix[row][col] == PlaygroundTile.VOID:
pygame.draw.rect(display, (0, 0, 0), (col * 20, row * 20, col * 20 + 20, row * 20 + 20))
elif matrix[row][col] == PlaygroundTile.SNAKE:
pygame.draw.rect(display, (248, 255, 4), (col * 20, row * 20, col * 20 + 20, row * 20 + 20))
elif matrix[row][col] == PlaygroundTile.FOOD:
pygame.draw.rect(display, (255, 4, 21), (col * 20, row * 20, col * 20 + 20, row * 20 + 20))
elif matrix[row][col] == PlaygroundTile.TEXT:
pygame.draw.rect(display, (255, 255, 255), (col * 20, row * 20, col * 20 + 20, row * 20 + 20))
elif matrix[row][col] == PlaygroundTile.WALL:
pygame.draw.rect(display, (255, 255, 255), (col * 20, row * 20, col * 20 + 20, row * 20 + 20))
while True:
matrix = game.playground.getPlaygroundMatrix()
display.fill((255, 255, 255))
draw(matrix)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == MOVEEVENT:
if game.isGameRunning():
m = game.player.move()
if m:
game.performGameOverCheck()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
game.player.setDirectionUp()
if event.key == pygame.K_s:
game.player.setDirectionDown()
if event.key == pygame.K_a:
game.player.setDirectionLeft()
if event.key == pygame.K_d:
game.player.setDirectionRight()
draw(matrix)
pygame.display.update()
# TODO: make functions of classes private if not needed elsewhere