球员运动麻烦

时间:2013-12-01 23:25:29

标签: python game-development

我试图让我的角色一次向一个方向移动,它已经有效了......有点儿。字符的向上和向下运动是错误的 - 它要求用户持有 W / S / / 让玩家向上/向下移动。

我不知道为什么会发生这种情况,因为角色的左右运动工作正常 - 也就是说,角色会随着用户输入按下ONCE按钮移动( 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 需要保持广场向所述方向移动。我怎样才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的event.type==KEYUP:条件似乎是错误的:
设置正确后,您正在设置false上下移动...
检查此代码:
查看最近两篇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


作为建议:
 您可以在您认为错误的代码部分中使用打印语句  然后跟踪打印的记录,看看哪些东西工作正常,哪些不合适  这将让你对这个bug有一个公平的认识  它是白盒测试的一部分

答案 1 :(得分:0)

看起来您复制代码以进行连续移动并尝试对其进行调整。如果您想在按下按钮后立即移动,请让您的KEYDOWN事件直接改变玩家的位置。

如果你想为动作制作动画,事情会变得更复杂:你必须有两种状态:移动和站立。站立时,您会侦听KEYDOWN事件,然后触发所需方向的移动(如果有)。如果移动,你会忽略KEYDOWN事件(或者如果你想要真正提高它们,可以将它们存储起来),并在到达目标方格后再次切换到站立状态。

此外,如果你想支持两种传输模式(按键或按住键),你将不得不添加额外的逻辑...也许检查pygame.key.get_pressed(),看看你是否应该从移动到转移,或者继续移动另一个方格。