-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_vis.py
More file actions
294 lines (221 loc) · 7.35 KB
/
game_vis.py
File metadata and controls
294 lines (221 loc) · 7.35 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
import pygame
import time
import os
def load_image(name, color_key=None):
fullname = os.path.join('data', name)
print(fullname)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Не удается загрузить', fullname)
raise SystemExit(message)
if color_key is not None:
if color_key == -1:
color_key = image.get_at((0, 0))
image.set_colorkey(color_key)
return image
pygame.init()
display_width = 1280
display_height = 1024
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Teleportation cube')
icon = load_image('usr_slavs.png')
pygame.display.set_icon(icon)
background = pygame.image.load(r'background.jpg')
enemy_width, enemy_height = 35, 70
enemy_x, enemy_y = display_width - 50, display_height - enemy_height - 150
usr_width = 75
usr_height = 150
usr_x = display_width // 3 - 200
usr_y = display_height - usr_height - 500
usr_image = load_image('usr.stand.png')
clock = pygame.time.Clock()
FPS = 80
invisible = False
can_jump = False
can_tp_up = False
can_tp_down = False
time_over = False
count_jump = 30
count_tp = 1
sit_yep = False
class Enemy_classic:
def __init__(self, x, y, width, height, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = speed
def move(self):
if self.x >= -self.width:
pygame.draw.rect(display, (30, 144, 255), (self.x, self.y, self.width, self.height))
self.x -= self.speed
else:
self.x = display_width - 50
def game_running():
global can_jump, can_tp_up, sit_yep, usr_image,can_tp_down, invisible, usr_color
game = True
enemy_arr_one_floor = []
enemy_arr_two_floor = []
create_enemy_classic(enemy_arr_one_floor)
create_bullet_enemy(enemy_arr_two_floor)
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
print('w')
can_tp_up = True
if event.key == pygame.K_DOWN:
print('s')
can_tp_down = True
usr_image = load_image('usr.stand.png')
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
print('SPACE')
can_jump = True
if keys[pygame.K_x]:
print('x')
sprint()
if keys[pygame.K_z]:
print('Z')
invisible = True
if keys[pygame.K_d] or keys[pygame.K_a]:
print('уже иду!')
go()
if keys[pygame.K_c]:
sit_yep = True
if keys[pygame.K_ESCAPE]:
pause()
if can_jump:
jump()
if sit_yep:
sit()
if can_tp_down or can_tp_up:
teleport()
if invisible:
cube_inv()
if keys[pygame.K_BACKSPACE]:
game = False
display.blit(background, (0, 0))
draw_enemy_classic(enemy_arr_one_floor)
draw_enemy_classic(enemy_arr_two_floor)
draw_usr()
draw_terra()
if check_collision(enemy_arr_one_floor) or check_collision(enemy_arr_two_floor):
game = False
pygame.display.update()
clock.tick(FPS)
return game_over()
def draw_usr():
display.blit(usr_image, (usr_x, usr_y))
def jump():
global usr_y, count_jump, can_jump
if count_jump >= -30:
usr_y -= count_jump / 3
count_jump -= 1
else:
count_jump = 30
can_jump = False
def teleport():
global usr_y, can_jump,can_tp_up, display_height, usr_height, can_tp_down
print('Лети вверх')
if not can_jump:
if can_tp_down and usr_y + 300 < 1024:
usr_y += 300
if can_tp_up and usr_y - 300 > 0:
usr_y -= 300
can_tp_up = False
can_tp_down = False
def cube_inv():
global invisible
print('меняю цвет')
invisible = False
def go():
global usr_x
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
if usr_x <= 1205: #чтобы персонаж не выходил за пределы окна
usr_x += 2
if keys[pygame.K_a]:
if usr_x >= 0:#чтобы персонаж не выходил за пределы окна
usr_x -= 2
def sit():
global usr_image, sit_yep, usr_height
if sit_yep:
usr_image = load_image('usr.png')
usr_height -= 125
sit_yep = False
usr_height += 125
def sprint():
global usr_x
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
for i in range(1):
if usr_x - 5 > 0:
usr_x -= 5
if keys[pygame.K_d]:
for i in range(1):
if usr_x + 5 < 1180:
usr_x += 5
def draw_terra():
pygame.draw.line(display, (0, 0, 0), [0, display_height - 150], [1280, display_height - 150], 5)
pygame.draw.line(display, (0, 0, 0), [0, display_height - 450], [1280, display_height - 450], 5)
pygame.draw.line(display, (0, 0, 0), [0, display_height - 750], [1280, display_height - 750], 5)
def create_enemy_classic(array):
array.append(Enemy_classic(display_width - 50, display_height - 240, 40, 90, 9))
array.append(Enemy_classic(display_width + 300, display_height - 220, 60, 70, 5))
array.append(Enemy_classic(display_width - 500, display_height - 165, 200, 15, 3))
def create_bullet_enemy(array):
array.append(Enemy_classic(display_width - 100, display_height - 600, 30, 10, 5))
array.append(Enemy_classic(display_width + 350, display_height - 600, 30, 10, 5))
array.append(Enemy_classic(display_width - 550, display_height - 600, 30, 10, 5))
def draw_enemy_classic(array):
for enemy_classic in array:
Enemy_classic.move(enemy_classic)
def print_text(massage, x, y, font_color = (0, 0, 0), font_type = 'fonts.otf', font_size = 40):
font_type = pygame.font.Font(font_type, font_size)
text = font_type.render(massage, True, font_color)
display.blit(text, (x, y))
def pause():
paused = True
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print_text('Пауза', 160, 300)
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
paused = False
pygame.display.update()
clock.tick(30)
def check_collision(barriers):
for barrier in barriers:
if usr_y + usr_height >= barrier.y + barrier.height:
if barrier.x <= usr_x <= barrier.x + barrier.width:
return True
elif barrier.x <= usr_x + usr_width <= barrier.x + barrier.width:
return True
return False
def game_over():
stoped = True
while stoped:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print_text('Game over', 160, 300)
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
return True
if keys[pygame.K_ESCAPE]:
return False
pygame.display.update()
clock.tick(30)
while game_running():
pass
pygame.quit()
quit()