gpt4 book ai didi

python - 球员移动问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:01 25 4
gpt4 key购买 nike

我尝试过让我的角色一次朝一个方向移动,而且它确实有效……有点效果。角色的向上和向下运动是错误的 - 它需要用户按住 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()

上面的代码会发生的情况是,玩家单击“玩游戏”按钮,游戏窗口将打开,玩家将控制一个绿色方 block 并将其左、右、上、下移动。

玩家左右移动时,只需按AD 一旦方 block 向左/向右移动。向上和向下方向的情况并不相同。只要用户提供输入,即按键 WS,角色就会移动需要保持正方形才能朝上述方向移动。我怎样才能解决这个问题?

最佳答案

您的 event.type==KEYUP: 条件似乎有问题:
在设置 true 后,您将上下移动设置为 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


作为建议:
您可以在您认为可能存在错误的代码部分使用 print 语句。
然后跟踪打印的记录,看看哪些地方运作良好,哪些地方运作不佳。
这将使您对该错误有一个公平的了解。
它是白盒测试的一部分

关于python - 球员移动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com