无法让玩家在pygame中移动

时间:2017-05-26 00:33:17

标签: python list 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()

1 个答案:

答案 0 :(得分:0)

您的代码仅在事件按下(当前)按键时访问“地图需要重绘”变量。如果你想在整个键持有时做一些事情,可以使用pygame.key.get_pressed或设置一个push事件,该事件在push事件中变为true,在release事件中变为false。