为啥我的按键动作不能正常工作?

Posted

技术标签:

【中文标题】为啥我的按键动作不能正常工作?【英文标题】:Why is my key movement not working properly?为什么我的按键动作不能正常工作? 【发布时间】:2021-03-06 02:33:40 【问题描述】:

我正在创建一个基本游戏,但我的代码中的键盘响应有问题。 当你点击开始时,立方体会一直沿着你按下的箭头键的方向移动,但我希望它可以移动你用箭头键按下的任何方向,如果没有按下任何键它就会停止。有人可以告诉我我的代码中有什么错误以及如何正确处理吗?

代码如下:

import  pygame
import  time
import  sys
import  pygame.event as EVENTS
from  pygame.locals import *
import  random

print("Welcome to the prototype of rpg game -Made by Adam Pospíchal.")

pygame.init()

sirka = 800
vyska = 800

okno = pygame.display.set_mode((sirka,vyska))
pygame.display.set_caption("RPG GAME")
#PARAMETRE OKNA#

#PARAMETRE OKNA#

#PARAMETRE KOCKY HRACA#

#PARAMETRE MENU#

#farby - RGB#
RM = 255
GM = 255
BM = 0


player_r = 0
player_g = 255
player_b = 0

player_height = 20
player_width = 20



smerx = 0
smery = 0

x_pos = 390
y_pos = 390
    
#farby - RGB#

x = 400
y = 400

#PARAMETRE MENU#

#TEXT#

font = pygame.font.SysFont('Arial', 40)
textsurface_title = font.render('CUBE ADVENTURE', False, (255, 0, 0))
textsurface_Controls = font.render('CONTROLS', False, (255, 0, 0))
textsurface = font.render('START', False, (255, 0, 0))
textsurface2 = font.render('CONTROLS', False, (255, 0, 0))
textsurface3 = font.render('EXIT', False, (255, 0, 0))

#TEXT#

#MAIN MENU#
obdlznik_start = pygame.Rect(300,200,200,100)
obdlznik_controls = pygame.Rect(300,350,200,100)
obdlznik_exit = pygame.Rect(300,500,200,100)


#MAIN MENU


def game():
    x_pos = 360
    y_pos = 360

    smerx = 0
    smery = 0

    player_r = 0
    player_g = 255
    player_b = 0

    player_height = 20
    player_width = 20

    sirka = 800
    vyska = 800
    
    while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    koniec()
            
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                         smerx = 1
                    if event.key == pygame.K_LEFT:
                         smerx = -1
                    if event.key == pygame.K_DOWN:
                         smery = 1
                    if event.key == pygame.K_UP:
                         smery = -1
                    if event.key == pygame.K_ESCAPE:
                         koniec()
    
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                        smerx = 0
                    if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                        smery = 0

            #BORDER
            if x_pos + player_width > 800:
                x_pos = 800
            if x_pos < 0:
                x_pos = 0
            if y_pos + player_height > 800:
                y_pos = 800
            if y_pos < 0:
                y_pos = 0
            #BORDER
            
            #PLAYER CUBE
            okno.fill((0,0,0))
            x_pos = x_pos + smerx
            y_pos = y_pos + smery
            obdlznik_player = pygame.Rect(x_pos,y_pos,player_height,player_width)
            pygame.draw.rect(okno,(player_r,player_g,player_b),obdlznik_player)
            
            pygame.display.update()
            #PLAYER CUBE

            #HEALTHBAR

            #HEALTHBAR

def koniec():   #EXIT GAME
    pygame.quit()
    sys.exit()



def menu_controls(): #CONTROLS MENU
    while True:
         for event in pygame.event.get():
            if event.type == QUIT:
                 koniec()

            #FARBY KOCKY KURZORA
            RR = 255
            GR = 0
            BR = 0
            #FARBY KOCKY KURZORA 
            mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
            
            mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI

            obdlznik_spat = pygame.Rect(10,10,40,40)
        
            obdlznik_spat_2 = pygame.Rect(30,25,10,10)
            
            spat_collision = mouse_rect.colliderect(obdlznik_spat)
            if spat_collision:
                RR = 0
                GR = 0
                BR = 255
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        menu_main()

            
           
            pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
            
            
            okno.fill((0,0,0))
    
            okno.blit(textsurface_Controls,(300,50))#NADPIS
               
            pygame.draw.rect(okno,(RM,GM,BM),obdlznik_spat)
            
            pygame.draw.polygon(okno, (255,0,0), ((15,30), (30,15), (30,45)))

            pygame.draw.rect(okno,(255,0,0),obdlznik_spat_2)

            pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
                
            pygame.display.update() #UPDATE OBRAZOVKY





def menu_main():
    while True:
         for event in pygame.event.get():
            if event.type == QUIT:
                koniec()

            #FARBY KOCKY KURZORA
            RR = 255
            GR = 0
            BR = 0
            #FARBY KOCKY KURZORA 
            
            mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
            pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
            mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI
            s_collision = mouse_rect.colliderect(obdlznik_start)
            c_collision = mouse_rect.colliderect(obdlznik_controls)
            e_collision = mouse_rect.colliderect(obdlznik_exit)
            if s_collision:
                RR = 0
                GR = 255
                BR = 0
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        game()
                                          
            if c_collision:
                RR = 0
                GR = 0
                BR = 255
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        menu_controls()
            if e_collision:
                RR = 255
                GR = 255
                BR = 255
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        koniec()
                        
           
                
            okno.fill((0,0,0))
    
            okno.blit(textsurface_title,(250,50))#NADPIS
               
            pygame.draw.rect(okno,(RM,GM,BM),obdlznik_start)
            okno.blit(textsurface,(350,230))
               
            pygame.draw.rect(okno,(RM,GM,BM),obdlznik_controls)
            okno.blit(textsurface2,(315,370))
               
            pygame.draw.rect(okno,(RM,GM,BM),obdlznik_exit)
            okno.blit(textsurface3,(360,520))

            pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
                
            pygame.display.update() #UPDATE OBRAZOVKY

menu_main()

#MAIN MENU#

#HERNY CYKLUS - PRIKAZY V HRE#






【问题讨论】:

请出示代码。 @Adam_2002 能否请您也添加代码 例如:如果我想向右移动立方体,我按右箭头键,它的 x 位置连续变为 x+1,直到我停止按下按钮。与立方体的 y 位置相同。 我明白了,但是您希望我们帮助您编写代码,或者,您编写了代码并且遇到了错误?? 您在问题中有编辑选项,编辑问题并将其发布到那里 【参考方案1】:

问题是您在 game() 中使用了两个事件循环。您必须在同一个事件循环中添加所有事件。

所以,

            for event in pygame.event.get():
                if event.type == QUIT:
                    koniec()
            
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                         smerx = 1
                    if event.key == pygame.K_LEFT:
                         smerx = -1
                    if event.key == pygame.K_DOWN:
                         smery = 1
                    if event.key == pygame.K_UP:
                         smery = -1
                    if event.key == pygame.K_ESCAPE:
                         koniec()
    
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                        smerx = 0
                    if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                        smery = 0

这个 ^ 会变成刚才的

            for event in pygame.event.get():
                if event.type == QUIT:
                    koniec()
            
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                         smerx = 1
                    if event.key == pygame.K_LEFT:
                         smerx = -1
                    if event.key == pygame.K_DOWN:
                         smery = 1
                    if event.key == pygame.K_UP:
                         smery = -1
                    if event.key == pygame.K_ESCAPE:
                         koniec()
    
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                        smerx = 0
                    if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                        smery = 0

【讨论】:

谢谢。你的回答对我帮助很大:)。【参考方案2】:

所以基本上,我在 game() 中使用 (2x for) 在我的代码中犯了一个错误。下次我使用(for)时,我会检查它下面的命令是否在相同的循环中。感谢for回答:)

【讨论】:

以上是关于为啥我的按键动作不能正常工作?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的约束不能正常工作?

我的 quickSort() 程序不能正常工作,为啥? [关闭]

为啥这个查询不能正常工作?

为啥我的多通道映射不能正常工作?

为啥我的模型查询集不能正常工作?

为啥我的 .htaccess 文件的 301 重定向不能正常工作?