-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
140 lines (101 loc) · 3.31 KB
/
game.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
import pygame
import time
pygame.init()
width = 900
height = 650
dark = (109, 104, 117)
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("MAZE Game")
maze=[[1,3,1,0,1,0,1,0,0,0,0,1,1,0,0],
[1,0,1,0,1,0,1,0,1,0,0,1,0,0,0],
[1,0,0,0,1,0,0,0,1,0,0,0,0,1,0],
[1,0,1,0,1,1,0,1,1,1,1,1,0,1,0],
[1,0,1,0,1,0,0,1,0,0,1,0,0,1,0],
[1,1,1,0,1,0,0,0,0,0,1,0,0,1,0],
[1,0,0,0,1,0,0,0,1,0,1,1,1,0,0],
[1,0,1,1,1,1,1,1,1,0,1,0,0,0,0],
[1,0,1,0,0,0,0,0,0,0,1,0,0,0,0],
[1,0,0,0,0,1,1,0,1,1,1,1,1,1,0],
[1,1,1,1,1,1,0,0,0,0,0,0,0,0,2],]
def renderMaze(maze):
x = 0
y = 0
for row in maze:
for block in row:
if block == 0:
pygame.draw.rect(gameDisplay, (255, 205, 178), (x, y, 60, 60))
elif block == 1:
pygame.draw.rect(gameDisplay, (229, 152, 155) ,(x, y, 60, 60))
elif block == 2:
pygame.draw.rect(gameDisplay, (255, 183, 0), (x, y, 60, 60))
elif block == 3:
pygame.draw.rect(gameDisplay, (120, 150, 100), (x, y, 60, 60))
x = x+60
y = y+60
x = 0
def displayText(text):
renderFont = pygame.font.Font('freesansbold.ttf', 45)
textsc = renderFont.render(text, True, dark)
surface, rect = textsc, textsc.get_rect()
rect.center = ((width/2),(height/2))
gameDisplay.blit(surface, rect)
pygame.display.update()
time.sleep(1)
x = 1
y = 0
dest = 0
gameDisplay.fill((150,150,255))
while True:
renderMaze(maze)
if dest == 1:
displayText("Yay! Destination reached!")
exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
block = maze[y][x-1]
if block == 0:
maze[y][x- 1]= 2
maze[y][x] = 0
x = x -1
elif block == 2:
maze[y][x-1] = 3
maze[y][x] = 0
x = x-1
dest =1
if event.key == pygame.K_RIGHT:
block = maze[y][x+1]
if block == 0:
maze[y][x + 1] =2
maze[y][x]=0
x = x+ 1
elif block == 2:
maze[y][x+1] = 3
maze[y][x] = 0
x = x + 1
dest = 1
if event.key == pygame.K_UP:
block = maze[y- 1][x]
if block == 0:
maze[y -1][x]= 2
maze[y][x]= 0
y =y -1
elif block == 2:
maze[y -1][x] = 3
maze[y][x] = 0
y = y- 1
dest=1
if event.key == pygame.K_DOWN:
block=maze[y +1][x]
if block == 0:
maze[y+ 1][x]=2
maze[y][x]=0
y = y + 1
elif block == 2:
maze[y+1][x] = 3
maze[y][x] = 0
y = y+ 1
dest = 1
pygame.display.update()