Hey guys,
I'm pretty inexperienced with Python, but I wanted to give it a shot because I plan on using it on an upcoming visual aid contest. I've been working on a side scrolling type project where you move left and right and the backgrounds scroll across displaying various images. At this point I'm almost there, but I'm having difficulty getting the backgrounds to scroll in the right order. Sometimes it jumps around, and sometimes it doesn't change at all. I've narrowed it down to a few possible problem areas, but cannot for the life of me figure it out.
I *think* it has something to do with my "self.current_room % len(rooms)" statements, but I am not sure and couldn't figure out how to fix it.
It was at this point that I figured I should defer to someone who knows what they're doing.
Here is my code. I'm using livewires and pygame with python 2.6.2
Thanks for your help! I really appreciate the input!
Cheers,
Jordan
import sys
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 30)
class Jumpman(games.Sprite):
current_room = 0
def update(self):
if games.keyboard.is_pressed(games.K_LEFT):
self.set_image(player_image_left)
self.x -= 8
#moves left/right
if games.keyboard.is_pressed(games.K_RIGHT):
self.set_image(player_image_right)
self.x += 8
#moves left/right
if self.x >= games.screen.width:
self.current_room += 1
self.current_room = self.current_room % len(rooms)
games.screen.background = rooms[self.current_room]
self.load()
#if move off right hand side of screen, add +1 to room counter, and change background accordingly
elif self.x < 0:
self.current_room -= 1
self.current_room = self.current_room % len(rooms)
games.screen.background = rooms[self.current_room]
self.load()
#if move off left hand side of screen, add -1 to room counter, and change background accordingly
def load(self):
#if self.current_room == len(rooms)-1:
#gameinit1()
if self.current_room == 1:
gameinit1()
elif self.current_room == 2:
gameinit2()
elif self.current_room == 3:
gameinit3()
elif self.current_room == 4:
gameinit4()
# adds character (jumpman) object after background change
def gamereset():
for sprite in games.screen.all_objects:
sprite.destroy()
#to reset objects after background change
def gameinit1():
gamereset()
p = Jumpman(player_image_right, x=0, y=480)
games.screen.add(p)
#destroys jumpman object, instantiates new object, adds to screen
def gameinit2():
gamereset()
p = Jumpman(player_image_right, x=0 , y=480)
games.screen.add(p)
def gameinit3():
gamereset()
p = Jumpman(player_image_right, x=0 , y=480)
games.screen.add(p)
def gameinit4():
gamereset()
p = Jumpman(player_image_right, x=0 , y=480)
games.screen.add(p)
bottom = games.screen.height - 25
wall_image = games.load_image("background.gif", transparent = False)
wall_image2 = games.load_image("background2.jpg", transparent = False)
wall_image3 = games.load_image("background3.jpg", transparent = False)
wall_image4 = games.load_image("background4.jpg", transparent = False)
rooms = [wall_image, wall_image2, wall_image3, wall_image4]
games.screen.background = wall_image
games.music.load('backgroundmusic.mp3')
games.music.play(loop=-1)
player_image_right = games.load_image("runright.gif", transparent = False)
player_image_left = games.load_image("runleft.gif", transparent = False)
player_image_jump = games.load_image("jump.gif", transparent = False)
p = Jumpman(player_image_right, x=0 / 2, y=games.screen.height-25)
games.screen.add(p)
games.screen.mainloop()