-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (79 loc) · 2.89 KB
/
main.py
File metadata and controls
109 lines (79 loc) · 2.89 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from pygame.locals import *
from random import randint
from apple import Apple
from player import Player
from logic import Game
import pygame
import time
STEP = 44
class App:
windowWidth = 800
windowHeight = 600
snake = 0
apple = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self._apple_surf = None
self.game = Game()
self.snake = Player(3)
self.apple = Apple(5, 5)
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth, self.windowHeight), pygame.HWSURFACE)
pygame.display.set_caption('Rabbit_2k18: Snake')
self._running = True
self._image_surf = pygame.Surface((STEP,STEP))
self._apple_surf = pygame.Surface((STEP,STEP))
def on_event(self, event):
if event.type == QUIT:
self._running = False
def on_loop(self):
self.snake.update()
# does snake eat apple?
for i in range(0, self.snake.length):
if self.game.isCollision(self.apple.x, self.apple.y, self.snake.x[i], self.snake.y[i], STEP):
self.apple.x = randint(2, 9) * STEP
self.apple.y = randint(2, 9) * STEP
self.snake.length = self.snake.length + 1
self.snake.points = self.snake.points + 1
# does snake collide with itself?
for i in range(2, self.snake.length):
if self.game.isCollision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i], 40):
print("You lose! Collision: ")
print("x[0] (" + str(self.snake.x[0]) + "," + str(self.snake.y[0]) + ")")
print("x[" + str(i) + "] (" + str(self.snake.x[i]) + "," + str(self.snake.y[i]) + ")")
print("Points: " + str(self.snake.points))
exit(0)
pass
def on_render(self):
self._display_surf.fill((0, 0, 0))
self.snake.draw(self._display_surf, self._image_surf)
self.apple.draw(self._display_surf, self._apple_surf)
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self._running = False
while (self._running):
pygame.event.pump()
keys = pygame.key.get_pressed()
if (keys[K_RIGHT]):
self.snake.moveRight()
if (keys[K_LEFT]):
self.snake.moveLeft()
if (keys[K_UP]):
self.snake.moveUp()
if (keys[K_DOWN]):
self.snake.moveDown()
if (keys[K_ESCAPE]):
self._running = False
self.on_loop()
self.on_render()
time.sleep(50.0 / 1000.0)
self.on_cleanup()
if __name__ == "__main__":
theApp = App()
theApp.on_execute()