【发布时间】:2021-02-21 01:14:30
【问题描述】:
我有以下问题:角色没有正确地向左移动。我尝试调试并看到在 drawfrantz 函数中 moveLeft 不是真的,但在 while 游戏循环中它是。我已经尝试了很多,比如设置全局等等,但它似乎根本不起作用。我遵循了一个 yt 教程,这是链接 https://www.youtube.com/watch?v=9Kh9s9__ywo
import pygame
pygame.init()
display_width = 1280
display_height = 720
x = 250
y = 250
velocity = 10
moveRIGHT = False
moveLeft = False
stepIndex = 0
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Frantz Reichts!")
clock = pygame.time.Clock()
frantz_stationary = pygame.image.load("assets/frantz/trash_ting3.png")
frantz_going_left = [None]*10
for indexofpic in range(1,9):
frantz_going_left[indexofpic-1] = pygame.image.load("assets/frantz/L" + str(indexofpic) + ".png")
indexofpic = (indexofpic + 1)
frantz_going_right = [None]*10
for picindex in range(1,9):
frantz_going_right[picindex-1] = pygame.image.load("assets/frantz/R" + str(picindex) + ".png")
picindex = (picindex + 1)
# Images #
stage1full = pygame.image.load("assets/WholeStage.png")
karavane = pygame.image.load("assets/Karavane.png")
def DrawFrantz():
global stepIndex
if stepIndex >= 8:
stepIndex = 0
if moveLeft == True:
screen.blit(frantz_going_left[stepIndex], (x, y))
stepIndex += 1
elif moveRIGHT == True:
screen.blit(frantz_going_right[stepIndex], (x, y))
stepIndex += 1
else:
screen.blit(frantz_stationary, (x, y))
while(True): #Game Loop
screen.fill((0, 0, 0))
screen.blit(stage1full, (0, 0))
screen.blit(karavane, (0,0))
DrawFrantz()
userInput = pygame.key.get_pressed()
if userInput[pygame.K_a]:
x -= velocity
moveLeft = True
moveRIGHT = False
if userInput[pygame.K_d]:
x += velocity
moveLeft = False
moveRIGHT = True
else:
moveLeft = False
moveRIGHT = False
stepIndex = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.time.delay(100)
pygame.display.update()
clock.tick(60)
【问题讨论】:
-
不确定,但只是一个简短的说明:您不需要在开始时更新那些“for”循环中的计数器,也许不试试?
-
在您的“DrawFrantz”函数中,您调用了全局 stepIndex,但也许您也错过了调用 moveLeft 和 moveRight 全局?
标签: python pygame pygame-surface