gpt4 book ai didi

python - Pygame 问题 : My player image won't move

转载 作者:行者123 更新时间:2023-12-04 09:39:29 25 4
gpt4 key购买 nike

我正在用 python 创建一个带有滚动背景的游戏,但我无法让我的玩家移动。只是让你知道,我们不允许使用类或 Sprite

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


这是我到目前为止所尝试的,第 40 行出现一个错误,提示“分配前引用了局部变量‘x’”。有谁知道让我的玩家移动的任何其他方法?

最佳答案

玩家的位置是 ( x , y ) 而不是 ( movex , movey )。此外,参数列表中缺少一个“,”:
screen.blit(player (movex, movey))

screen.blit(player, (x, y))

当然你必须定义 xy在应用程序循环之前的某个地方:

x = 0
y = 0

while running:
# [...]

x += movex
y += movey

# [...]

screen.blit(player, (x, y))

关于python - Pygame 问题 : My player image won't move,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62395720/

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