-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pyw
More file actions
249 lines (190 loc) · 6.33 KB
/
main.pyw
File metadata and controls
249 lines (190 loc) · 6.33 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# coding=utf-8
"""
* File Name : main.pyw
* Created By : Natan Žabkar, natan.zabkar@gmail.com
* Creation Date : 02-02-2012
* Last Modified : 2.2.2012 3:34:44
"""
import pygame
import random
def drawSquare(screen, position, color):
"""
Draw on the screen on position x, y (x and y are not coordinates, they're the grid position!)
Upperleft square is 0,0
"""
screen.fill(color, (position[0] * w + 1, position[1] * h + 1, w - 1, h - 1))
class Food:
def __init__(self, screen, x, y, color, nutrition):
self.screen = screen
self.x = x
self.y = y
self.color = color
self.nutrition = nutrition
def givePos(self):
return (self.x, self.y)
def draw(self):
drawSquare(self.screen, self.givePos(), self.color)
class Snake:
def __init__(self, screen, bgcolor, x, y, color, grow_to = 2):
self.screen = screen
self.bgcolor = bgcolor
self.x = x
self.y = y
self.color = color
self.grow_to = grow_to - 1
self.vx = 0
self.vy = 0
self.body = []
self.crashed = False
self.length = 0
self.speed = 1
def eat(self, n):
self.grow_to += n
def givePos(self):
return (self.x, self.y)
def keyHandler(self, event):
try:
if(event.key == pygame.K_UP and (self.x, self.y - 1) != self.body[0]):
self.vx = 0
self.vy = -self.speed
elif(event.key == pygame.K_DOWN and (self.x, self.y + 1) != self.body[0]):
self.vx = 0
self.vy = self.speed
elif(event.key == pygame.K_LEFT and (self.x - 1, self.y) != self.body[0]):
self.vx = -self.speed
self.vy = 0
elif(event.key == pygame.K_RIGHT and (self.x + 1, self.y) != self.body[0]):
self.vx = self.speed
self.vy = 0
except:
if(event.key == pygame.K_UP):
self.vx = 0
self.vy = -self.speed
elif(event.key == pygame.K_DOWN):
self.vx = 0
self.vy = self.speed
elif(event.key == pygame.K_LEFT):
self.vx = -self.speed
self.vy = 0
elif(event.key == pygame.K_RIGHT):
self.vx = self.speed
self.vy = 0
def draw(self):
# Initial draw, other drawing will be done in move function
drawSquare(self.screen, (self.x,self.y), self.color)
def move(self):
if(self.vx != 0 or self.vy != 0):
self.body.insert(0, (self.x, self.y))
self.x += self.vx
self.y += self.vy
if((self.x, self.y) in self.body):
self.crashed = True
return False
if(self.x < 0 or self.x >= wid):
self.crashed = True
return False
if(self.y < 0 or self.y >= hei):
self.crashed = True
return False
# Draw the head that moved
drawSquare(self.screen, (self.x,self.y), self.color)
if(self.grow_to > self.length):
self.length += 1
# If too long, pop the last element of body and "delete" that square
if(len(self.body) > self.length):
drawSquare(self.screen, self.body.pop(), self.bgcolor)
"""
The user can now enter different game modes. He can choose between different sizes (50x50, 30x30, 15x15) and speeds (5, 10, 20 fps)
"""
x = int(input("What size would you like the playing field to be? Enter the appropriate number:\n"
+ "\t1. 15x15\n"
+ "\t2. 30x30\n"
+ "\t3. 50x50\n"))
while(x not in (1,2,3)):
print("Please enter one of the provided numbers!")
x = int(input("What size would you like the playing field to be? Enter the appropriate number:\n"
+ "\t1. 15x15\n"
+ "\t2. 30x30\n"
+ "\t3. 50x50\n"))
y = int(input("How fast would you like the game to be? Enter the appropriate number:\n"
+ "\t1. Slow\n"
+ "\t2. Medium\n"
+ "\t3. Fast\n"))
while(y not in (1,2,3)):
y = int(input("How fast would you like the game to be? Enter the appropriate number:\n"
+ "\t1. Slow\n"
+ "\t2. Medium\n"
+ "\t3. Fast\n"))
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
bgcolor = white
fruitcolors = [black, white, red, green, blue]
width = 600
height = 600
if(x == 1):
wid = 15
hei = 15
elif(x == 2):
wid = 30
hei = 30
elif(x == 3):
wid = 50
hei = 50
w = width//wid
h = height//hei
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
# Initialises speed
if(y == 1):
speed = 5
elif(y == 2):
speed = 10
elif(y == 3):
speed = 20
running = True
# Makes background and grid
screen.fill(bgcolor)
for i in range(w, width, w):
pygame.draw.line(screen, black, (i,0), (i,height))
for i in range(h, height, h):
pygame.draw.line(screen, black, (0,i), (height,i))
# Makes snake and does the initial drawing
snake = Snake(screen, bgcolor, wid//2 - 1, hei//2 - 1, black, 5)
snake.draw()
# Makes sure food isn't colored the same as snake
if(snake.color in fruitcolors):
fruitcolors.remove(snake.color)
# Makes sure food isn't colored the same as background (making it invisible)
if(bgcolor in fruitcolors):
fruitcolors.remove(bgcolor)
foo = random.randint(0, wid-1)
bar = random.randint(0, hei-1)
while((foo, bar) in snake.body):
foo = random.randint(0, wid-1)
bar = random.randint(0, hei-1)
food = Food(screen, foo, bar, fruitcolors[random.randint(0, len(fruitcolors)-1)], 1)
food.draw()
pygame.display.flip()
while running:
snake.move()
if(snake.crashed):
running = False
for event in pygame.event.get():
if(event.type == pygame.QUIT):
running = False
if(event.type == pygame.KEYDOWN):
snake.keyHandler(event)
if(snake.givePos() == food.givePos()):
snake.eat(food.nutrition)
foo = random.randint(0, wid-1)
bar = random.randint(0, hei-1)
while((foo, bar) in snake.body):
foo = random.randint(0, wid-1)
bar = random.randint(0, hei-1)
food = Food(screen, foo, bar, fruitcolors[random.randint(0, len(fruitcolors)-1)], 1)
food.draw()
pygame.display.flip()
clock.tick(speed)