简体   繁体   English

玩家运动问题

[英]Player Movement Troubles

I have tried to make my character move in one direction at a time, and it has worked...kind of. 我试图使我的角色一次朝一个方向移动,并且确实起作用了……有点。 The upward and downward motion of the character is wrong -- it requires the user to hold the W / S / / for the player to move in the up / down direction. 角色的向上和向下运动是错误的-它要求用户按住W / S / / 才能使玩家沿向上/向下方向移动。

I have no clue why this is occurring, since the left and right motion of the character works fine - that is, the character moves with the user input pressing a button ONCE ( A / D / / ). 我不知道为什么会发生这种情况,因为角色的左右动作正常-也就是说,角色随用户输入按一次按钮( A / D / / )移动。 Note that this is Python 2.7. 请注意,这是Python 2.7。

Code: 码:

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

What would happen with the code above is that the player would click the button 'Play Game', the game window would open, and the player would control a green square and move it left, right, up and down. 上面的代码将发生以下情况:玩家将单击“玩游戏”按钮,游戏窗口将打开,并且玩家将控制一个绿色方块并将其左右,上下移动。

When the player moves left and right, they only need to press A or D or or ONCE for the square to move left / right. 当玩家左右移动时,他们只需要按AD ONCE即可使方块左右移动。 The case for the top and down direction is not the same. 上下方向的情况不同。 The character moves as long the user provides input, that is, the keys W or S or or needs to be held for the square to move in said directions. 只要用户提供输入,字符就会移动,也就是说,需要按住WS才能使正方形在所述方向上移动。 How can I fix this? 我怎样才能解决这个问题?

It seems that your event.type==KEYUP: condition is buggy: 看来您的event.type==KEYUP:条件有问题:
you are setting false the up and down movements after setting them true... 您将其设置为false后将上,下运动设置为false ...
Check this code: 检查此代码:
see comments in the last two if-else statements 查看最后两个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


As a Suggestion: 作为建议:
You can use print statements in the parts of codes where you think the bug can be. 您可以在您认为该错误可能存在的代码部分中使用打印语句。
And then track the printed records to see where things are working well and where they are not. 然后跟踪打印的记录,以查看哪些地方运行良好,哪些地方运行不正常。
This will give you a fair idea about the bug. 这将使您对错误有一个清晰的认识。
Its part of White Box Testing 白盒测试的一部分

It looks like you copied code for continuous movement and tried to adapt it. 看起来您复制了代码以使其连续移动并尝试对其进行调整。 If you want to just move instantly upon pressing the button, have your KEYDOWN events change the player's position directly. 如果您想在按下按钮后立即移动,请让您的KEYDOWN事件直接更改玩家的位置。

If you want to animate the movement though, things get more complex: you will have to have 2 states: moving and standing. 但是,如果要为运动设置动画,事情会变得更加复杂:您将必须具有两种状态:运动和站立。 When standing, you listen for KEYDOWN events, and then trigger a move in the desired direction if there is one. 站立时,您侦听KEYDOWN事件,如果有,则触发向所需方向的移动。 If moving, you ignore KEYDOWN events (or store them for later if you want to get really advanced) and switch again to standing once you arrive at the target square. 如果移动,则可以忽略KEYDOWN事件(或者,如果要使其真正高级,可以将它们存储以备后用),并在到达目标方块后再次切换到站立状态。

Additionally, if you want to support both modes of transit (press key OR hold key) you will have to add additional logic... maybe check pygame.key.get_pressed() to see if you should transition from moving to standing, or just keep moving another square. 此外,如果您想同时支持两种运输方式(按键或按住键),则必须添加其他逻辑...也许检查pygame.key.get_pressed()看看您是否应该从移动过渡到站立,或者继续移动另一个正方形。

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

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