Silverhawk11011
Still Fresh
- Joined
- Jul 27, 2011
- Messages
- 11
I would first like to say hello to everyone in the community, this is my first time here, and I like what ive been reading, there are some very bright people here. I am a programming student and currently taking game programming and working on a game assignment. The assignment is to add sounds to the game (.ogg - done) and play sounds on colission of sprites (done), create an additional sprite to get bonus points (first problem), and randomize the size/scale of the sprtes that come onto the screen. So as of right now I have tried to add a new sprite called Treasure. I have the treasure.gif file saved in the correct location (along with the other .gif files), and I thought I coded properly. However I am getting this error upon starting the game (the game opens to the main intro screen) When I click to start playing I get an error. I hope someone is able to help .
--- File "C:\Users\Steve\workspace\Assignment2\src\mailPilot.py", line 35, in __init__
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\treasure.gif")
pygame.error: Couldn't open I:\School Work\Term 5\Game Programming\Project 2\Images reasure.gif
Below in green are all areas with the new code for the treasure sprite, if I remove all instances of treasure the game runs fine.
I thought it may have been a typo but I could not find one at all. Here is the code below :
"""
mail pilot - Project 2
"""
import pygame, random
pygame.init()
screen = pygame.display.set_mode((640, 480))
class Plane(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\plane.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
if not pygame.mixer:
print "problem with sound"
else:
pygame.mixer.init()
self.sndCoins = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\coins.ogg")
self.sndExplosion = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\explosion.ogg")
self.sndEngine = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\engine.ogg")
self.sndEngine.play(-1)
def update(self):
mousex, mousey = pygame.mouse.get_pos()
self.rect.center = (mousex, 430)
"""This is the new sprite added in"""
class Treasure(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\treasure.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
self.dy = 5
def update(self):
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.top = 0
self.rect.centerx = random.randrange(0, screen.get_width())
class Island(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\island.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
self.dy = 5
def update(self):
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.top = 0
self.rect.centerx = random.randrange(0, screen.get_width())
class Cloud(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\Cloud.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
def update(self):
self.rect.centerx += self.dx
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.bottom = 0
self.rect.centerx = random.randrange(0, screen.get_width())
self.dy = random.randrange(3, 8)
self.dx = random.randrange(-2, 2)
class Ocean(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\ocean.gif")
self.rect = self.image.get_rect()
self.dy = 5
self.reset()
def update(self):
self.rect.bottom += self.dy
if self.rect.bottom >= 1440:
self.reset()
def reset(self):
self.rect.top = -960
class Scoreboard(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.lives = 5
self.score = 0
self.font = pygame.font.SysFont("None", 50)
def update(self):
self.text = "planes: %d, score: %d" % (self.lives, self.score)
self.image = self.font.render(self.text, 1, (255, 255, 0))
self.rect = self.image.get_rect()
def game():
pygame.display.set_caption("Mail Pilot!")
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
plane = Plane()
"""defining the variable"""
treasure = Treasure()
island = Island()
cloud1 = Cloud()
cloud2 = Cloud()
cloud3 = Cloud()
ocean = Ocean()
scoreboard = Scoreboard()
"""defining the sprite groups"""
friendSprites = pygame.sprite.Group(ocean, treasure, island, plane)
cloudSprites = pygame.sprite.Group(cloud1, cloud2, cloud3)
scoreSprite = pygame.sprite.Group(scoreboard)
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
pygame.mouse.set_visible(False)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
#check collisions
"""defining collision with new sprite"""
if plane.rect.colliderect(treasure.rect):
plane.sndCoins.play()
treasure.reset()
scoreboard.score += 500
if plane.rect.colliderect(island.rect):
plane.sndCoins.play()
island.reset()
scoreboard.score += 100
hitClouds = pygame.sprite.spritecollide(plane, cloudSprites, False)
if hitClouds:
plane.sndExplosion.play()
scoreboard.lives -= 1
if scoreboard.lives <= 0:
keepGoing = False
for theCloud in hitClouds:
theCloud.reset()
friendSprites.update()
cloudSprites.update()
scoreSprite.update()
friendSprites.draw(screen)
cloudSprites.draw(screen)
scoreSprite.draw(screen)
pygame.display.flip()
plane.sndEngine.stop()
#return mouse cursor
pygame.mouse.set_visible(True)
return scoreboard.score
def instructions(score):
pygame.display.set_caption("Mail Pilot!")
plane = Plane()
ocean = Ocean()
allSprites = pygame.sprite.Group(ocean, plane)
insFont = pygame.font.SysFont(None, 50)
insLabels = []
instructions = (
"Mail Pilot. Last score: %d" % score ,
"Instructions: You are a mail pilot,",
"delivering mail to the islands.",
"",
"Fly over an island to drop the mail,",
"but be careful not to fly too close",
"to the clouds. Your plane will fall ",
"apart if it is hit by lightning too",
"many times. You can also collect",
"treasure chest to gain a nice bonus.",
"Steer with the mouse.",
"",
"good luck!",
"",
"click to start, escape to quit..."
)
for line in instructions:
tempLabel = insFont.render(line, 1, (255, 255, 0))
insLabels.append(tempLabel)
keepGoing = True
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
donePlaying = True
if event.type == pygame.MOUSEBUTTONDOWN:
keepGoing = False
donePlaying = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
keepGoing = False
donePlaying = True
allSprites.update()
allSprites.draw(screen)
for i in range(len(insLabels)):
screen.blit(insLabels, (50, 30*i))
pygame.display.flip()
plane.sndEngine.stop()
pygame.mouse.set_visible(True)
return donePlaying
def main():
donePlaying = False
score = 0
while not donePlaying:
donePlaying = instructions(score)
if not donePlaying:
score = game()
if __name__ == "__main__":
main()
The second problem is randomizing the size of the sprites, I cant seem to find anything in my text on doing this. The only thing I could think of is creating multiple .gif files with different sizes; However my Prof informed me that this would not count .
I really hope someone has come across this problem before and could help. Thank you in advance to anyone who is taking the time to read this.
--- File "C:\Users\Steve\workspace\Assignment2\src\mailPilot.py", line 35, in __init__
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\treasure.gif")
pygame.error: Couldn't open I:\School Work\Term 5\Game Programming\Project 2\Images reasure.gif
Below in green are all areas with the new code for the treasure sprite, if I remove all instances of treasure the game runs fine.
I thought it may have been a typo but I could not find one at all. Here is the code below :
"""
mail pilot - Project 2
"""
import pygame, random
pygame.init()
screen = pygame.display.set_mode((640, 480))
class Plane(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\plane.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
if not pygame.mixer:
print "problem with sound"
else:
pygame.mixer.init()
self.sndCoins = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\coins.ogg")
self.sndExplosion = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\explosion.ogg")
self.sndEngine = pygame.mixer.Sound("I:\School Work\Term 5\Game Programming\Project 2\Sounds\engine.ogg")
self.sndEngine.play(-1)
def update(self):
mousex, mousey = pygame.mouse.get_pos()
self.rect.center = (mousex, 430)
"""This is the new sprite added in"""
class Treasure(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\treasure.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
self.dy = 5
def update(self):
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.top = 0
self.rect.centerx = random.randrange(0, screen.get_width())
class Island(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\island.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
self.dy = 5
def update(self):
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.top = 0
self.rect.centerx = random.randrange(0, screen.get_width())
class Cloud(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\Cloud.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.reset()
def update(self):
self.rect.centerx += self.dx
self.rect.centery += self.dy
if self.rect.top > screen.get_height():
self.reset()
def reset(self):
self.rect.bottom = 0
self.rect.centerx = random.randrange(0, screen.get_width())
self.dy = random.randrange(3, 8)
self.dx = random.randrange(-2, 2)
class Ocean(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("I:\School Work\Term 5\Game Programming\Project 2\Images\ocean.gif")
self.rect = self.image.get_rect()
self.dy = 5
self.reset()
def update(self):
self.rect.bottom += self.dy
if self.rect.bottom >= 1440:
self.reset()
def reset(self):
self.rect.top = -960
class Scoreboard(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.lives = 5
self.score = 0
self.font = pygame.font.SysFont("None", 50)
def update(self):
self.text = "planes: %d, score: %d" % (self.lives, self.score)
self.image = self.font.render(self.text, 1, (255, 255, 0))
self.rect = self.image.get_rect()
def game():
pygame.display.set_caption("Mail Pilot!")
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
plane = Plane()
"""defining the variable"""
treasure = Treasure()
island = Island()
cloud1 = Cloud()
cloud2 = Cloud()
cloud3 = Cloud()
ocean = Ocean()
scoreboard = Scoreboard()
"""defining the sprite groups"""
friendSprites = pygame.sprite.Group(ocean, treasure, island, plane)
cloudSprites = pygame.sprite.Group(cloud1, cloud2, cloud3)
scoreSprite = pygame.sprite.Group(scoreboard)
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
pygame.mouse.set_visible(False)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
#check collisions
"""defining collision with new sprite"""
if plane.rect.colliderect(treasure.rect):
plane.sndCoins.play()
treasure.reset()
scoreboard.score += 500
if plane.rect.colliderect(island.rect):
plane.sndCoins.play()
island.reset()
scoreboard.score += 100
hitClouds = pygame.sprite.spritecollide(plane, cloudSprites, False)
if hitClouds:
plane.sndExplosion.play()
scoreboard.lives -= 1
if scoreboard.lives <= 0:
keepGoing = False
for theCloud in hitClouds:
theCloud.reset()
friendSprites.update()
cloudSprites.update()
scoreSprite.update()
friendSprites.draw(screen)
cloudSprites.draw(screen)
scoreSprite.draw(screen)
pygame.display.flip()
plane.sndEngine.stop()
#return mouse cursor
pygame.mouse.set_visible(True)
return scoreboard.score
def instructions(score):
pygame.display.set_caption("Mail Pilot!")
plane = Plane()
ocean = Ocean()
allSprites = pygame.sprite.Group(ocean, plane)
insFont = pygame.font.SysFont(None, 50)
insLabels = []
instructions = (
"Mail Pilot. Last score: %d" % score ,
"Instructions: You are a mail pilot,",
"delivering mail to the islands.",
"",
"Fly over an island to drop the mail,",
"but be careful not to fly too close",
"to the clouds. Your plane will fall ",
"apart if it is hit by lightning too",
"many times. You can also collect",
"treasure chest to gain a nice bonus.",
"Steer with the mouse.",
"",
"good luck!",
"",
"click to start, escape to quit..."
)
for line in instructions:
tempLabel = insFont.render(line, 1, (255, 255, 0))
insLabels.append(tempLabel)
keepGoing = True
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
donePlaying = True
if event.type == pygame.MOUSEBUTTONDOWN:
keepGoing = False
donePlaying = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
keepGoing = False
donePlaying = True
allSprites.update()
allSprites.draw(screen)
for i in range(len(insLabels)):
screen.blit(insLabels, (50, 30*i))
pygame.display.flip()
plane.sndEngine.stop()
pygame.mouse.set_visible(True)
return donePlaying
def main():
donePlaying = False
score = 0
while not donePlaying:
donePlaying = instructions(score)
if not donePlaying:
score = game()
if __name__ == "__main__":
main()
The second problem is randomizing the size of the sprites, I cant seem to find anything in my text on doing this. The only thing I could think of is creating multiple .gif files with different sizes; However my Prof informed me that this would not count .
I really hope someone has come across this problem before and could help. Thank you in advance to anyone who is taking the time to read this.