簡體   English   中英

為什么我的按鍵動作不能正常工作?

[英]Why is my key movement not working properly?

我正在創建一個基本游戲,但我的代碼中出現鍵盤響應問題。 當你點擊開始時,立方體會一直沿着你按下的箭頭鍵的方向移動,但我希望它移動你用箭頭鍵按下的任何方向,如果沒有按下任何鍵,它就會停止。 有人可以告訴我我的代碼中有什么錯誤以及如何正確執行嗎?

這是代碼:

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#






問題是您在 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

所以基本上,我在game() 中使用 ( 2x for ) 在我的代碼中犯了一個錯誤。 下次當我使用(for) 時,我會檢查它下面的命令是否在同一個循環中。 謝謝你回答:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM