-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.py
More file actions
221 lines (164 loc) · 8.27 KB
/
Copy pathShell.py
File metadata and controls
221 lines (164 loc) · 8.27 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
#tank_shells
import pygame
import math
from pygame.sprite import Sprite
clock = pygame.time.Clock()
class Shell(Sprite):
def __init__(self, image, screen, player): #, mouse_pos):
super(Shell, self).__init__()
self.image = pygame.image.load(image)
self.image = pygame.transform.scale(self.image, (15,15))
self.screen = screen
self.speed = 20
self.angle = player.top_angle
self.angle_rad = self.angle * math.pi / 180
self.shot_length = 0
self.shot_length_current = 0
self.start_x = player.x + math.cos(self.angle_rad)* 50 #_top
self.start_y = player.y - math.sin(self.angle_rad)* 50 #_top
self.x = self.start_x
self.y = self.start_y
self.mouse_pos = pygame.mouse.get_pos()
self.load_scaled_fires()
self.load_scaled_explosions()
self.shot_start_tick = pygame.time.get_ticks()
self.shot_end_tick = 0
self.add_x = self.speed * math.cos(self.angle_rad)
self.dec_y = self.speed * math.sin(self.angle_rad)
def draw_shot(self):
self.draw_fire()
if self.shot_length - self.shot_length_current == 0:
self.draw_explosion()
else:
self.draw_bullet()
def update(self, player, obj_list):
x2 = self.mouse_pos[0]
y2 = self.mouse_pos[1]
# self.rect = pygame.Rect(self.x, self.y, 20, 20)
self.shot_length = math.sqrt((x2 - self.start_x)**2 +(y2 - self.start_y)**2)
self.shot_length_current = math.sqrt((self.x - self.start_x)**2 + (self.y - self.start_y)**2)
# print self.collide(obj_list, [self.x, self.y])
if self.collide(obj_list, [self.x, self.y]):
self.shot_length_current = self.shot_length
x_collided = self.collide(obj_list, [self.x, self.y])[0]
y_collided = self.collide(obj_list, [self.x, self.y])[1]
self.x = x_collided
self.y = y_collided
# print self.shot_length, self.shot_length_current
else:
if (self.shot_length - self.shot_length_current) < self.speed:
self.x = x2
self.y = y2
#i can put it beyond the screen instead
elif self.shot_length > self.shot_length_current:
print self.shot_length - self.shot_length_current
self.x += self.add_x
self.y -= self.dec_y
# self.y -= math.sqrt((self.speed ** 2) - (self.speed * math.cos(angle_radians))**2)
# print self.x, start_x
## change self.x, y like top_image
# else:
# self.x = x2
# self.y = y2
# print "stopped shooting"
# ##add explosion
# return
#update last tick
if self.shot_length - self.shot_length_current > 0:
self.shot_end_tick = pygame.time.get_ticks()
def load_scaled_fires(self):
self.fire0 = self.load_scale("images/tank_fire01.png", (30,30))
self.fire1 = self.load_scale("images/tank_fire11.png", (30,30))
self.fire2 = self.load_scale("images/tank_fire21.png", (30,30))
def load_scaled_explosions(self):
self.explosion0 = self.load_scale("images/explosion0.png", (60,60))
self.explosion1 = self.load_scale("images/explosion1.png", (60,60))
self.explosion2 = self.load_scale("images/explosion2.png", (60,60))
self.explosion3 = self.load_scale("images/explosion3.png", (60,60))
self.explosion4 = self.load_scale("images/explosion4.png", (60,60))
def load_scale(self, image_address, scale_tuple):
loaded_image = pygame.image.load(image_address)
scaled_image = pygame.transform.scale(loaded_image, scale_tuple)
return scaled_image
def copy_rotate(self, image, angle):
copied_image = image.copy()
copied_image = pygame.transform.rotate(copied_image, angle)
return copied_image
def draw_fire(self):
copied_fire0 = self.copy_rotate(self.fire0, self.angle)
copied_fire1 = self.copy_rotate(self.fire1, self.angle)
copied_fire2 = self.copy_rotate(self.fire2, self.angle)
change_x0 = copied_fire0.get_rect().center[0]
change_y0 = copied_fire0.get_rect().center[1]
change_x1 = copied_fire1.get_rect().center[0]
change_y1 = copied_fire1.get_rect().center[1]
change_x2 = copied_fire2.get_rect().center[0]
change_y2 = copied_fire2.get_rect().center[1]
far_from_center_x = math.cos(self.angle_rad) * 15
far_from_center_y = - math.sin(self.angle_rad) * 15
if (self.shot_length > 80):
if self.shot_length_current < 23:
self.screen.blit(copied_fire0, [self.start_x - change_x0 + far_from_center_x, self.start_y - change_y0 + far_from_center_y])
elif self.shot_length_current < 63:
self.screen.blit(copied_fire1, [self.start_x - change_x1 + far_from_center_x, self.start_y - change_y1 + far_from_center_y])
elif self.shot_length_current < 80:
self.screen.blit( copied_fire1, [self.start_x - change_x2 + far_from_center_x, self.start_y - change_y2 + far_from_center_y])
elif self.shot_length > 25:
if self.shot_length_current < 20:
self.screen.blit(copied_fire1, [self.start_x - change_x1 + far_from_center_x, self.start_y - change_y1 + far_from_center_y])
elif self.shot_length_current < 25:
self.screen.blit(copied_fire2, [self.start_x - change_x2 + far_from_center_x, self.start_y - change_y2 + far_from_center_y] )
def draw_explosion(self):
#recentering
change_coo = self.explosion0.get_rect().center[0]
current_tick = pygame.time.get_ticks()
if current_tick - self.shot_end_tick < 60:
self.screen.blit(self.explosion0, [self.x - change_coo, self.y - change_coo])
elif current_tick - self.shot_end_tick < 130:
self.screen.blit(self.explosion1, [self.x - change_coo, self.y - change_coo])
elif current_tick - self.shot_end_tick < 200:
self.screen.blit(self.explosion2, [self.x - change_coo, self.y - change_coo])
elif current_tick - self.shot_end_tick < 270:
self.screen.blit(self.explosion3, [self.x - change_coo, self.y - change_coo])
elif current_tick - self.shot_end_tick < 320:
self.screen.blit(self.explosion4, [self.x - change_coo, self.y - change_coo])
def draw_bullet(self):
copied_bullet = self.copy_rotate(self.image, self.angle - 90)
change_coo_x = copied_bullet.get_rect().center[0]
change_coo_y = copied_bullet.get_rect().center[1]
self.screen.blit(copied_bullet, [self.x - change_coo_x , self.y - change_coo_y ])
def beyond_screen(self):
width = self.screen.get_rect()[2]
height = self.screen.get_rect()[3]
if self.y < 0 or self.y > height or self.x < 0 or self.x > width:
return True
return False
def find_end_coordinates_x(self):
pass
def find_end_coordinates_y(self):
pass
def collide(self, obj_list, my_coordinates):
for obj in obj_list:
# if obj != self:
if obj.point_within_my_area(my_coordinates, obj.cornersList):
return my_coordinates
r = 7
for x in range(r - 1):
my_coordinates[0] += self.add_x / r
my_coordinates[1] -= self.dec_y / r
if obj.point_within_my_area(my_coordinates, obj.cornersList):
return my_coordinates
return False
## preventing skipping corners
# def check_segment_for_collision(self, obj, my_coordinates):
# if obj.point_within_my_area(my_coordinates, obj.cornersList):
# return my_coordinates
# r = 7
# for x in range(r - 1):
# my_coordinates[0] += self.add_x / r
# my_coordinates[1] -= self.dec_y / r
# if obj.point_within_my_area(my_coordinates, obj.cornersList):
# return my_coordinates
# return False
## the game works fine after saving, shooting starts lugging second time you run it
#