gpt4 book ai didi

python - 为什么我的按键 Action 不能正常工作?

转载 作者:行者123 更新时间:2023-12-03 16:19:56 25 4
gpt4 key购买 nike

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

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

关于python - 为什么我的按键 Action 不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64967375/

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