私が現在取り組んでいるプロジェクトは、リストを使って地図を描きます。リスト内の各文字は、スクリーン上にタイルを描く関数を通る。プレーヤーがa、s、d、w、またはLEFT、DOWN、RIGHT、UPを押すと、リスト内のpの位置が変わり、地図が再描画され、プレイヤーが移動したように見えます。しかし、私の問題は、これは動作していないということです。地図は最初は画面上に描画され、ボタンのどれかをクリックすると、プレーヤーは移動しますが、最初のボタンを押すだけで、プレーヤーは移動を停止します。私はリストが正しく更新されていないと信じていますが、私は間違っている可能性が最も高いです、私がしようとしていることは役に立たないので、ここで誰かが私が間違っていることを教えてくれることを願っていました。 8つのボタンのうちの1つです。私はコメントがありますので、私のコードは理解しやすいです、先進的にありがとう!パイゲームでプレイヤーを動かすことができません
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()
ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 MCVEコードを投稿して問題を正確に記述するまでは、効果的にお手伝いすることはできません。 – Prune
また、デバッグ作業の証拠を投稿してください。リストが正しく更新されているかどうかは確かではありませんか?いくつかの簡単な 'print'ステートメントは制御とデータフローをトレースします。 – Prune
この素敵な[デバッグ](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)のブログを参照してください。 – Prune