Hi all,
I'm trying to do this challenge from the book I'm following, but I'm so confused. I need to get a variable from one class, and use it as data in another class. As you see in my code, in the Shuttle class, I need to get the score (self.score.value) and put it in the Alien.check_drop() part. I'm going to use an if statement to change to level, so when the user's score reaches 100, I'll increase the Aliens speed at which it moves.
I don't even know where to start.
Please help.
#Asteroid panic
#Player must catch falling asteroids before they hit the ground
from livewires import games, color
import random
games.init(screen_width = 640, screen_height = 480, fps = 150)
class Shuttle(games.Sprite):
"""A shuttle controlled by player to catch falling asteroids"""
image = games.load_image("shuttle.bmp")
def __init__(self, alien):
"""Initialize Shuttle object and create Text object for score"""
super(Shuttle, self).__init__(image = Shuttle.image,
x = games.mouse.x,
bottom = games.screen.height)
self.score = games.Text(value = 0,
size = 25,
color = color.red,
top = 5,
right = games.screen.width - 10)
def update(self):
"""Move to mouse x position"""
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
def check_catch(self):
"""Check if catch asteroids"""
for asteroid in self.overlapping_sprites:
self.score.value += 10
self.score.right = games.screen.width - 10
asteroid.handle_caught()
class Asteroid(games.Sprite):
"""An asteroid which falls to the ground"""
image = games.load_image("asteroid.bmp")
speed = 1
def __init__(self, x, y = 90):
"""Initialize an Asteroid object"""
super(Asteroid, self).__init__(image = Asteroid.image,
x = x, y = y,
dy = Asteroid.speed)
def update(self):
"""Check if botton edge has reached screen bottom"""
if self.bottom > games.screen.height:
self.end_game()
self.destroy()
def handle_caught(self):
"""Destroy self if caught"""
self.destroy()
def end_game(self):
"""End the game"""
end_message = games.Message(value = "Game Over",
size = 90,
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)
class Alien(games.Sprite):
"""An alien which moves left and right, dropping asteroids"""
image = games.load_image("alien.bmp")
def __init__(self, y = 55, speed = 1, odds_change = 200):
"""Initialize the Alien object"""
super(Alien, self).__init__(image = Alien.image,
x = games.screen.width / 2,
y = y,
dx = speed)
self.odds_change = odds_change
self.time_till_drop = 0
self.current_level = 1
def update(self):
"""Determine if direction needs to be reversed"""
if self.left < 0 or self.right > games.screen.width:
self.dx = -self.dx
elif random.randrange(self.odds_change) == 0:
self.dx = -self.dx
self.check_drop()
def check_drop(self):
"""Decrease countdown or drop asteroid and reset countdown"""
if self.time_till_drop > 0:
self.time_till_drop -= 1
else:
new_asteroid = Asteroid(x = self.x)
games.screen.add(new_asteroid)
#set buffer to approx 30% of asteroid height, regardless of asteroid speed
self.time_till_drop = int(new_asteroid.height * 1.3 / Asteroid.speed) + 1
def main():
"""Play the game"""
wall_image = games.load_image("galaxy.jpg", transparent = False)
games.screen.background = wall_image
the_alien = Alien()
games.screen.add(the_alien)
the_shuttle = Shuttle()
games.screen.add(the_shuttle)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()