-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.py
190 lines (147 loc) · 5.8 KB
/
Snake.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import sys, pygame
from pygame import *
import random
import time
class player:
x = [0]
y = [50]
playerWidth = 0
playerHeight = 0
direction = 0
gameDelay = 1
playerWait = 0
def __init__(self, length):
self.length = length
self.image = pygame.image.load("Images//block.jpg")
self.playerWidth = self.image.get_rect().size[0]
self.playerHeight = self.image.get_rect().size[1]
for i in range(self.length - 1):
self.x.append(0)
self.y.append(50)
self.x[i] = (self.length - i - 1) * self.playerWidth
def playerAutoMove(self):
self.playerWait += 1
if (self.playerWait >= self.gameDelay):
# update player body
for i in range(self.length - 1, 0, -1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
# update player head
if(self.direction == 0):
self.x[0] += self.playerWidth
elif(self.direction == 1):
self.x[0] -= self.playerWidth
elif(self.direction == 2):
self.y[0] -= self.playerHeight
elif(self.direction == 3):
self.y[0] += self.playerHeight
self.playerWait = 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 growPlayer(self, screen):
self.y.append(self.y[-1])
self.x.append(self.x[-1])
self.length += 1
def drawPlayer(self, screen):
for i in range(self.length):
screen.blit(self.image, (self.x[i], self.y[i]))
class apple:
x = 0
y = 0
appleWidth = 0
appleHeight = 0
appleCounter = 0
def __init__(self):
self.image = pygame.image.load("Images//apple.png")
self.appleWidth = self.image.get_rect().size[0]
self.appleHeight = self.image.get_rect().size[1]
self.positioning()
def positioning(self):
self.x = random.randrange(app.windowWidth - self.appleWidth)
self.y = random.randrange(app.windowHeight - self.appleHeight)
class app:
windowWidth = 800
windowHeight = 600
color = 255,255,255
def __init__(self):
self.running = True
self.player = player(3)
self.apple = apple()
self.screen = pygame.display.set_mode((app.windowWidth, app.windowHeight))
pygame.display.set_caption("Template")
def on_init(self):
pygame.init()
self.running = True
pygame.font.init()
self.font1 = pygame.font.SysFont('Open Sans', 30)
def renderField(self):
self.screen.fill(self.color)
self.score = self.font1.render('Score: ' + str(apple.appleCounter), True, (0, 0, 0))
self.screen.blit(self.score, (0,0))
self.player.drawPlayer(self.screen)
self.screen.blit(self.apple.image, (self.apple.x, self.apple.y))
pygame.display.flip()
def playerCollision(self, x1, y1, x2, y2, position):
# collision with snake or with area borders
if((x1 == x2 and y1 == y2) or (self.player.x[0] < 0 or self.player.y[0] < 0 or self.player.y[0] > app.windowHeight - self.player.playerHeight or self.player.x[0] > app.windowWidth - self.player.playerWidth)):
self.screen.fill(self.color)
self.score = self.font1.render("Game Over! You reached: " + str(apple.appleCounter) + " points " + "You collided at: " + "( " + str(x1) + ", " + str(y1) + " )" + " with position: " + str(position) , True, (0, 0, 0))
self.screen.blit(self.score, (0, self.windowHeight / 2 - self.font1.get_height() / 2))
pygame.display.flip()
self.running = False
while True:
self.checkQuit()
def checkCollision(self, x1, y1, x2, y2, objectWidth, objectHeight):
if(x1 + self.player.playerWidth >= x2 and x1 <= x2 + objectWidth):
if(y1 + self.player.playerHeight >= y2 and y1 <= y2 + objectHeight):
apple.appleCounter += 1
self.player.growPlayer(self.screen)
return(True)
return(False)
# Main function
def execute(self):
if self.on_init() == False:
self.running = False
while self.running:
pygame.event.pump()
keys = pygame.key.get_pressed()
# set movement direction
if(keys[K_RIGHT]):
self.player.moveRight()
elif(keys[K_LEFT] ):
self.player.moveLeft()
elif(keys[K_UP] ):
self.player.moveUp()
elif(keys[K_DOWN]):
self.player.moveDown()
elif(keys[K_ESCAPE]):
self.running = False
# player automove
self.player.playerAutoMove()
# apple collsion
if(self.checkCollision(self.player.x[0], self.player.y[0], self.apple.x, self.apple.y, self.apple.appleWidth, self.apple.appleHeight)):
self.apple.positioning()
# Player Collision
for i in range(1, self.player.length):
self.playerCollision(self.player.x[0], self.player.y[0], self.player.x[i], self.player.y[i], i)
# field rendering
self.renderField()
time.sleep(100.0 / 1000.0)
self.checkQuit()
def checkQuit(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
pygame.quit()
sys.exit()
if __name__ == "__main__":
app.windowWidth = 800
app.windowHeight = 600
game = app()
game.execute()