为什么我的按键动作不正常



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

这是代码:

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(时,我将检查它下面的命令是否在的同一周期中感谢回答:(

相关内容

  • 没有找到相关文章

最新更新