2016-07-06 67 views
1

我還是個新來的python,並且已經開始使用pygame。我正在製作一個無盡的亞軍遊戲,但是我的跑步週期遇到了問題。這裏是我的代碼:基本運行週期不工作

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() 

所以屏幕滾動,並出現在我的性格,但他留在他的第一個位置(幀我猜。)在部分地方運行set_poses(pose)我試圖擁有它也是對之前打印的東西設置它的姿勢,但沒有設置姿勢。我試着讓它打印出姿勢,並表明姿勢沒有變化。所以,是的,我發現了我的問題,但我不能爲我的生活尋找解決方案。

+0

Btw。詳細瞭解[MCVE],_not設置pose_可以在沒有pygame和所有代碼邏輯的情況下進行復制。 –

回答

1

而不是試圖通過引用傳遞 - 這是你在做什麼,嘗試這樣的事情:

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

然後當你試圖更新pose變量別處:

pose = set_pose(pose)

您是否同意您可以隨意調用參數給您的函數?例如,set_pose看起來是這樣的,做正是同樣的事情:

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

換句話說,只是因爲它被稱爲pose裏面的功能並不意味着它會是指構成功能。

+0

謝謝!它的作品,我明白你的意思。 –