# Galaga.py
import codesters
e = codesters.Environment()
e.disable_all_walls()
class Bullet:
shape = None
def __init__(self, x, y):
self.shape = codesters.Circle(x, y, 5, 'blue')
def check_offscreen(self):
if (self.shape.y() > 230 or self.shape.y() < -230):
e.remove_sprite(self.shape)
return True
return False
class Player:
ship = None
speed = 10
bullets = []
def __init__(self):
self.ship = codesters.TriangleIso(0,-150, 20,40,'tomato')
def left(self):
self.ship.set_x(self.ship.x() - self.speed)
def right(self):
self.ship.set_x(self.ship.x() + self.speed)
def up(self):
self.ship.set_y(self.ship.y() + self.speed)
def down(self):
self.ship.set_y(self.ship.y() - self.speed)
def shoot(self):
b = Bullet(self.ship.x(), self.ship.y())
b.shape.set_vely(5)
self.bullets.append(b)
def check_bullets(self):
for b in self.bullets:
if(b.check_offscreen()):
self.bullets.remove(b)
class Enemy:
ship = None
bullets = []
speed = 50
movement_amount = -50
def __init__(self, start_x, start_y):
self.ship = codesters.TriangleIso(start_x,start_y, 20,40,'grey')
def move(self):
if self.ship.x() < -220:
self.ship.set_y(self.ship.y() - 50)
movement_amount = -1 * self.speed
elif self.ship.x() > 220:
self.ship.set_y(self.ship.y() - 50)
movement_amount = -1 * self.speed
self.ship.set_x(self.ship.x() + self.movement_amount)
def shoot(self):
b = Bullet(self.ship.x(), self.ship.y())
b.shape.set_vely(-5)
self.bullets.append(b)
def check_bullets(self):
for b in self.bullets:
if(b.check_offscreen()):
self.bullets.remove(b)
s = Player()
enemies = []
# Generate Enemies
columns = 6
spacing_x = 30
for c in range(columns):
en = Enemy(200 - c * spacing_x, 200)
enemies.append(en)
counter = 0
def forever():
global counter
if counter > 30:
for en in enemies:
en.move()
en.shoot()
counter = 0
else:
counter += 1
s.check_bullets()
for en in enemies:
en.check_bullets()
e.event_forever(forever)
def left():
s.left()
e.event_left_key(left)
def right():
s.right()
e.event_right_key(right)
def up():
s.up()
e.event_up_key(up)
def down():
s.down()
e.event_down_key(down)
def space():
s.shoot()
e.event_space_key(space)
-
Run Code
-
-
Stop Running Code
-
Show Chart
-
Show Console
-
Codesters How To (opens in a new tab)