-
Notifications
You must be signed in to change notification settings - Fork 0
/
infiltration.py
182 lines (138 loc) · 4.86 KB
/
infiltration.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
import pygame
import random
#defining colours
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0,)
BLUE = (0,0,225)
YELLOW = (255,255,0)
#black screen
size = (1000,720)
# width of each tile
width = 40
height = 40
screen = pygame.display.set_mode(size)
#title of the window
pygame.display.set_caption("Infiltration")
#exit game falg set to false
done = False
#screen refresh rate
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self,colour,width,height,speed):
super().__init__()
#set player dimentions
self.speed_x = 0
self.speed_y = 0
self.image = pygame.Surface([width,height])
self.image.fill(colour)
#set position of player need to make it so the player starts in a spot with no wall
self.rect = self.image.get_rect()
self.rect.x = 360
self.rect.y = 500
self.old_x = self.rect.x
self.old_y = self.rect.y
def update(self):
self.rect.x = self.rect.x + self.speed_x
self.rect.y = self.rect.y + self.speed_y
wall_hit_list = pygame.sprite.spritecollide(my_player, wall_group, False)
for x in wall_hit_list:
self.rect.x = self.old_x
self.rect.y = self.old_y
self.speed_x = 0
self.speed_y = 0
self.old_y = self.rect.y
self.old_x = self.rect.x
def player_set_speed(self,x,y):
self.speed_x = x
self.speed_y = y
class Wall(pygame.sprite.Sprite):
def __init__(self,colour,width,height,x,y):
super().__init__()
self.image = pygame.Surface([width,height])
self.image.fill(colour)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
#end procedure
#end class
class Maze:
def __init_(self,width,height):
super().__init__()
self.width = width
self.height = height
self.size = (width, height)
def generate(self):
numcols = 25
numrows = 25
map = [[random.randint(0,1) for i in range(numcols)]for j in range(numrows)]
'''map = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
]
'''
all_sprite_group = pygame.sprite.Group()
my_player = Player(YELLOW,40,40,5)
all_sprite_group.add(my_player)
wall_group = pygame.sprite.Group()
x = 0
y = 0
for row in map:
for column in row:
if column == 1:
my_wall = Wall(WHITE,width,height,x,y)
all_sprite_group.add(my_wall)
wall_group.add(my_wall)
x = x + 40
#end if
#next column
x = 0
y = y + 40
#next row
pygame.init()
#game loop
while not done:
#user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:#if left key is pressed
my_player.player_set_speed(-1,0)
elif event.key == pygame.K_RIGHT:
my_player.player_set_speed(1,0)
elif event.key == pygame.K_UP:
my_player.player_set_speed(0,-1)
elif event.key == pygame.K_DOWN:
my_player.player_set_speed(0,1)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
my_player.player_set_speed(0,0)
#
#update all sprites
all_sprite_group.update()
#screen background is black
screen.fill(BLACK)
#draw function
all_sprite_group.draw(screen)
#flip display to show new position of objects
pygame.display.flip()
clock.tick(240)
#end game loop
pygame.quit()