简体   繁体   English

无法让玩家在pygame中移动

[英]Cannot get player to move in pygame

The project I am currently working on draws a map by using a list. 我当前正在从事的项目通过使用列表绘制地图。 Each character in the list goes through a function that will draw a tile on screen. 列表中的每个字符都会通过一项在屏幕上绘制图块的功能。 If the player hits a,s,d,w or LEFT,DOWN,RIGHT,UP the p position in the list will change and the map will be redrawn making it look like the player moved. 如果玩家击中a,s,d,w或LEFT,DOWN,RIGHT,UP,则列表中的p位置将发生变化,并且地图将重新绘制,使其看起来像玩家在移动。 My problem however is that this is not working. 我的问题是,这不起作用。 The map initially draws on the screen fine and if you click any of the button the player will move but only on the first button press, after that the player stops moving. 最初,地图会在屏幕上正常绘制,如果您单击任何按钮,播放器都会移动,但仅在第一次按下按钮后,播放器才会停止移动。 I believe that the list isn't updating correctly but I am most likely wrong, everything I have tried to do doesn't help so I was hoping that someone here could tell me what I am doing wrong, to start the game just press one of the eight buttons. 我认为列表没有正确更新,但是我很可能错了,我尝试做的所有事情都无济于事,所以我希望这里的人可以告诉我我做错了什么,要开始游戏,只需按一下八个按钮中的一个。 I have comments so my code is more easy to understand, thank you in advanced! 我有意见,所以我的代码更容易理解,谢谢您!

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

Your code only accesses the 'map needs redraw' variable on events WHEN (the moment) the key is pushed. 您的代码仅在按下键时的事件上访问“地图需要重绘”变量。 If you want to do something the entire time a key is held, use pygame.key.get_pressed or set up a pressed variable that turns true on the push event and false on the release event. 如果您想在整个按住键期间执行某项操作,请使用pygame.key.get_pressed或设置一个pushed变量,该变量在push事件中变为true,在release事件中变为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM