Skip to content

Commit 4ce0696

Browse files
author
Luis Lpz
authored
Add files via upload
1 parent 3f4de05 commit 4ce0696

File tree

11 files changed

+659
-2
lines changed

11 files changed

+659
-2
lines changed

Assets/hit.wav

60.5 KB
Binary file not shown.

Assets/laser_shot.wav

18.6 KB
Binary file not shown.

Assets/song.mp3

3.45 MB
Binary file not shown.

Circle.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
class Circle:
3+
4+
def __init__(self, center, radius):
5+
6+
self.center = center
7+
self.radius = radius
8+
9+
# takes in another circle
10+
def overlaps(self, other):
11+
12+
distance = (self.center - other.center).magnitude()
13+
14+
sum_radii = self.radius + other.radius
15+
16+
return distance <= sum_radii

Circle.pyc

846 Bytes
Binary file not shown.

Entity.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
2+
from pygame import draw
3+
from Vector2 import Vector2
4+
from Circle import Circle
5+
import random
6+
import math
7+
import weakref
8+
9+
10+
# This class describes game objects in our game
11+
class Entity(object):
12+
13+
def __init__(self):
14+
15+
# R G B
16+
# default color is white
17+
self.color = (255, 255, 255)
18+
19+
self.graphicsBounds = Circle(Vector2(0.0, 0.0), 1)
20+
21+
self.collider = Circle(Vector2(0.0, 0.0), 1)
22+
23+
self.position = Vector2(0.0, 0.0)
24+
self.velocity = Vector2(0.0, 0.0)
25+
26+
self.acceleration = Vector2(0.0, 0.0)
27+
28+
self.max_speed = 400
29+
30+
def update(self, dt):
31+
32+
self.velocity += self.acceleration * dt
33+
self.position += self.velocity * dt
34+
35+
# cap speed
36+
if self.velocity.magnitude() >= self.max_speed:
37+
self.velocity.set_magnitude(self.max_speed)
38+
39+
# Update graphical and physical bounds
40+
self.graphicsBounds.center = self.position
41+
self.collider.center = self.position
42+
43+
def render(self, screen):
44+
45+
center = self.graphicsBounds.center
46+
radius = self.graphicsBounds.radius
47+
48+
# Pygame needs a 2-tuple of integers as the center of a circle
49+
center_int = (int(center.x), int(center.y))
50+
51+
draw.circle(screen, self.color, center_int, radius)
52+
53+
54+
class Bullet(Entity):
55+
56+
def __init__(self, range, position, velocity):
57+
58+
# Construct parent so we have all of the Entities properties
59+
# like position, velocity...
60+
super(Bullet, self).__init__()
61+
62+
# How far the bullet travels
63+
self.range = range
64+
65+
self.position = Vector2(position.x, position.y)
66+
self.velocity = Vector2(velocity.x, velocity.y)
67+
68+
# The time needed to travel a certain range
69+
self.life = range / self.velocity.magnitude()
70+
71+
def update(self, dt):
72+
73+
# Call parent update so we can still move
74+
super(Bullet, self).update(dt)
75+
76+
# Kill the bullet slowly
77+
self.life -= dt
78+
79+
80+
class Asteroid(Entity):
81+
82+
def __init__(self, target, homing_speed):
83+
84+
super(Asteroid, self).__init__()
85+
86+
self.target = weakref.ref(target)
87+
88+
self.homing_speed = homing_speed
89+
90+
def update(self, dt):
91+
92+
super(Asteroid, self).update(dt)
93+
94+
self.follow()
95+
96+
def follow(self):
97+
98+
if self.target() is None:
99+
return
100+
101+
to_target = Vector2.get_normal(self.target().position - self.position)
102+
103+
self.acceleration = to_target * self.homing_speed
104+
105+
106+
class Particle(Entity):
107+
108+
def __init__(self, life, position, velocity):
109+
110+
super(Particle, self).__init__()
111+
112+
self.life = life
113+
self.position = Vector2(position.x, position.y)
114+
self.velocity = Vector2(velocity.x, velocity.y)
115+
116+
self.active = False
117+
118+
def update(self, dt):
119+
120+
if not self.active:
121+
return
122+
123+
super(Particle, self).update(dt)
124+
125+
self.life -= dt
126+
127+
if self.life <= 0:
128+
self.active = False
129+
130+
def render(self, screen):
131+
132+
if self.active:
133+
super(Particle, self).render(screen)
134+
135+
136+
class BurstEmitter(Entity):
137+
138+
def __init__(self, particle_count, max_life):
139+
140+
self.particles = list()
141+
142+
for i in xrange(0, particle_count):
143+
self.particles.append(Particle(1, Vector2(0, 0), Vector2(0, 0)))
144+
145+
self.life = max_life
146+
147+
def update(self, dt):
148+
149+
for p in self.particles:
150+
p.update(dt)
151+
152+
self.life -= dt
153+
154+
def render(self, screen):
155+
156+
for p in self.particles:
157+
p.render(screen)
158+
159+
def emit(self, position):
160+
161+
for p in self.particles:
162+
163+
p.active = True
164+
165+
p.position = Vector2(position.x, position.y)
166+
167+
p.velocity = Vector2(1, 0)
168+
p.velocity.set_direction(random.uniform(0, 2 * math.pi))
169+
p.velocity.set_magnitude(random.randint(200, 500))
170+
171+
size = random.randint(2, 8)
172+
p.graphicsBounds.radius = size
173+
174+
p.life = random.uniform(0.2, 2.5)
175+
176+
p.color = (0, 255, 0)
177+
178+
def is_done(self):
179+
return self.life <= 0
180+
181+
class BurstEmitter2(Entity):
182+
183+
def __init__(self, particle_count, max_life):
184+
185+
self.particles = list()
186+
187+
for i in xrange(0, particle_count):
188+
self.particles.append(Particle(1, Vector2(0, 0), Vector2(0, 0)))
189+
190+
self.life = max_life
191+
192+
def update(self, dt):
193+
194+
for p in self.particles:
195+
p.update(dt)
196+
197+
self.life -= dt
198+
199+
def render(self, screen):
200+
201+
for p in self.particles:
202+
p.render(screen)
203+
204+
def emit(self, position):
205+
206+
for p in self.particles:
207+
208+
p.active = True
209+
210+
p.position = Vector2(position.x, position.y)
211+
212+
p.velocity = Vector2(1, 0)
213+
p.velocity.set_direction(random.uniform(0, 1 * math.pi))
214+
p.velocity.set_magnitude(random.randint(50, 100))
215+
216+
size = random.randint(2, 8)
217+
p.graphicsBounds.radius = size
218+
219+
p.life = random.uniform(0.2, 2.5)
220+
221+
p.color = (0, 255, 255)
222+
223+
def is_done(self):
224+
return self.life <= 0

Entity.pyc

7.75 KB
Binary file not shown.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# LovelacesAsteroids
2-
Team lovelace asteroid modification
1+
# HomingAsteroids
2+
Tutorial game for ACM game dev

Vector2.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
from math import sqrt
3+
from math import acos
4+
from math import sin
5+
from math import cos
6+
7+
8+
__author__ = 'unit978'
9+
10+
11+
# Two dimensional vector that supports the basic operations
12+
# such addition of vectors, scalar multiplication, dot product,
13+
# and normalization
14+
class Vector2:
15+
16+
def __init__(self, x=0.0, y=0.0):
17+
self.x = x
18+
self.y = y
19+
20+
def __add__(self, other):
21+
return Vector2(self.x + other.x, self.y + other.y)
22+
23+
def __iadd__(self, other):
24+
self.x += other.x
25+
self.y += other.y
26+
return self
27+
28+
def __sub__(self, other):
29+
return Vector2(self.x - other.x, self.y - other.y)
30+
31+
def __isub__(self, other):
32+
self.x -= other.x
33+
self.y -= other.y
34+
return self
35+
36+
def __mul__(self, scale):
37+
return Vector2(self.x * scale, self.y * scale)
38+
39+
def __rmul__(self, scale):
40+
return self.__mul__(scale)
41+
42+
def __div__(self, scale):
43+
return self.__mul__(1/scale)
44+
45+
def scale_by(self, s):
46+
self.x *= s
47+
self.y *= s
48+
49+
# Return copy of a scaled version of the vector
50+
@staticmethod
51+
def get_scaled_by(vector2, s):
52+
return Vector2(vector2.x*s, vector2.y*s)
53+
54+
def normalize(self):
55+
m = self.magnitude()
56+
57+
if m == 0:
58+
self.x = 0
59+
self.y = 0
60+
61+
else:
62+
self.x /= m
63+
self.y /= m
64+
65+
# Return a copy of the normalized vector
66+
@staticmethod
67+
def get_normal(vector2):
68+
v = Vector2(vector2.x, vector2.y)
69+
v.normalize()
70+
71+
return v
72+
73+
def magnitude(self):
74+
return sqrt(self.x*self.x + self.y*self.y)
75+
76+
# Squared magnitude
77+
def sq_magnitude(self):
78+
return self.x*self.x + self.y*self.y
79+
80+
def set_magnitude(self, mag):
81+
# Use properties of similar triangles
82+
m = self.magnitude()
83+
if m != 0:
84+
ratio = mag / m
85+
self.x *= ratio
86+
self.y *= ratio
87+
else:
88+
self.x = 0.0
89+
self.y = 0.0
90+
91+
# Uses the angle between i-unit vector and x-y vector component
92+
def direction(self):
93+
r = acos(self.x / self.magnitude())
94+
if self.y < 0:
95+
r *= -1
96+
return r
97+
98+
# direction must be in radians
99+
def set_direction(self, direction):
100+
m = self.magnitude()
101+
self.x = m * cos(direction)
102+
self.y = m * sin(direction)
103+
104+
# y_temp = m * sin(direction)
105+
# self.y = -y_temp if self.y > 0 else y_temp
106+
107+
# Dot product of two vectors
108+
def dot(self, other):
109+
return self.x*other.x + self.y*other.y
110+
111+
# Return angle between vectors a and b
112+
@staticmethod
113+
def angle(vector_a, vector_b):
114+
n = vector_a.dot(vector_b)
115+
d = vector_a.magnitude() * vector_b.magnitude()
116+
return acos(n / d)
117+
118+
def zero(self):
119+
self.x = 0
120+
self.y = 0
121+
122+
def is_zero(self):
123+
return self.x == 0 and self.y == 0
124+
125+
def __str__(self):
126+
return "<" + str(self.x) + ", " + str(self.y) + ">"
127+
128+
def to_tuple(self):
129+
return self.x, self.y

Vector2.pyc

5.61 KB
Binary file not shown.

0 commit comments

Comments
 (0)