So I'm trying to get it to update the score by 30 each time a fly is hit. I've tried this a number of ways, but none of them seem to be working. Here is my code (I've commented out some unneeded things so I didn't have to upload so many files):
Additionally, I am having an issue with the flies spawning on top of each other. What is an easy way to remedy that?
import math, random
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Fly(games.Animation):
images = ["fly1.bmp",
"fly2.bmp"]
speed = 2
def __init__(self, x, y= 20):
super(Fly, self).__init__(
images = Fly.images,
x = x, y = y,
repeat_interval = 4, n_repeats = 0,
dy = 1)
def update(self):
if self.top < 0:
self.dy = -self.dy
if self.top > games.screen.height:
self.top = 0
def handle_hit(self):
if self.overlapping_sprites:
for fly in self.overlapping_sprites:
self.destroy()
new_explosion = Explosion(x = self.x, y = self.y)
games.screen.add(new_explosion)
for i in range(2):
x = random.randrange(games.screen.width)
new_fly = Fly(x = x)
games.screen.add(new_fly)
class Ship(games.Sprite):
image = games.load_image("ship.bmp")
speed = 25
MISSILE_DELAY = 25
def __init__(self):
super(Ship, self).__init__(image = Ship.image,
bottom = games.screen.height)
self.missile_wait = 0
self.score = games.Text(value = 0, size = 25, color = color.red,
top = 19, right = games.screen.width - 30)
games.screen.add(self.score)
def update(self):
if games.keyboard.is_pressed(games.K_LEFT):
self.x -=1
if games.keyboard.is_pressed(games.K_RIGHT):
self.x +=1
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
if self.missile_wait > 0:
self.missile_wait -=1
if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0:
new_Missile = Missile(self.x, self.y, self.angle)
games.screen.add(new_Missile)
self.missile_wait = Ship.MISSILE_DELAY
if self.overlapping_sprites:
for sprite in self.overlapping_sprites:
self.die()
self.check_hit()
def die(self):
self.destroy()
end_message = games.Message(value = "Game Over",
size = 150,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 5 * games.screen.fps,
after_death = games.screen.quit)
games.screen.add(end_message)
def check_hit(self):
for fly in self.overlapping_sprites:
self.score.value += 30
self.score.right = games.screen.width - 30
fly.handle_hit
class Missile(games.Sprite):
image = games.load_image("missile.bmp")
sound = games.load_sound("missile.wav")
BUFFER = 40
VELOCITY_FACTOR = 7
LIFETIME = 60
def __init__(self, ship_x, ship_y, ship_angle):
Missile.sound.play()
angle = ship_angle * math.pi / 180
buffer_x = Missile.BUFFER * math.sin(angle)
buffer_y = Missile.BUFFER * -math.cos(angle)
x = ship_x + buffer_x
y = ship_y + buffer_y
dx = Missile.VELOCITY_FACTOR * math.sin(angle)
dy = Missile.VELOCITY_FACTOR * -math.cos(angle)
super(Missile,self).__init__(image = Missile.image,
x = x, y = y,
dx = dx, dy = dy)
self.lifetime = Missile.LIFETIME
def update(self):
self.lifetime -= 1
if self.lifetime == 0:
self.destroy()
if self.top > games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top = games.screen.height
if self.left > games.screen.width:
self.right = 0
if self.right < 0:
self.left = games.screen.width
if self.overlapping_sprites:
for sprite in self.overlapping_sprites:
sprite.handle_hit()
self.die()
def die(self):
self.destroy()
#class Explosion(games.Animation):
# sound = games.load_sound("explosion.wav")
#images = ["explosion1.bmp",
# "explosion2.bmp",
#"explosion3.bmp",
#"explosion4.bmp",
#"explosion5.bmp",
#"explosion6.bmp",
#"explosion7.bmp",
#"explosion8.bmp",
# "explosion9.bmp"]
#def __init__(self, x, y):
# super(Explosion, self).__init__(images = Explosion.images,
# x = x, y= y,
# repeat_interval = 4, n_repeats = 1,
# is_collideable=False)
#Explosion.sound.play()
def main():
background_image = games.load_image("background.bmp")
games.screen.background = background_image
for i in range(5):
x = random.randrange(games.screen.width)
new_fly = Fly(x = x)
games.screen.add(new_fly)
the_ship = Ship()
games.screen.add(the_ship)
games.screen.mainloop()
main()