Reputation: 89
I am creating a game in python with a scrolling background and I can't get my player to move. Just so you know, we are not allowed to use classes or sprites
def game():
running = True
backgrounX = 0
button = 0
movex = 0
movey = 0
player = image.load("images/player.gif")
def drawscene(screen,button,backx):
screen.fill((0, 0 , 0))
background = image.load("images/bc3.png") # Load image
bc = transform.scale(background, (1000, 700)) # Scale the image
screen.blit(bc, [backx, 0]) # To show image
screen.blit(bc, [backx + 1000, 0]) # For background to move
button_1 = Rect(0, 0, 100, 70)
draw.rect(screen, (0, 85, 255), button_1)
draw_text("Exit", font, (0, 0, 0), screen, 25, 25)
for e in event.get():
if e.type == MOUSEBUTTONDOWN:
if e.button == 1:
main_menu()
# Game loop
while running:
for e in event.get():
if e.type == QUIT:
quit()
sys.exit()
running = False
if e.type == KEYDOWN:
if e.key == K_LEFT or e.key == ord('a'):
movex = -1
if e.key == K_RIGHT or e.key == ord('d'):
movex = +1
if e.key == K_UP or e.key == ord('w'):
movey = -1
if e.type == KEYUP:
if e.key == K_LEFT or e.key == ord('a') or e.key == K_RIGHT or e.key == ord('d'):
movex = 0
if e.key == K_UP or e.key == ord('w'):
movey = 0
x += movex
y += movey
drawscene(screen, button, backgrounX)
screen.blit(player (movex, movey))
myClock.tick(60)
backgrounX -= 3 # background scroller speed
display.update()
this is what I have tried so far and an error shows up saying " local variable 'x' referenced before assignment" for line 40. Does anyone know any other way for making my player move?
Upvotes: 2
Views: 63
Reputation: 210890
The position of the player is (x
, y
) rather than (movex
, movey
). Furthermore there is a ',' missing in the argument list:
screen.blit(player (movex, movey))
screen.blit(player, (x, y))
Of course you have to define x
and y
somewhere before the application loop:
x = 0
y = 0
while running:
# [...]
x += movex
y += movey
# [...]
screen.blit(player, (x, y))
Upvotes: 2