Hello all,
I created a simple game using the pygame and livewires packages a few years ago on a windows PC. Since then my pc died on me :( and I've switched over to a mac. I decided I wanted to dust off my old game, make some modifications, and post it to a personal site I'm working on. Here are my specs on my mac:
Python Version 2.6
Pygame 1.9.2
Livewires 2.1
However, when I fire up my game using the python launcher or idle I get the following error:
games.init(screen_width = 700, screen_height = 700, fps = 50)
AttributeError: 'module' object has no attribute 'init'
I know this code used to work but there have been many updates for python since I last used it and I'll bet there is different syntax for what I'm trying to do. I've searched for related topics and found 2 individuals with the same error but whose questions didn't get solved. Anyways, here is the code:
# Deep Space
# A ship must shoot hammers to destroy and evil, slime throwing, space duck.
from livewires import games, colour, time
import random, sys
games.init(screen_width = 700, screen_height = 700, fps = 50)
class Spaceship(games.Sprite):
"""
A Spaceship that shoots hammers.
"""
delay = 35
try:
image = games.load_image("spaceship.bmp", transparent = True)
except():
print "Sorry but the Spaceship image could not be loaded."
sys.exit()
def __init__(self):
""" Initialize Ship object and create Text object for score. """
super(Spaceship, self).__init__(image = Spaceship.image,
x = games.mouse.x,
bottom = games.screen.height)
self.last_fire = 0
self.ship_health = games.Text(value = 1, size = 25, color = color.white,
top = 5, right = games.screen.width - 10)
games.screen.add(self.ship_health)
def update(self):
""" Move to mouse x position. """
self.last_fire += 1
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
if games.keyboard.is_pressed(games.K_SPACE) and self.last_fire > self.delay:
self.fire()
self.last_fire = 0
self.check_hit()
def check_hit(self):
""" Check if the ship has been hit. """
for other in self.overlapping_sprites:
if type(other) == Slime:
other.destroy()
if self.ship_health.value > 0:
self.ship_health.value -= 1
self.ship_health.right = games.screen.width - 10
else:
self.you_lose()
def fire(self):
if self.angle == 0: dx = 0; dy = -10
the_hammer = Hammer(x=self.x, y=self.y, dx=dx, dy=dy)
games.screen.add(the_hammer)
def you_lose(self):
""" Loss message + end game. """
end_message = games.Message(value = "Game Over",
size = 90,
color = color.white,
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 Hit(games.Sprite):
try:
hit_image = games.load_image("explosion.png", transparent = True)
except():
print "Sorry but the Hit image could not be loaded."
sys.exit()
def __init__(self, x, y):
super(Hit, self).__init__(Hit.hit_image, x=x, y=y)
self.life = 0
def update(self):
self.life += 1
if self.life > 5:
self.destroy()
class Hammer(games.Sprite):
try:
hammer_image = games.load_image("hammer.jpg", transparent = True)
except():
print "Sorry but the Hammer image could not be loaded."
sys.exit()
try:
impact_sound = games.load_sound('explosion.wav')
except():
print "Sorry but the Sound Effect could not be loaded."
sys.exit()
def __init__(self, x, y, dx, dy):
super(Hammer, self).__init__(Hammer.hammer_image, x=x, y=y, dx=dx, dy=dy)
def update(self):
if self.y < 0:
self.destroy()
self.check_hit()
def impact(self):
Hammer.impact_sound.play()
visual = Hit(x=self.x, y=self.y)
games.screen.add(visual)
self.destroy()
def check_hit(self):
""" Check if the duck has been hit. """
for other in self.overlapping_sprites:
if type(other) == Slime:
self.destroy()
self.impact()
elif type(other) == Duck:
self.destroy()
self.impact()
class Slime(games.Sprite):
"""
Slime projectile moving toward the spaceship.
"""
try:
image = games.load_image("slime.bmp", transparent = True)
except:
print "Sorry but the Slime image could not be loaded."
sys.exit()
speed = 1
def __init__(self, x, y = 90):
super(Slime, self).__init__(image = Slime.image,
x = x, y = y,
dy = Slime.speed)
def update(self):
""" Check if bottom edge has reached screen bottom. """
if self.bottom > games.screen.height:
self.destroy()
class Duck(games.Sprite):
"""
An evil space duck throwing slime.
"""
try:
image = games.load_image("evil_space_duck.bmp", transparent = True)
except():
print "Sorry but the Duck image could not be loaded."
sys.exit()
def __init__(self, y = 55, speed = 3, odds_change = 200):
super(Duck, self).__init__(image = Duck.image,
x = games.screen.width / 2,
y = y,
dx = speed)
self.odds_change = odds_change
self.time_til_drop = 0
self.duck_health = games.Text(value = 10, size = 25, color = color.white,
top = 5, left = games.screen.width - 690)
games.screen.add(self.duck_health)
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()
self.check_hit()
def check_hit(self):
""" Check if the duck has been hit. """
for other in self.overlapping_sprites:
if type(other) == Hammer:
other.destroy()
if self.duck_health.value > 0:
self.duck_health.value -= 1
self.duck_health.left = games.screen.width - 690
else:
self.you_win()
def you_win(self):
""" Win message + end game. """
end_message = games.Message(value = "You Win",
size = 90,
color = color.white,
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_drop(self):
""" Decrease time or shoot slime. """
if self.time_til_drop > 0:
self.time_til_drop -= 1
else:
new_slime = Slime(x = self.x)
games.screen.add(new_slime)
self.time_til_drop = int(new_slime.height * 1.3 / Slime.speed) + 1
def main():
""" Display Instructions, then Play the Game """
splash_image = games.load_image("metal_wall.bmp", transparent = False)
games.screen.background = splash_image
time.sleep(10)
games.music.load('BlueFunk.mp3')
games.music.play(loop=-1)
wall_image = games.load_image("space.jpg", transparent = False)
games.screen.background = wall_image
the_duck = Duck()
games.screen.add(the_duck)
the_ship = Spaceship()
games.screen.add(the_ship)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
try:
main()
except(SystemExit):
print "Exiting..."
raw_input("\n\nPress the enter key to exit.")
That is the game in its entirety. I don't think you need that much information but I just wanted to make sure I've provided everything I can to troubleshoot this error.
Thanks in advance for any help that's thrown my way!