[英]Pygame problem: My player image won't move
我正在 python 中創建一個帶有滾動背景的游戲,但我無法讓我的玩家移動。 請注意,我們不允許使用類或精靈
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’”。有誰知道讓我的玩家移動的其他方法嗎?
播放器的 position 是 ( x
, y
) 而不是 ( movex
, movey
)。 此外,參數列表中缺少一個“,”:
screen.blit(player (movex, movey))
screen.blit(player, (x, y))
當然,您必須在應用程序循環之前的某處定義x
和y
:
x = 0
y = 0
while running:
# [...]
x += movex
y += movey
# [...]
screen.blit(player, (x, y))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.