This repository was archived by the owner on Jul 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactors.py
More file actions
186 lines (148 loc) · 5.27 KB
/
actors.py
File metadata and controls
186 lines (148 loc) · 5.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
import os
import math
import pygame
from lava.primitives import *
from lava.media import MediaManager
mediaman = MediaManager()
class Star(GLSprite):
def __init__(self, world, startx, starty):
GLSprite.__init__(self)
self.gravityEffect = world.g_collision
self.can_die = False
self.to_hit = 32000
self.load_texture(mediaman.load_image('sun.png', 'images'))
self.width = self.height = 50
self.radius = 19
self.x = startx
self.y = starty
self.vx = 0.0
self.vy = 0.0
self.facing = 0.0
def update(self, interval):
# Divide by 4 to fake a larger mass
self.x += (self.vx / 4) * interval
self.y += (self.vy / 4) * interval
def gravitate(self, interval):
for sprite in self.gravityEffect.iterate_sprites():
rad = self.radius * self.scale
# Get vector: from self to sprite
vector = (sprite.x - self.x, sprite.y - self.y)
# Find radian angle of vector, reverse direction
angle = math.atan2(vector[1], vector[0]) - math.pi
# Get distance, adjust for self's radius
distance = math.sqrt(vector[0] ** 2 + vector[1] ** 2) - rad
if distance > 0:
r = distance / 200 + 1
# Gravity calculation
force = 0.15 / 1000 / (r ** 2) * interval
else:
force = 0.0
(gx, gy) = sincos(angle, force)
sprite.vx += gx
sprite.vy += gy
class Ship(GLSprite):
def __init__(self, world, name = "Unknown", image = 'ship.png'):
GLSprite.__init__(self)
self.can_die = True
self.world = world
self.name = name
self.load_texture(mediaman.load_image(image, 'images'))
self.width = self.height = 40
self.radius = 18
# Starting position and orientation
self.facing = 0.0
self.x = 0.0
self.y = 0.0
self.vx = 0.0
self.vy = 0.0
# Movement attributes
self.accel = 0.15 / 1000.0
self.maxspeed = 0.3
self.turn_rate = 0.2
self.turn = 0
# Ship attributes
self.life = 3
self.gun_ready = True
self.fire_rate = 400
self.last_fired = 0
self.score = 0
def update(self, interval):
self.x += self.vx * interval
self.y += self.vy * interval
self.facing += self.turn * interval
self.facing %= 360
if not self.gun_ready:
ticks = pygame.time.get_ticks()
if ticks - self.last_fired > self.fire_rate:
self.gun_ready = True
def fire(self):
if not self.gun_ready:
return None
if not self.alive():
return None
self.last_fired = pygame.time.get_ticks()
self.gun_ready = False
Torpedo(self)
def thrust(self, interval):
radian = math.radians(self.facing + 90)
(vx, vy) = sincos(radian, self.accel)
self.vx += vx * interval
self.vy += vy * interval
current_speed = math.sqrt(self.vx ** 2 + self.vy ** 2)
if current_speed > self.maxspeed:
multiple = self.maxspeed / current_speed
self.vx *= multiple
self.vy *= multiple
def reverse_thrust(self, interval):
radian = math.radians(self.facing + 90)
(vx, vy) = sincos(radian, (self.accel / 2))
self.vx -= vx * interval
self.vy -= vy * interval
current_speed = math.sqrt(self.vx ** 2 + self.vy ** 2)
if current_speed > self.maxspeed:
multiple = self.maxspeed / current_speed
self.vx *= multiple
self.vy *= multiple
def rotate(self, multiplier):
self.turn = self.turn_rate * multiplier
def take_damage(self, attacker):
self.life -= attacker.to_hit
ParticleEmitter(self, 3)
if self.life <= 0:
self.kill()
def kill(self):
if self.alive():
ParticleEmitter(self, 10)
GLSprite.kill(self)
class Torpedo(GLSprite):
def __init__(self, parent):
GLSprite.__init__(self)
self.parent = parent
self.name = "torpedo"
self.can_die = True
self.to_hit = 1
self.load_texture(mediaman.load_image('torpedo.png', 'images'))
self.width = self.height = 6
self.radius = 4
# Starting position
self.facing = parent.facing
angle = math.radians(self.facing + 90)
distance = parent.radius * parent.scale + self.radius * self.scale
(sx, sy) = sincos(angle, distance + 1)
self.x = parent.x + sx
self.y = parent.y + sy
# Velocity and TTL
speed = 0.3
self.life = 1500
(tx, ty) = sincos(angle, speed)
self.vx = parent.vx + tx
self.vy = parent.vy + ty
# Add to world
parent.world.g_render.add(self)
parent.world.g_collision.add(self)
def update(self, interval):
self.x += self.vx * interval
self.y += self.vy * interval
self.life -= interval
if self.life <= 0:
self.kill()