-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tank.py
144 lines (114 loc) · 5.13 KB
/
Tank.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
import enum
import os
import pygame
import Bullet
import Direction
import GameSettings
import GameObjects
class Tank(pygame.sprite.Sprite):
def __init__(self, bullet_level, armor_level, speed, is_tank_player):
pygame.sprite.Sprite.__init__(self)
self.images = []
self.move_x = 0
self.move_y = 0
self.frame = 0
self.previous_x, self.previous_y, self.previous_move_x, self.previous_move_y = 0, 0, 0, 0
self.direction = Direction.Direction.down
self.remaining_reload_time = 0
self.reload_time = GameSettings.change_for_fps(5)
self.bullet_level = bullet_level
self.armor_level = armor_level
self.tank_speed = GameSettings.change_for_fps(speed)
self.is_tank_player = is_tank_player
self.remaining_armor_level = armor_level
self.sliding_time = GameSettings.change_for_fps(20)
self.sliding_time_remaining = 0
self.did_slide = False
self.slide_x, self.slide_y = 0, 0
self.collision_type = 1
def control(self, x, y):
self.move_x = x
self.move_y = y
def set_positive_move_x(self):
self.move_x = self.tank_speed
def set_negative_move_x(self):
self.move_x = -self.tank_speed
def set_positive_move_y(self):
self.move_y = self.tank_speed
def set_negative_move_y(self):
self.move_y = -self.tank_speed
def unset_move_x(self):
self.move_x = 0
def unset_move_y(self):
self.move_y = 0
def update(self):
if self.sliding_time_remaining == 0 \
and GameObjects.GameObjects.instance.is_player_on_ice() \
and self.move_x == self.move_y == 0 \
and (self.previous_move_x != 0 or self.previous_move_y != 0) \
and not self.did_slide:
self.sliding_time_remaining = self.sliding_time
self.did_slide = True
self.slide_x = self.previous_move_x
self.slide_y = self.previous_move_y
if self.move_x != 0 or self.move_y != 0 or not GameObjects.GameObjects.instance.is_player_on_ice():
self.sliding_time_remaining = 0
self.did_slide = False
self.previous_x, self.previous_y = self.rect.x, self.rect.y
self.move_player()
self.rect.x = min(780 - self.image.get_size()[0], self.rect.x)
self.rect.x = max(0, self.rect.x)
self.rect.y = min(780 - self.image.get_size()[1], self.rect.y)
self.rect.y = max(0, self.rect.y)
self.remaining_reload_time -= 1
self.remaining_reload_time = max(0, self.remaining_reload_time)
def move_player(self):
if self.sliding_time_remaining != 0:
self.rect.x += self.slide_x
self.rect.y += self.slide_y
self.sliding_time_remaining -= 1
return
self.rect.x += self.move_x
self.previous_move_x = self.move_x
self.previous_move_y = 0
if self.move_x == 0:
self.rect.y += self.move_y
self.previous_move_y = self.move_y
self.set_direction()
def set_direction(self):
if self.rect.x - self.previous_x > 0:
self.direction = Direction.Direction.right
if self.rect.x - self.previous_x < 0:
self.direction = Direction.Direction.left
if self.rect.y - self.previous_y > 0:
self.direction = Direction.Direction.down
if self.rect.y - self.previous_y < 0:
self.direction = Direction.Direction.up
self.image = pygame.transform.scale(self.images[self.direction.value], (50, 50))
self.image.set_colorkey((0, 0, 0))
def undo_move(self):
self.rect.x, self.rect.y = self.previous_x, self.previous_y
def shoot(self):
if self.remaining_reload_time > 0:
return
if self.direction == Direction.Direction.up:
Bullet.Bullet(self.rect.x + 18, self.rect.y - 11, self.direction, self.is_tank_player, self.bullet_level)
if self.direction == Direction.Direction.right:
Bullet.Bullet(self.rect.x + 41, self.rect.y + 18, self.direction, self.is_tank_player, self.bullet_level)
if self.direction == Direction.Direction.down:
Bullet.Bullet(self.rect.x + 18, self.rect.y + 41, self.direction, self.is_tank_player, self.bullet_level)
if self.direction == Direction.Direction.left:
Bullet.Bullet(self.rect.x - 11, self.rect.y + 18, self.direction, self.is_tank_player, self.bullet_level)
self.remaining_reload_time = self.reload_time
def collider_with_bullet(self, bullet):
if (bullet.is_bullet_friendly and not self.is_tank_player) \
or (not bullet.is_bullet_friendly and self.is_tank_player):
self.remaining_armor_level -= 1
if self.remaining_armor_level == 0:
GameObjects.GameObjects.instance.dynamic_objects.remove(self)
if not self.is_tank_player:
GameObjects.GameObjects.instance.enemies.remove(self)
else:
GameObjects.GameObjects.instance.player = None
self.kill()
bullet.destroy_bullet()