gpt4 book ai didi

python - 基本运行周期不工作

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:03 25 4
gpt4 key购买 nike

我对 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)我尝试让它在设置姿势之前打印一些东西,它确实做到了,但它没有设置姿势。我尝试让它打印姿势,结果表明姿势没有改变。所以,是的,我已经找到了我的问题,但我一生都找不到解决方案。

最佳答案

不要尝试通过引用传递 - 这就是您正在做的事情,而是尝试这样的事情:

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 并不意味着它会引用函数外部的pose。

关于python - 基本运行周期不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com