没有按下时,Pygame.key.get_pressed被识别出来

时间:2018-04-09 01:42:20

标签: python python-3.x pygame

我对编码很新(比如3周),我一直在尝试使用pygame制作一个回合制的RPG战斗系统。我遇到了一个涉及pygame的key.get_pressed的问题。我正在制作一个下拉菜单,如果你按下ENTER,会出现一个较小的黑框,当你再次按下ENTER时,该框应该消失。不幸的是,黑盒出现但几乎立即消失。我使用print函数作为调试来查找错误,似乎程序注册了第二个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()

1 个答案:

答案 0 :(得分:1)

所以发生的事情是你按住ENTER按钮。在while循环中,当您按Enter键时,它会调用select()函数,然后该函数会检测仍在按下的ENTER。让我画一个视觉效果:

按下ENTER键 - &gt; select()被调用,drop_menu_state变为True - &gt; select()检测到仍然按下了ENTER - &gt; drop_menu_state变为虚假

您可能会问,它是如何检测到的?那么帧正以每秒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

我简化了你的代码(没有==真)pre_time + 5 > time.time()说的是自从上次将pre_time定义为当前时间以来已经过了5秒。 我希望这有帮助!