我试着让我的角色一次朝一个方向移动,结果成功了。。。有点字符的上下移动是错误的——它要求用户按住W/S/↓/↑用于玩家在上/下方向上移动。
我不知道为什么会发生这种情况,因为角色的左右运动都很好——也就是说,角色随着用户输入按下一次按钮而移动(a/D/←/→)。请注意,这是Python 2.7。
代码:
# Import modules
import sys, pygame, os, random, Tkinter, ttk, math
from pygame import *
from random import *
from Tkinter import *
from ttk import *
from math import *
class Application(Frame):
# Game Menu Setup
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_buttons()
def Collision(self):
pass
def create_buttons(self):
self.button = Button(self)
self.button["text"] = "Play Game"
self.button["command"] = self.Game
self.button.grid(row=0, column=1)
self.button1 = Button(self)
self.button1["text"] = "Help / Instructions"
self.button1["command"] = self.Instructions
self.button1.grid(row=0, column=2)
self.button2 = Button(self)
self.button2["text"] = "Quit"
self.button2["command"] = self.Quit
self.button2.grid(row=0, column=3)
# Main Class
def Game(self):
# Initialise Pygame + Clock
pygame.init()
mainClock = pygame.time.Clock()
pygame.mouse.set_visible(False)
# Image Setup
Cursor = pygame.image.load("Cursor.png")
Food = pygame.image.load("Food.png")
Background = pygame.image.load("Wallpaper.jpg")
# Food Setup
FoodImage = pygame.Surface((20, 20))
# Music Setup
pygame.mixer.music.load("Helix Nebula.mp3")
# Window Setup
WINDOWHEIGHT = 600 #set standard values
WINDOWWIDTH = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('The Basilisk')
# Player Drawing
player = pygame.Rect(50, 50, 50, 50)
# Movement Variables
moveLEFT = False
moveRIGHT = False
moveUP = False
moveDOWN = False
MOVESPEED = 7
# Score Setup
## Score = 0
## print score
# Game Loop & Events
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Change the keyboard variables
elif event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
moveLEFT = True
moveRIGHT = False
moveUP = False
moveDOWN = False
movex = 0.5
movey = 0
if event.key == K_RIGHT or event.key == ord('d'):
moveRIGHT = True
moveLEFT = False
moveUP = False
moveDOWN = False
movex = -0.5
movey = 0
if event.key == K_UP or event.key == ord('w'):
moveUP = True
moveLEFT = False
moveRIGHT = False
moveDOWN = False
movey = 0.5
movex = 0
if event.key == K_DOWN or event.key == ord('s'):
moveDOWN = True
moveLEFT = False
moveRIGHT = False
moveUP = False
movey = -0.5
movex = 0
if event.key == ord('o'):
pygame.mixer.music.pause()
if event.key == ord('r'):
pygame.mixer.music.play()
if event.key == ord('p'):
pygame.mixer.music.unpause()
elif event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == K_LEFT or event.key == ord('a'):
moveLEFT = True
moveRIGHT = False
moveUP = False
moveDOWN = False
elif event.key == K_RIGHT or event.key == ord('d'):
moveRIGHT = True
moveLEFT = False
moveUP = False
moveDOWN = False
elif event.key == K_UP or event.key == ord ('w'):
moveUP = True
moveLEFT = False
moveUP = False
moveDOWN = False
elif event.key == K_DOWN or event.key == ord('s'):
moveDOWN = True
moveLEFT = False
moveUP = False
moveDOWN = False
# Loading Image(s) Setup
windowSurface.blit(Background, (0,0))
windowSurface.blit(Food, (15, 15))
# Player Movement Setup
if moveDOWN and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUP and player.top > 0:
player.top-= MOVESPEED
if moveLEFT and player.left > 0:
player.left -= MOVESPEED
if moveRIGHT and player.right < WINDOWWIDTH:
player.right += MOVESPEED
# Mouse Setup
mousex, mousey = pygame.mouse.get_pos()
mousex -= Cursor.get_width()/2
mousey -= Cursor.get_height()/2
# Final Update + Character Loading
pygame.draw.rect(windowSurface, pygame.Color('green'), player)
windowSurface.blit(Cursor, (mousex, mousey))
pygame.display.update()
mainClock.tick(60)
# Quit Screen
def Quit(self):
os._exit(0)
# Instructions Screen
def Instructions(self):
root.title("Instructions")
root.geometry("260x200")
app = Frame(root)
app.grid()
label_1 = Label(app, text = "The Point of the game is to eat")
label_2 = Label(app, text = "as many chicks as possible using")
label_3 = Label(app, text = "the 'Basilisk'. The character controls")
label_4 = Label(app, text = "the Basilisk using the WASD / arrow keys.")
label_5 = Label(app, text = "Press the 'Escape' key to exit.")
label_6 = Label(app, text = "Press the 'o' key to pause the music,")
label_7 = Label(app, text = "'p' key to unpause the music, and")
label_8 = Label(app, text = "'r' to restart / play the music.")
label_1.grid()
label_2.grid()
label_3.grid()
label_4.grid()
label_5.grid()
label_6.grid()
label_7.grid()
label_8.grid()
# Final Settings for Menu
root = Tk()
root.title("Navigation")
root.geometry("260x25")
app = Application(root)
root.mainloop()
上面的代码会发生的情况是,玩家会点击"玩游戏"按钮,游戏窗口会打开,玩家会控制一个绿色方块,并将其向左、向右、向上和向下移动。
当玩家左右移动时,他们只需要按下A或D→ONCE使正方形向左/向右移动。上下方向的情况不同。只要用户提供输入,字符就会移动,即按键W或S或↑以使正方形在所述方向上移动。我该怎么解决这个问题?
您的event.type==KEYUP:
条件似乎有问题:
你在设置false
上下运动后,将它们设置为true
检查此代码:
请参阅最后两个if-else
语句中的注释
elif event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == K_LEFT or event.key == ord('a'):
moveLEFT = True
moveRIGHT = False
moveUP = False
moveDOWN = False
elif event.key == K_RIGHT or event.key == ord('d'):
moveRIGHT = True
moveLEFT = False
moveUP = False
moveDOWN = False
elif event.key == K_UP or event.key == ord ('w'):
moveUP = True
moveLEFT = False
moveUP = False ##This line is faulty moveRIGHT=FALSE should be used instead
moveDOWN = False
elif event.key == K_DOWN or event.key == ord('s'):
moveDOWN = True
moveLEFT = False
moveUP = False
moveDOWN = False ##This line is faulty moveRIGHT=FALSE should be used instead
建议:
您可以在您认为可能存在错误的代码部分使用print语句。
然后跟踪打印的记录,看看哪些地方运行良好,哪些地方不正常
这会让你对这个bug有一个大致的了解
它是白盒测试的一部分
看起来你复制了连续移动的代码,并试图对其进行调整。如果你想在按下按钮后立即移动,请让你的KEYDOWN事件直接更改玩家的位置。
如果你想设置运动的动画,事情会变得更复杂:你必须有两种状态:移动和站立。当你站着的时候,你会监听KEYDOWN事件,然后触发向所需方向移动(如果有的话)。如果移动,您将忽略KEYDOWN事件(或者如果您想获得真正的高级,则将其存储以便稍后使用),并在到达目标广场后再次切换为站立。
此外,如果你想支持这两种运输模式(按键或按住键),你必须添加额外的逻辑。。。也许可以查看pygame.key.get_pressed(),看看你是否应该从移动过渡到站立,或者只是继续移动另一个方块。