-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor.py
147 lines (112 loc) · 3.81 KB
/
refactor.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from pygame.locals import *
import pygame
import time
from random import randint
# snake class
class Player:
# horizontal position
x = 0
# vertical position
y = 0
# direction
d = 0
# old positions
positions = []
# Size of snake
length = 4
# apple class
class Apple:
x = 0
y = 0
# game class
class Game:
game_width = 10
game_height = 10
grid_size = 44
# collision detection
def isCollision(self,x1,y1,x2,y2,bsize):
if x1 >= x2 and x1 <= x2 + bsize:
if y1 >= y2 and y1 <= y2 + bsize:
return True
return False
def __init__(self):
self._running = True
# create player obj
self.player = Player()
# create apple obj
self.apple = Apple()
# set position
self.apple.x = randint(0,self.game_width) * self.grid_size
self.apple.y = randint(0,self.game_height) * self.grid_size
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((640,480), pygame.HWSURFACE)
pygame.display.set_caption('Pygame example')
# load images
self._snake_image = pygame.image.load("snake.png").convert()
self._apple_image = pygame.image.load("apple.png").convert()
def on_render(self):
self._display_surf.fill((0,0,0))
#self._display_surf.blit(self._snake_image, (self.player.x,self.player.y))
# draw snake
for pos in self.player.positions:
self._display_surf.blit(self._snake_image, (pos[0],pos[1]))
# draw apple
self._display_surf.blit(self._apple_image, (self.apple.x,self.apple.y))
# update screen
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def snake_logic(self):
if self.player.d == 0:
self.player.x += 44
elif self.player.d == 1:
self.player.x -= 44
elif self.player.d == 2:
self.player.y -= 44
elif self.player.d == 3:
self.player.y += 44
if len(self.player.positions) < self.player.length:
self.player.positions.append((self.player.x,self.player.y))
else:
self.player.positions.pop(0)
self.player.positions.append((self.player.x,self.player.y))
def game_play_logic(self):
if self.isCollision(self.player.x,self.player.y,self.apple.x,self.apple.y, 44):
print('collides')
self.apple.x = randint(0,self.game_width) * self.grid_size
self.apple.y = randint(0,self.game_height) * self.grid_size
self.player.length += 1
if len(self.player.positions) > self.player.length-1:
for i in range(0,self.player.length-1):
if self.isCollision(self.player.x,self.player.y,self.player.positions[i][0], self.player.positions[i][1],40):
print('GAME OVER')
exit()
def game_logic(self):
self.snake_logic()
self.game_play_logic()
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
self.player.d = 0
if keys[K_LEFT]:
self.player.d = 1
if keys[K_UP]:
self.player.d = 2
if keys[K_DOWN]:
self.player.d = 3
def on_execute(self):
if self.on_init() == False:
self._running = False
while self._running:
self.handle_events()
self.game_logic()
self.on_render()
time.sleep(0.1)
self.on_cleanup()
if __name__ == "__main__" :
game = Game()
game.on_execute()