[英]Pygame.key.get_pressed recognized when not pressed
我是編碼新手(大約3周),並且我一直在嘗試使用pygame制作基於回合的RPG戰斗系統。 我遇到了涉及pygame的key.get_pressed的問題。 我正在制作一個下拉菜單,其中如果您按ENTER鍵,則會出現一個較小的黑框,而當您再次按ENTER鍵時,該框應消失。 不幸的是,黑框出現了,但幾乎立即消失了。 我將打印功能用作調試程序以查找錯誤,並且似乎程序注冊了第二個ENTER鍵(應關閉該框),而沒有被按下。 任何幫助將不勝感激。 我也在使用Windows 10和Python 3.6
import pygame
pygame.init()
win = pygame.display.set_mode((820,567))
pygame.display.set_caption('Basic battle')
drop_menu_attack = pygame.image.load('drop menu attack.png')
health_pp = pygame.image.load('hp and pp.png')
menu_img = pygame.image.load('menu.png')
attack_img = pygame.image.load('attack.png')
defend_img = pygame.image.load('defend.png')
bag_img = pygame.image.load('bag.png')
background_img = pygame.image.load('rpg background.gif')
Snake_enemy_img = pygame.image.load('pixil-frame-0.png')
clock = pygame.time.Clock()
class player(object):
def __init__ (self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.cursor_state = 1
self.attack_x = 100
self.defend_x = 325
self.bag_x = 575
self.top_menu_y = 70
class drop_menu(object):
def __init__ (self, x, y):
self.x = x
self.y = y
self.drop_menu_state = False
def cursor_position():
if c_p.cursor_state == 1:
print ('it should be on attack')
c_p.x = c_p.attack_x
c_p.y = c_p.top_menu_y
elif c_p.cursor_state == 2:
print ('it hould be on defend')
c_p.x = c_p.defend_x
c_p.y = c_p.top_menu_y
elif c_p.cursor_state == 3:
print ('it should be on bag')
c_p.x = c_p.bag_x
c_p.y = c_p.top_menu_y
elif c_p.cursor_state > 3:
c_p.cursor_state = 3
print ('it should be on bag')
c_p.x = c_p.bag_x
c_p.y = c_p.top_menu_y
elif c_p.cursor_state < 1:
c_p.cursor_state = 1
print ('it should be on attack')
c_p.x = c_p.attack_x
c_p.y = c_p.top_menu_y
def select():
if c_p.cursor_state == 1:
d_m.drop_menu_state = True
print (d_m.y)
while d_m.drop_menu_state:
pygame.time.delay(150)
win.blit (drop_menu_attack, (d_m.x, d_m.y))
pygame.display.update()
print ('dooooog')
keys = pygame.key.get_pressed()
if d_m.drop_menu_state == True:
if keys[pygame.K_RETURN]:
d_m.drop_menu_state = False
print('SSSSSS')
pygame.event.pump()
def redraw_gamewindow():
win.blit (background_img, (0,0))
win.blit (Snake_enemy_img, (300, 174))
win.blit (menu_img, (25, 425 ))
win.blit (menu_img, (25, 10))
win.blit (health_pp, (70, 450))
win.blit (attack_img, (c_p.attack_x + 30, c_p.top_menu_y - 15))
win.blit (defend_img, (c_p.defend_x + 30, c_p.top_menu_y - 15))
win.blit (bag_img, (c_p.bag_x + 30, c_p.top_menu_y - 15))
pygame.draw.rect(win, (255, 255, 255), (c_p.x, c_p.y, 7 , 7))
pygame.display.update()
d_m = drop_menu(25, 125)
print (d_m.x)
c_p = player(100, 70, 7,7)
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
c_p.cursor_state = c_p.cursor_state + 1
cursor_position()
pygame.time.delay(150)
if keys[pygame.K_LEFT]:
c_p.cursor_state = c_p.cursor_state -1
cursor_position()
pygame.time.delay(150)
if keys[pygame.K_RETURN]:
select()
pygame.time.delay(500)
redraw_gamewindow()
所以發生的事情是您按住ENTER鍵。 在while
循環中,當您按Enter鍵時,它將調用select()
函數,然后該函數將檢測仍在按的ENTER鍵。 讓我畫一個視覺效果:
按下ENTER鍵-> select()被調用, drop_menu_state
變為drop_menu_state
> select()檢測到ENTER仍被按下-> drop_menu_state
變為false
您可能會問,它如何檢測? 好了,這些幀以每秒想要的27幀的速度運行。 因此,在按ENTER鍵時,它將每隔1/27秒更改一次放置菜單狀態。 如果您可以按住ENTER鍵的時間不超過1/27秒,它將正常工作。
有3個解決方案。 將菜單的關閉綁定到另一個鍵,添加短計時器或使用事件循環。 我的編程風格是添加一個計時器。
import time
pre_time = time.time()
def select():
... # code in select()
if d_m.drop_menu_state and pre_time + 5 > time.time() and keys[pygame.K_RETURN]:
pre_time = time.time()
d_m.drop_menu_state = False
我簡化了您的代碼(否== True) pre_time + 5 > time.time()
所說的是,自上次將pre_time定義為當前時間以來已經有5秒了。 我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.