-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakegame.py
171 lines (132 loc) · 4.49 KB
/
snakegame.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pygame
from pygame.locals import *
import time
from random import randint
class apple:
x=0
y=0
step=44
def __init__(self,x,y):
self.x=x*self.step
self.y=y*self.step
def draw (self,surface,image):
surface.blit(image,(self.x,self.y))
class Player:
x=[0]
y=[0]
step=44
length=3
direction=0
updateCountMax=2
updateCount=0
def __init__(self,length):
self.length=length
for i in range(0,2000):
self.x.append(-100)
self.y.append(-100)
#initial position no collision
self.x[1]=1*44
self.x[2]=2*44
def update(self):
self.updateCount=self.updateCount+1
if self.updateCount > self.updateCountMax:
#update previous positions
for i in range(self.length-1,0,-1):
self.x[i]=self.x[i-1]
self.y[i]=self.y[i-1]
#update position of head of snake
if self.direction==0:
self.x[0]=self.x[0]+self.step
if self.direction==1:
self.x[0]-=self.step
if self.direction==2:
self.y[0]-=self.step
if self.direction==3:
self.y[0]+=self.step
self.updateCount=0
def moveRight(self):
self.direction=0
def moveLeft(self):
self.direction=1
def moveUp(self):
self.direction=2
def moveDown(self):
self.direction=3
def draw(self,surface,image):
for i in range(0,self.length):
surface.blit(image,(self.x[i],self.y[i]))
class game:
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
class app:
windowWidth=800
windowHeight=600
player=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.player=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("Snake game by Rumman")
self._running=True
self._apple_surf=pygame.image.load("d:\\saitama.jpg").convert()
self._image_surf=pygame.image.load("d:\\snake.jpg").convert()
def on_event(self,event):
if event.type==QUIT:
self._running=False
def on_loop(self):
self.player.update()
# does snake eat apple?
for i in range(0,self.player.length):
if self.game.isCollision(self.apple.x,self.apple.y,self.player.x[i],self.player.y[i],44):
self.apple.x=randint(2,9)*44
self.apple.y=randint(2,9)*44
self.player.length=self.player.length+1
# does snake collide with itself?
for i in range(2,self.player.length):
if self.game.isCollision(self.player.x[0],self.player.y[0],self.player.x[i],self.player.y[i],40):
print("You loose!")
print("x[0]("+str(self.player.x[0])+ "," +str(self.player.y[0])+")")
print("x[" + str(i) + "] (" + str(self.player.x[i]) + "," + str(self.player.y[i]) + ")")
exit(0)
pass
def on_render(self):
self._display_surf.fill((0,0,0))
self.player.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.player.moveRight()
if keys[K_LEFT]:
self.player.moveLeft()
if keys[K_UP]:
self.player.moveUp()
if keys[K_DOWN]:
self.player.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()