/
Ship.py
293 lines (269 loc) · 10.8 KB
/
Ship.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
import pygame, sys, math, random, Arm
from pygame.locals import *
pygame.init()
FPS = 10
turnr = .02
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
def vadd(mag1, mag2, ang1, ang2, magOrAng):
magx = mag1 * math.cos(ang1) + mag2 * math.cos(ang2)
magy = mag1 * math.sin(ang1) + mag2 * math.sin(ang2)
if magOrAng == 'mag':
return int(math.sqrt(magx * magx + magy * magy))
if magOrAng == 'ang':
return math.atan2(magy, magx)
class statistics:
level = int()
name = 'Default'
length = int()
resistance = int()
propulsion = int()
evasion = float()
health = []
armor = []
weapon = []
block = []
color = (255, 0, 0)
def __init__(self):
self.level = 0
self.name = 'Default'
self.length = 100
self.resistance = 10
self.propulsion = 100
self.evasion = .1
self.armor = [50, 50, 50, 50, 50, 50, 50, 50]
self.health = [30, 30, 30, 30, 30, 30, 30, 30]
self.weapon = [[Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()], [Arm.cannon127()]]
self.color = white
self.block = [block(), block(), block(), block(), block(), block(), block(), block()]
for i in range(0, 8):
self.block[i].number = i
self.block[i].init(self)
class block:
state = bool()
number = int()
armor = int()
health = float()
weapon = []
addtime = int()
def __init__(self):
self.state = True
self.number = 0
self.armor = 0
self.health = 0
self.weapon = []
self.addtime = 50
def init(self, sta):
self.armor = sta.armor[self.number]
self.health = sta.health[self.number]
self.weapon = sta.weapon[self.number]
for weapon in self.weapon:
weapon.init(self.number)
weapon.reInit()
def hitten(self, harm, ship):
harmfloat = float()
harmfloat = harm / self.armor
if self.state:
if random.randint(0, 50) == 0 and ship.sta.propulsion > 100:
ship.sta.propulsion -= 1
if self.health > harmfloat:
self.health -= harmfloat
for weapon in self.weapon:
if random.randint(0, 4) == 0:
weapon.harm -= int(weapon.harm * harmfloat / (self.health + harmfloat))
if weapon.harm <= 1:
weapon.harm = 0
else:
self.state = False
def cannonFire(self, shipS, shipE):
for weapon in self.weapon:
if weapon.typ == 'cannon':
weapon.fire(shipS, shipE)
def torpedoFire(self, torlist, shipS):
for weapon in self.weapon:
if weapon.typ == 'torpedo':
weapon.fire(torlist, shipS)
class inputRecord:
left = bool()
right = bool()
up = bool()
down = bool()
cannonFire = bool()
torpedoFire = bool()
sidekickleft = bool()
sidekickright = bool()
def __init__(self):
self.left = False
self.right = False
self.up = False
self.down = False
self.cannonFire = False
self.torpedoFire = False
self.sidekickleft = False
self.sidekickright = False
class inputControl:
left = K_LEFT
right = K_RIGHT
up = K_UP
down = K_DOWN
cannonFire = K_PERIOD
torpedoFire = K_COMMA
sidekickleft = K_k
sidekickright = K_l
def __init__(self):
self.left = K_LEFT
self.right = K_RIGHT
self.up = K_UP
self.down = K_DOWN
self.cannonFire = K_PERIOD
self.torpedoFire = K_COMMA
self.sidekickleft = K_k
self.sidekickright = K_l
class ship:
player = int()
x = int()
y = int()
theta = float()
v = int()
vangel = float()
ax = int()
ay = int()
sta = statistics()
inpR = inputRecord()
inpC = inputControl()
def clear(self):
self.ax = 0
self.ay = 0
self.alpha = 0
def __init__(self):
player = 0
self.x = 5000
self.y = 5000
self.theta = 0
self.v = 0
self.vangel = 0
self.ax = 0
self.ay = 0
self.sta = statistics()
self.inpR = inputRecord()
self.inpC = inputControl()
def reInit(self):
for block in self.sta.block:
block.armor += self.sta.level
block.health += self.sta.level
for weapon in block.weapon:
weapon.harm += self.sta.level
def control(self):
if (self.vangel - self.theta) % (2 * math.pi) <= math.pi / 2 and (self.vangel - self.theta) % (2 * math.pi) > turnr:
self.vangel -= turnr
elif (self.vangel - self.theta) % (2 * math.pi) >= 3 * math.pi / 2 and (self.vangel -self.theta) % (2 * math.pi) < 2 * math.pi - turnr:
self.vangel += turnr
elif (self.vangel - self.theta) % (2 * math.pi) > math.pi / 2 and (self.vangel - self.theta) % (2 * math.pi) < math.pi - turnr:
self.vangel -= turnr
elif (self.vangel - self.theta) % (2 * math.pi) < 3 * math.pi / 2 and (self.vangel -self.theta) % (2 * math.pi) > math.pi + turnr:
self.vangel += turnr
elif (self.vangel - self.theta) % (2 * math.pi) <= turnr or (self.vangel - self.theta) % (2 * math.pi) >= 2 * math.pi - turnr:
self.vangel = self.theta
elif (self.vangel - self.theta) % (2 * math.pi) <= math.pi + turnr and (self.vangel - self.theta) % (2 * math.pi) >= math.pi - turnr:
self.vangel = self.theta + math.pi
vx = int(self.v * math.cos(self.vangel))
vy = int(self.v * math.sin(self.vangel))
self.ax -= int(self.v * self.v * math.cos(self.vangel) / 100) / self.sta.resistance
self.ay -= int(self.v * self.v * math.sin(self.vangel) / 100) / self.sta.resistance
vx += self.ax / FPS
vy += self.ay / FPS
self.x = (self.x + vx / FPS) % 10000
self.y = (self.y + vy / FPS) % 10000
self.v = math.sqrt(vx * vx + vy * vy)
self.vangel = math.atan2(vy, vx)
def left(self):
self.theta -= self.sta.evasion
def right(self):
self.theta += self.sta.evasion
def up(self):
self.ax += int(self.sta.propulsion * math.cos(self.theta))
self.ay += int(self.sta.propulsion * math.sin(self.theta))
def down(self):
self.ax -= int(self.sta.propulsion * math.cos(self.theta) / 10)
self.ay -= int(self.sta.propulsion * math.sin(self.theta) / 10)
def sidekickleft(self):
v = vadd(self.v, self.sta.propulsion / 10, self.vangel, self.theta - (math.pi / 2), 'mag')
vangel = vadd(self.v, self.sta.propulsion / 10, self.vangel, self.theta - (math.pi / 2), 'ang')
self.v = v
self.vangel = vangel
def sidekickright(self):
v = vadd(self.v, self.sta.propulsion / 10, self.vangel, self.theta + (math.pi / 2), 'mag')
vangel = vadd(self.v, self.sta.propulsion / 10, self.vangel, self.theta + (math.pi / 2), 'ang')
self.v = v
self.vangel = vangel
def showSituation(self, DISPLAYSURF, x, y):
for i in range(0, 8):
pygame.draw.rect(DISPLAYSURF, (min(255, self.sta.block[i].health), min(255, self.sta.block[i].health), min(255, self.sta.block[i].health)), (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
if self.sta.block[i].state == False:
pygame.draw.rect(DISPLAYSURF, red, (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
def hitten(self, weapon, x, y):
for block in self.sta.block:
centerx = ((block.number / 4) * 2 - 1) * self.sta.length / 10
centery = (3 - (block.number % 4) * 2) * self.sta.length / 8
d = math.sqrt((centerx - x) * (centerx - x) + (centery - y) * (centery - y))
if d < weapon.harmr:
block.hitten(weapon.harm, self)
if d < weapon.harmr / 2:
block.hitten(weapon.harm, self)
if d < weapon.harmr / 4:
block.hitten(weapon.harm, self)
def cannonFire(self, shipE):
for block in self.sta.block:
if block.state:
block.cannonFire(self, shipE)
def torpedoFire(self, torlist):
for block in self.sta.block:
if block.state:
block.torpedoFire(torlist, self)
def revive(self, DISPLAYSURF, x, y):
for i in range(0, 8):
conditionmaincan = False
existmaincan = 0
if self.sta.block[i].armor < self.sta.armor[i]:
if self.sta.block[i].addtime < 0:
self.sta.block[i].addtime -= 1
else:
self.sta.block[i].addtime = 50
self.sta.block[i].armor += 1
conditioncan = False
existcan = False
conditiontor = False
existtor = False
for weapon in self.sta.block[i].weapon:
if weapon.typ == 'cannon':
existcan = True
existmaincan += 1
if weapon.addtime > 0:
weapon.addtime -= 1
if existmaincan == 1:
pygame.draw.rect(DISPLAYSURF, (255 - weapon.addtime, 255 - weapon.addtime, 255 - weapon.addtime), (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
else:
pygame.draw.rect(DISPLAYSURF, green, (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
if weapon.harm != 0:
conditioncan = True
if existmaincan == 1:
if weapon.addtime == 0:
conditionmaincan = True
if weapon.typ == 'torpedo':
existtor = True
if weapon.addtime > 0:
weapon.addtime -= 1
pygame.draw.rect(DISPLAYSURF, (255 - weapon.addtime, 255 - weapon.addtime, 255 - weapon.addtime), (x + (i / 4) * 50, y + (i % 4) * 50 + 300, 50, 50))
else:
pygame.draw.rect(DISPLAYSURF, green, (x + (i / 4) * 50, y + (i % 4) * 50 + 300, 50, 50))
if weapon.harm != 0:
conditiontor = True
if conditionmaincan:
pygame.draw.rect(DISPLAYSURF, yellow, (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
if (self.sta.block[i].state == False or conditioncan == False) and existcan:
pygame.draw.rect(DISPLAYSURF, red, (x + (i / 4) * 50, y + (i % 4) * 50, 50, 50))
if (self.sta.block[i].state == False or conditiontor == False) and existtor:
pygame.draw.rect(DISPLAYSURF, red, (x + (i / 4) * 50, y + (i % 4) * 50 + 300, 50, 50))