1

I'm still new to python, and have started with pygame. I'm making an endless runner game, but I've run into a problem with my run cycle. Here's my code:

import pygame
import sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
clock2 = pygame.time.get_ticks()
screen = pygame.display.set_mode((640, 575))
bgx = 0
bgx2 = -800
scroll = 10
pose = 1
background = pygame.image.load("images/background2.png").convert_alpha();
player = pygame.image.load("images/character1.png").convert_alpha();
screen.blit(background, (0, 0))
def draw_screen(x):
screen.blit(background, (x, 0))
def draw_screen2(x2):
screen.blit(background, (x2, 0))
def draw_player(pose):
    if pose == 1:
        player = pygame.image.load("images/character1.png").convert_alpha();
        screen.blit(player, (0, 0))
    elif pose == 2:
        player = pygame.image.load("images/character2.png").convert_alpha();
        screen.blit(player, (0, 0))
    elif pose == 3:
        player = pygame.image.load("images/character3.png").convert_alpha();
        screen.blit(player, (0, 0))
    elif pose == 4:
        player = pygame.image.load("images/character2.png").convert_alpha();
        screen.blit(player, (0, 0))
def set_poses(pose):
    if pose == 1:
        pose = 2
    if pose == 2:
        pose = 3
    if pose == 3:
        pose = 4
    if pose == 4:
        pose = 1

while True: #Loop
    clock2 = pygame.time.get_ticks()
    #Quitting Function
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            quit()
    clock.tick(50)
    #Screen Scrolling
    if bgx != 800:
        bgx += scroll
    else:
        bgx = 0
    if bgx2 != 0:
        bgx2 += scroll
    else:
        bgx2 = -800
    #Drawing Items
    draw_screen2(bgx2)
    draw_screen(bgx)
    draw_player(pose)
    if clock2%6 == 0:
        set_poses(pose)
    pygame.display.update()

So the screen is scrolling, and my character appears but he stays in his first position (Frame I guess.) at the part where it runs set_poses(pose) I tried having it also print something right before setting the pose, which it did, but its not setting the pose. I tried having it printing the pose, and it showed that the poses wasn't changing. So, yea, I've found my problem, but I can't for the life of me find a solution.

1 Answers1

1

Rather than trying to pass by reference - which is what you're doing, try something like this:

def set_pose(pose):
    if pose >= 4:
        return 1
    else:
        return pose + 1

Then when you're trying to update the pose variable elsewhere:

pose = set_pose(pose)

Would you agree that you can call the arguments to your function whatever you want? For example, set_pose could look like this and do exactly the same thing:

def set_pose(foo):
    if foo >= 4:
        return 1
    else:
        return foo + 1

In other words, just because it's called pose inside the function doesn't mean it will refer to pose outside the function.

Athena
  • 3,200
  • 3
  • 27
  • 35