无法让玩家在皮格姆中移动

2024-10-04 09:25:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前从事的项目使用列表绘制地图。列表中的每个字符都通过一个函数在屏幕上绘制一个平铺。如果玩家点击a、s、d、w或左、下、右、上,列表中的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()

Tags: thekeyeventtrueifdefglobalpygame
1条回答
网友
1楼 · 发布于 2024-10-04 09:25:49

您的代码只在按键时访问事件上的“map needs redraw”变量。如果您想在按住某个键的整个过程中执行某项操作,请使用pygame.key.get\u pressed或设置一个pressed变量,该变量在push事件中变为true,在release事件中变为false

相关问题 更多 >