-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanks.py
308 lines (259 loc) · 9.5 KB
/
tanks.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
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
import os
import sys
from math import cos, sin, radians
import pygame
WIDTH, HEIGHT = 800, 600
PLAYERONEKEY = pygame.K_a
PLAYERTWOKEY = pygame.K_l
ANGLE_SPEED = 0.25
BULLET_SPEED = 1.5
BULLET_RADIUS = 10
TANK_SPEED = 0.3
p_pos = ((700, 20), (80, 480))
all_sprites = pygame.sprite.Group()
boxes = pygame.sprite.Group()
p1bullets = pygame.sprite.Group()
p2bullets = pygame.sprite.Group()
vertical_borders = pygame.sprite.Group()
horizontal_borders = pygame.sprite.Group()
tanks = pygame.sprite.Group()
win_window_sprites = pygame.sprite.Group()
players = [True, True]
players_colors = ['blue', 'red']
def rot_center(image, rect, angle):
"""rotate an image while keeping its center"""
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image, rot_rect
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
if not os.path.isfile(fullname):
print(f"Файл с изображением '{fullname}' не найден")
sys.exit()
image = pygame.image.load(fullname)
return image
def load_map(filename):
with open(f"data/maps/{filename}", "r") as map:
info = map.read()
box_coordinates = [tuple(int(g) for g in i.split(',')) for i in info.split(';')]
return box_coordinates
def set_boxes(coordinate_mass):
for i in coordinate_mass:
print(i)
a = Box(*i, load_image("tank box.png"))
class Bullet(pygame.sprite.Sprite):
def __init__(self, radius, x, y, angle, pn):
if pn == 1:
super().__init__(p1bullets)
else:
super().__init__(p2bullets)
self.radius = radius
self.image = pygame.Surface((2 * radius, 2 * radius),
pygame.SRCALPHA, 32)
pygame.draw.circle(self.image, pygame.Color("grey"),
(radius, radius), radius)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.vx = BULLET_SPEED * cos(radians(angle + 90))
self.vy = -BULLET_SPEED * sin(radians(angle + 90))
self.pos = x, y
self.player_number = pn
def update(self):
self.pos = self.pos[0] + self.vx, self.pos[1] + self.vy
self.rect.x, self.rect.y = self.pos
if pygame.sprite.spritecollideany(self, vertical_borders) or pygame.sprite.spritecollideany(self,
horizontal_borders):
self.kill()
if pygame.sprite.spritecollideany(self, tanks):
sprite = pygame.sprite.spritecollideany(self, tanks)
if sprite.get_player_number() != self.player_number:
sprite.death()
self.kill()
elif pygame.sprite.spritecollideany(self, boxes):
sprite = pygame.sprite.spritecollideany(self, boxes)
self.kill()
sprite.bullet_collision()
def get_player_number(self):
return self.player_number
class Tank(pygame.sprite.Sprite):
def __init__(self, color, player_number, im_name, pos):
super().__init__(all_sprites, tanks)
self.color = color
self.number = player_number
self.image = load_image(im_name)
self.rect = self.image.get_rect()
self.pos = pos
self.rect.x = pos[0]
self.rect.y = pos[1]
self.vx, self.vy = 0, 0
self.rot = 0
self.rot_speed = ANGLE_SPEED
self.last_update = pygame.time.get_ticks()
self.image_orig = self.image.copy()
self.player_alive = True
self.orig_rot_speed = 0.25
self.rotating = True
self.player_number = player_number
def update(self, ticks=0):
if self.player_alive:
if not self.rotating:
self.collision()
self.pos = self.pos[0] + self.vx, self.pos[1] + self.vy
self.rect.x, self.rect.y = int(self.pos[0]), int(self.pos[1])
# if pygame.sprite.spritecollideany(self, boxes):
# self.vx = 0
# self.vy = 0
# self.rotating = True
if not self.player_alive:
return
if ticks and self.rotating:
self.rotate(ticks)
def rotate(self, ticks):
self.image, self.rect = rot_center(self.image_orig, self.rect, self.rot)
self.rot += self.rot_speed
self.rot %= 360
def shoot(self):
if self.player_alive:
Bullet(10, *self.rect.center, self.rot, self.player_number)
self.vx, self.vy = TANK_SPEED * cos(radians(self.rot + 90)), -TANK_SPEED * sin(radians(self.rot + 90))
self.rotating = False
def rotate_change(self):
self.rotating = True
self.rot_speed = -self.rot_speed
self.vx, self.vy = 0, 0
def collision(self):
if pygame.sprite.spritecollideany(self, horizontal_borders):
self.vy = 0
else:
self.vy = -TANK_SPEED * sin(radians(self.rot + 90))
if pygame.sprite.spritecollideany(self, vertical_borders):
self.vx = 0
else:
self.vx = TANK_SPEED * cos(radians(self.rot + 90))
def get_rot(self):
return self.rot
def get_rot_speed(self):
return self.rot_speed
def get_pos(self):
return self.pos
def get_rect(self):
return self.rect
def get_player_number(self):
return self.player_number
def get_tank_info(self):
return (
self.get_pos(),
self.get_rot_speed(),
self.get_rect(),
self.get_player_number()
)
def death(self):
self.rect.move(10000, 10000)
self.player_alive = False
global players
players[self.player_number - 1] = self.player_alive
def set_alive(self):
self.player_alive = True
global players
players[self.player_number - 1] = self.player_alive
self.rect.x = p_pos[self.player_number - 1][0]
self.rect.y = p_pos[self.player_number - 1][1]
self.pos = p_pos[self.player_number - 1]
def win(screen_out, player):
font = pygame.font.SysFont('arial', 50)
text = font.render(f'player {player + 1} won', True, players_colors[player])
text_x = WIDTH // 2 - text.get_width() // 2
text_y = HEIGHT // 3 - text.get_height() // 2
screen_out.blit(text, (text_x, text_y))
class Border(pygame.sprite.Sprite): # window borders
def __init__(self, x1, y1, x2, y2):
super().__init__(all_sprites)
if x1 == x2:
self.add(vertical_borders)
self.image = pygame.Surface([1, y2 - y1])
self.rect = pygame.Rect(x1, y1, 1, y2 - y1)
else:
self.add(horizontal_borders)
self.image = pygame.Surface([x2 - x1, 1])
self.rect = pygame.Rect(x1, y1, x2 - x1, 1)
class Box(pygame.sprite.Sprite):
def __init__(self, x, y, pic):
super().__init__(boxes, all_sprites)
self.bullet_counter = 0
self.image = pic
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
Border(x, y, x + 64, y)
Border(x, y + 64, x + 64, y + 64)
Border(x, y, x, y + 64)
Border(x + 64, y, x + 64, y + 64)
def remove_box(self):
self.rect.x = 10000
self.rect.y = 10000
self.kill()
def bullet_collision(self):
self.bullet_counter += 1
if self.bullet_counter >= 10:
self.remove_box()
def set_bullet_count(self, n):
self.bullet_counter = n
def tank_collision(self):
pass
class win_window(pygame.sprite.Sprite):
def __init__(self, win_pic):
super().__init__(win_window_sprites)
self.image = win_pic
self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = 0
p1 = Tank("red", 1, "tank player1.png", (700, 20))
p2 = Tank("blue", 2, "tank player2.png", (80, 480))
def tanks_run(key_one, key_two):
PLAYERONEKEY = key_one
PLAYERTWOKEY = key_two
global players
players = [True, True]
pygame.init()
pygame.display.set_caption('Tanks')
size = WIDTH, HEIGHT
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
map_coords = load_map("tank_map.txt")
set_boxes(map_coords)
Border(5, 5, WIDTH - 5, 5)
Border(5, HEIGHT - 5, WIDTH - 5, HEIGHT - 5)
Border(5, 5, 5, HEIGHT - 5)
Border(WIDTH - 5, 5, WIDTH - 5, HEIGHT - 5)
p1.set_alive()
p2.set_alive()
running = True
while running:
screen.fill((50, 255, 139))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
global tanks
if event.type == pygame.KEYDOWN:
if event.key == PLAYERONEKEY:
p1.shoot()
elif event.key == PLAYERTWOKEY:
p2.shoot()
elif event.key == pygame.K_ESCAPE:
running = False
elif event.type == pygame.KEYUP:
if event.key == PLAYERONEKEY:
p1.rotate_change()
elif event.key == PLAYERTWOKEY:
p2.rotate_change()
tick_passed = clock.tick()
p1.update(tick_passed)
all_sprites.draw(screen)
all_sprites.update(tick_passed)
p1bullets.draw(screen)
p2bullets.draw(screen)
p1bullets.update()
p2bullets.update()
if not all(players):
win(screen, players.index(False))
pygame.display.flip()