2017-05-26 79 views
0

我目前正在使用的項目使用列表繪製地圖。列表中的每個字符都會經過一個可以在屏幕上繪製圖塊的功能。如果玩家擊中a,s,d,w或左,右,右,上,列表中的p位置將會改變,地圖將重新繪製,使其看起來像玩家移動。但我的問題是,這是行不通的。地圖最初在屏幕上畫好,如果你點擊任何一個按鈕,玩家將會移動,但只有在第一個按鈕按下後,玩家停止移動。我相信名單沒有正確更新,但我很可能是錯的,我試圖做的所有事情都沒有幫助,所以我希望有人在這裏能告訴我我做錯了什麼,開始遊戲只需按一下八個按鈕中的一個。我有評論,所以我的代碼更容易理解,謝謝高級!無法讓玩家在pygame中移動

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

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在發佈您的MCVE代碼並準確描述問題之前,我們無法爲您提供有效的幫助。 – Prune

+1

另外,從調試工作中發佈證據。你爲什麼不確定*列表是否正確更新?一些簡單的'print'語句將跟蹤控制和數據流。 – Prune

+1

看到這個可愛的[debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)博客尋求幫助。 – Prune

回答

0

您的代碼只能在事件(當前)按鍵被按下時訪問'地圖需要重繪'變量。如果您希望在整個操作過程中執行某項操作,請使用pygame.key.get_pressed或設置一個按下的變量,該變量在推送事件中爲true,在釋放事件中爲false。