-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
202 lines (144 loc) · 5.08 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
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
191
192
193
194
195
196
197
198
199
200
201
202
import pygame
import time
from pygame.locals import *
import random
#grape has dimensions 5x5
BACKGROUND = (230, 115, 80)
SIZE = 35
class Grape:
def __init__ (self, parentScreen):
self.grape = pygame.image.load("resources/grape.png").convert()
self.parentScreen = parentScreen
self.x = SIZE * 10
self.y = SIZE *10
def draw(self):
self.parentScreen.blit(self.grape, (self.x , self.y))
pygame.display.flip()
def move(self):
self.x = random.randint(1,20)*SIZE
self.y = random.randint(1,20)*SIZE
class Snake:
def __init__(self, parentScreen, length):
self.length = length
self.parentScreen = parentScreen
self.block = pygame.image.load("resources/block.jpg").convert()
self.x = [SIZE] * length
self.y = [SIZE] * length
self.direction = 'down'
def draw(self):
for i in range (self.length):
self.parentScreen.blit(self.block, (self.x[i] , self.y[i]))
pygame.display.flip()
def increaseLength(self):
self.length +=1
self.x.append(-1)
self.y.append(-1)
def moveLeft(self):
self.direction = 'left'
def moveRight(self):
self.direction = 'right'
def moveUp(self):
self.direction = 'up'
def moveDown(self):
self.direction = 'down'
def walk(self):
for i in range(self.length-1, 0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
if self.direction == 'up':
self.y[0] -= SIZE
if self.direction == 'down':
self.y[0] +=SIZE
if self.direction == 'left':
self.x[0] -= SIZE
if self.direction == 'right':
self.x[0] += SIZE
self.draw()
class Game:
def __init__(self):
pygame.init()
length = 1
#initialize pygame window
self.surface = pygame.display.set_mode((750,750))
self.surface.fill(BACKGROUND)
#snake class is inside the Game() class
self.snake = Snake(self.surface, length)
self.snake.draw()
#initialize grape
self.grape = Grape(self.surface)
self.grape.draw()
def collision(self, x1, y1, x2, y2):
if (x1 >= x2 and x1 < x2 + SIZE):
if (y1 >= y2 and y1 < y2 + SIZE):
return True
return False
def background(self):
bg = pygame.image.load("resources/background.jpg")
self.surface.blit(bg, (0,0))
def play(self):
self.background()
self.snake.walk()
self.grape.draw()
self.displayScore()
pygame.display.flip()
#snake eating grape
if self.collision(self.snake.x[0], self.snake.y[0], self.grape.x, self.grape.y):
self.snake.increaseLength()
self.grape.move()
#snake colliding with itself
for i in range(3, self.snake.length):
if self.collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]):
raise "Game Over"
def displayScore(self):
font = pygame.font.SysFont('arial', 30)
score = font.render(f"Score: {self.snake.length}", True, (255,255,255))
self.surface.blit(score, (600,10))
def gameOver(self):
self.background()
font = pygame.font.SysFont('arial', 30)
self.surface.fill(BACKGROUND)
line1 = font.render(f"Game Over! Your score is: {self.snake.length}", True, (255,255,255))
self.surface.blit(line1, (200,300))
line2 = font.render('Press Enter to replay!', True, (255,255,255))
self.surface.blit(line2, (200,350))
pygame.display.flip()
def reset(self):
self.snake = Snake(self.surface, 1)
self.grape = Grape(self.surface)
def run(self):
#event loop
run = True
pause = False
while run:
for ev in pygame.event.get():
if ev.type == KEYDOWN:
if (ev.key == K_RETURN):
pause = False
#escape to quit
if(ev.key == K_ESCAPE):
run = False
if not pause:
if(ev.key == K_UP):
self.snake.moveUp()
if(ev.key == K_DOWN):
self.snake.moveDown()
if(ev.key == K_LEFT):
self.snake.moveLeft()
if(ev.key == K_RIGHT):
self.snake.moveRight()
elif (ev.type == QUIT) :
run = False
try:
if not pause:
self.play()
except Exception as e:
self.gameOver()
pause = True
self.reset()
#timer to move snake automatically
time.sleep(0.1)
#main routine
if __name__ == "__main__":
game = Game()
game.run()
pygame.display.flip()