-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg.py
More file actions
325 lines (215 loc) · 6.98 KB
/
g.py
File metadata and controls
325 lines (215 loc) · 6.98 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import pygame
from pygame.locals import *
from numpy import loadtxt
import time
import random
#Constants for the game
WIDTH, HEIGHT = (32, 32)
black=(0,0,0)
WALL_COLOR = pygame.Color(0, 0, 255, 255) # BLUE
PACMAN_COLOR = pygame.Color(255, 0, 0, 255) # RED
COIN_COLOR = pygame.Color(255, 255, 0, 255) # RED
DOWN = (0,1)
RIGHT = (1,0)
TOP = (0,-1)
LEFT = (-1,0)
NONE = (0,0)
count=0
blue=pygame.image.load('blue.png')
blue=pygame.transform.scale(blue,(32,32))
wall=pygame.image.load('wall1.png')
wall=pygame.transform.scale(wall,(32,32))
pacman=pygame.image.load('yo.png')
pacman=pygame.transform.scale(pacman,(32,32))
coin=pygame.image.load('coin.png')
coin=pygame.transform.scale(coin,(32,32))
def count_time(seconds,minutes):
font=pygame.font.SysFont(None,50)
seconds="Time = " + str(minutes) + ' : ' + str(seconds)
text=font.render(seconds ,True,black)
screen.blit(text,(350,0))
def Lives_disp(count):
font=pygame.font.SysFont(None,40)
text=font.render(" Lives - " + str(count),True,black)
screen.blit(text,(200,0))
def add_enemy(screen,enm):
i=random.randint(1,4)
a=pixels_from_points(enm)
screen.blit(blue,a)
def enemy(pos,layout):
switch=random.randint(1,10)
duck=True
while duck :
switch=random.randint(1,10)
if switch==1 or switch==3 or switch ==5:
enemy_pos=add_to_pos(pos,LEFT)
elif switch==2 or switch==4 or switch==6:
enemy_pos=add_to_pos(pos,RIGHT)
elif switch==7 or switch ==8:
enemy_pos=add_to_pos(pos,TOP)
else:
enemy_pos=add_to_pos(pos,DOWN)
if obstacle(enemy_pos,layout):
duck=False
return enemy_pos
def score(count):
font=pygame.font.SysFont(None,25)
text=font.render(" Points - " + str(count),True,black)
screen.blit(text,(0,0))
def level_message():
font=pygame.font.SysFont(None,50)
text=font.render(" LEVEL COMPLETE ",True,black)
screen.blit(text,(153,300))
def crash_message():
font=pygame.font.SysFont(None,50)
text=font.render(" CRASHED ",True,black)
screen.blit(text,(153,300))
def coin_collection(new_pos,layout):
if layout[new_pos[1]][new_pos[0]]=="c":
layout[new_pos[1]][new_pos[0]]="."
return True
else:
return False
def obstacle(new_pos,layout):
if layout[new_pos[1]][new_pos[0]]=="w":
return False
else:
return True
#Draws a rectangle for the wall
def draw_wall(screen, pos):
pixels = pixels_from_points(pos)
screen.blit(wall,pixels)
# pygame.draw.rect(screen, WALL_COLOR, [pixels, (WIDTH, HEIGHT)])
#Draws a rectangle for the player
def draw_pacman(screen, pos):
pixels = pixels_from_points(pos)
screen.blit(pacman,pixels)
# pygame.draw.rect(screen, PACMAN_COLOR, [pixels, (WIDTH, HEIGHT)])
#Draws a rectangle for the coin
def draw_coin(screen, pos):
pixels = pixels_from_points(pos)
screen.blit(coin,pixels)
# pygame.draw.rect(screen, COIN_COLOR, [pixels, (WIDTH, HEIGHT)])
#Uitlity functions
def add_to_pos(pos, pos2):
return (pos[0]+pos2[0], pos[1]+pos2[1])
def pixels_from_points(pos):
return (pos[0]*WIDTH, pos[1]*HEIGHT)
#Initializing pygame
pygame.init()
screen = pygame.display.set_mode((640,640), 0, 32)
background = pygame.surface.Surface((640,640)).convert()
#Initializing variables
layout = loadtxt('layout.txt', dtype=str)
rows, cols = layout.shape
pacman_position = (1,1)
background.fill((0,0,0))
enemy1=(6,8)
enemy2=(17,1)
enemy3=(4,14)
enemy4=(16,18)
crash=False
# Main game loop
x_sec=0
lives=3
seconds=0
minutes=0
count = 0
while crash is False:
for event in pygame.event.get():
if event.type == QUIT:
exit()
#TODO: Take input from the user and update pacman moving direction, Currently hardcoded to always move down
# Pacman Movements....Keydown and keyup movements accordingly.
# Special Key - TAB to STop pacman..
screen.blit(background, (0,0))
#Draw board from the 2d layout array.
#In the board, '.' denote the empty space, 'w' are the walls, 'c' are the coins
for col in range(cols):
for row in range(rows):
value = layout[row][col]
pos = (col, row)
if value == 'w':
draw_wall(screen, pos)
elif value == 'c':
draw_coin(screen, pos)
#Draw the player
draw_pacman(screen, pacman_position)
add_enemy(screen,enemy1)
add_enemy(screen,enemy2)
add_enemy(screen,enemy3)
add_enemy(screen,enemy4)
enemy1=enemy(enemy1,layout)
enemy2=enemy(enemy2,layout)
enemy3=enemy(enemy3,layout)
enemy4=enemy(enemy4,layout)
move_direction = NONE
#TODO: Take input from the user and update pacman moving direction, Currently hardcoded to always move down
# Pacman Movements....Keydown and keyup movements accordingly.
# Special Key - TAB to STop pacman..
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
new_pos = add_to_pos(pacman_position, LEFT)
if obstacle(new_pos,layout):
move_direction=LEFT
elif event.key==pygame.K_RIGHT:
new_pos = add_to_pos(pacman_position, RIGHT)
if obstacle(new_pos,layout):
move_direction=RIGHT
elif event.key==pygame.K_UP:
new_pos = add_to_pos(pacman_position, TOP)
if obstacle(new_pos,layout):
move_direction=TOP
elif event.key==pygame.K_DOWN:
new_pos = add_to_pos(pacman_position, DOWN)
if obstacle(new_pos,layout):
move_direction=DOWN
elif event.key==pygame.K_TAB:
new_pos = add_to_pos(pacman_position, NONE)
if event.type==pygame.KEYUP:
if event.key==pygame.K_RIGHT :
new_pos = add_to_pos(pacman_position, RIGHT)
if obstacle(new_pos,layout)==1:
move_direction=RIGHT
elif event.key==pygame.K_LEFT:
new_pos = add_to_pos(pacman_position, LEFT)
if obstacle(new_pos,layout)==1:
move_direction=LEFT
elif event.key==pygame.K_UP:
new_pos = add_to_pos(pacman_position, TOP)
if obstacle(new_pos,layout)==1:
move_direction=TOP
elif event.key==pygame.K_DOWN:
new_pos = add_to_pos(pacman_position, DOWN)
if obstacle(new_pos,layout)==1:
move_direction=DOWN
#Update player position based on movement.
if coin_collection(pacman_position,layout):
count=count+1
score(count)
if count==28:
level_message()
pacman_position = add_to_pos(pacman_position, move_direction)
Lives_disp(lives)
if enemy1==pacman_position or enemy2==pacman_position or enemy3==pacman_position or enemy4==pacman_position :
lives=lives-1
Lives_disp(lives)
pacman_position=(1,1)
crash_message()
# crash=True
if lives==0:
crash=True
#TODO: Check if player ate any coin, or collided with the wall by using the layout array.
# player should stop when colliding with a wall
# coin should dissapear when eating, i.e update the layout array
#Update the display
x_sec=x_sec+1
if x_sec%10==0:
seconds=seconds+1
if seconds==60:
seconds=0
minutes=minutes+1
count_time(seconds,minutes)
pygame.display.update()
#Wait for a while, computers are very fast.
time.sleep(0.1)