[英]Cannot get player to move in pygame
我當前正在從事的項目通過使用列表繪制地圖。 列表中的每個字符都會通過一項在屏幕上繪制圖塊的功能。 如果玩家擊中a,s,d,w或LEFT,DOWN,RIGHT,UP,則列表中的p位置將發生變化,並且地圖將重新繪制,使其看起來像玩家在移動。 我的問題是,這不起作用。 最初,地圖會在屏幕上正常繪制,如果您單擊任何按鈕,播放器都會移動,但僅在第一次按下按鈕后,播放器才會停止移動。 我認為列表沒有正確更新,但是我很可能錯了,我嘗試做的所有事情都無濟於事,所以我希望這里的人可以告訴我我做錯了什么,要開始游戲,只需按一下八個按鈕中的一個。 我有意見,所以我的代碼更容易理解,謝謝您!
import random, sys, copy, os, pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Dungeon Escape')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (147, 147, 147)
ORANGE = (255, 165, 0)
DISPLAYSURF = pygame.display.set_mode((400, 440))
FPSCLOCK = pygame.time.Clock()
#Pygame works where the graph has no negative
#The Y axis also starts at 0 ON TOP then GOES DOWN
XMAPCORD = 0
YMAPCORD = 0
mapNeedsRedraw = True
#This is the map
currentLevel = [
'w','w','w','w','g','g','w','w','w','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','p','s','s','s','s','s','s','s','w',
'w','w','w','w','w','w','w','w','w','w',
]
#is responsible for drawing the map
def redrawMap():
global XMAPCORD
global YMAPCORD
for i in range(0,100):
if playerPositionMap[i-1] == 'w':
drawWall()
XMAPCORD = XMAPCORD + 40
elif playerPositionMap[i-1] == 's':
drawStone()
XMAPCORD = XMAPCORD + 40
elif playerPositionMap[i-1] == 'g':
drawGoal()
XMAPCORD = XMAPCORD + 40
elif playerPositionMap[i-1] == 'p':
drawPlayer()
XMAPCORD = XMAPCORD + 40
if i % 10 == 0:
YMAPCORD = YMAPCORD + 40
XMAPCORD = 0
mapNeedsRedraw = False
#The main game loop
def movePlayer():
global currentLevel
global playerPositionMap
global drawmap
global playerPosition
global mapNeedsRedraw
running = True
drawmap = True
FPS = 30
fpsClock = pygame.time.Clock()
playerPositionMap = currentLevel
while running:
#This checks to see if the user quits and the keys he presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
#This moves the player according to the key pressed
if event.type == KEYDOWN:
#Tells python the players position in the list
playerPosition = playerPositionMap.index('p')
if ((event.key == K_LEFT or event.key == K_a) and (playerPositionMap[playerPosition - 1] != 'w')):
#Edits the p in the list
playerPositionMap[playerPosition - 1] = 'p'
playerPositionMap[playerPosition] = 's'
#Tells python to redraw map
mapNeedsRedraw = True
elif ((event.key == K_DOWN or event.key == K_s) and (playerPositionMap[playerPosition + 10] != 'w')):
playerPositionMap[playerPosition + 10] = 'p'
playerPositionMap[playerPosition] = 's'
mapNeedsRedraw = True
elif ((event.key == K_RIGHT or event.key == K_d) and (playerPositionMap[playerPosition + 1] != 'w')):
playerPositionMap[playerPosition + 1] = 'p'
playerPositionMap[playerPosition] = 's'
mapNeedsRedraw = True
elif ((event.key == K_UP or event.key == K_w) and (playerPositionMap[playerPosition - 10] != 'w')):
playerPositionMap[playerPosition - 10] = 'p'
playerPositionMap[playerPosition] = 's'
mapNeedsRedraw = True
#Redraws the map if the player pressed a key
if mapNeedsRedraw:
redrawMap()
pygame.display.update()
fpsClock.tick(FPS)
#The four tiles
def drawWall():
pygame.draw.rect(DISPLAYSURF, WHITE, (XMAPCORD, YMAPCORD, 40, 40), 0)
def drawStone():
pygame.draw.rect(DISPLAYSURF, GRAY, (XMAPCORD, YMAPCORD, 40, 40), 0)
def drawGoal():
pygame.draw.rect(DISPLAYSURF, ORANGE, (XMAPCORD, YMAPCORD, 40, 40), 0)
def drawPlayer():
pygame.draw.rect(DISPLAYSURF, GRAY, (XMAPCORD, YMAPCORD, 40, 40), 0)
pygame.draw.rect(DISPLAYSURF, BLACK, (XMAPCORD + 10, YMAPCORD + 10, 20, 20), 0)
movePlayer()
您的代碼僅在按下鍵時的事件上訪問“地圖需要重繪”變量。 如果您想在整個按住鍵期間執行某項操作,請使用pygame.key.get_pressed或設置一個pushed變量,該變量在push事件中變為true,在release事件中變為false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.