Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中使用键盘命令时出现问题_Python_Python 3.x_Pygame - Fatal编程技术网

Python 在pygame中使用键盘命令时出现问题

Python 在pygame中使用键盘命令时出现问题,python,python-3.x,pygame,Python,Python 3.x,Pygame,在做一些游戏编程时,我遇到了键盘命令的问题。在我的代码中,我有一个食物条和一个名为money\u bar的货币银行变量。当我在游戏中按一个键(比如f)时,游戏中的食物栏会增加,当我按f时,游戏中的食物栏会从我的钱栏中扣除10美元。 食物栏显示了我当前的食物量,应该每秒钟减少一次。但是,event()中的键盘命令似乎都不起作用。我可以知道我的代码有什么问题吗? 这是我的食物栏和“金钱”栏的初始化: def __init__(self): pygame.init()

在做一些游戏编程时,我遇到了键盘命令的问题。在我的代码中,我有一个食物条和一个名为
money\u bar
的货币银行变量。当我在游戏中按一个键(比如f)时,游戏中的食物栏会增加,当我按f时,游戏中的食物栏会从我的钱栏中扣除10美元。
食物栏显示了我当前的食物量,应该每秒钟减少一次。但是,
event()
中的键盘命令似乎都不起作用。我可以知道我的代码有什么问题吗? 这是我的
食物栏
和“金钱”栏的初始化:

def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()
        self.living = 1
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        self.time = pygame.time.get_ticks()
        pygame.key.set_repeat(500, 100)
        self.all_sprites = pygame.sprite.Group()
        self.console = Console(self, 0)
        self.player = Player(self, 390, 595)
        self.work = Work(self, 450, 250)
        self.food_station = Food_Station(self, 750, 200)
        self.food = Food(self, 25, 20)
        self.education = Education(self, 300, 10)
        self.school = School(self, 100, 200)
        self.family = Family(self, 600, 10)
        self.money = Money(self, 800, 15)
        initial_food = 100
        self.food_bar = initial_food
        initial_money = 0
        self.money_bar = initial_money
        initial_education = "Student"
        self.education_level = initial_education
        initial_family = 3
        self.family_member = 3
这是我运行主算法的地方:

    def run(self):
        self.playing = True
        self.hunger()
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.draw()
            self.update()
下面是我如何检查事件(包括键盘命令)


提前感谢而
pygame.K_f
是一个关键枚举数常量(请参阅)事件的内容。type是事件枚举数常量(请参阅)。
如果要确定是否按下了某个键,则必须验证事件类型是否为
pygame.KEYDOWN
(或
pygame.keydup
用于按钮释放)以及事件的
.key
属性是否等于键枚举数。e、 g:

pygame.event.get()中事件的
:
如果event.type==pygame.QUIT:
self.quit()
# [...]
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\u f:
# [...]

很抱歉这么晚才回复,但谢谢!
    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()
            if event.type == self.HUNGEREVENT:
                self.food_bar = self.food_bar - 10
                self.all_sprites.update()
                pygame.display.flip()

            if event.type == pygame.K_f:
                self.money_bar = self.money_bar - 10
                self.food_bar = self.food_bar + 15
                self.all_sprites.update()
                pygame.display.flip()

            if event.type == pygame.K_ESCAPE:
                self.quit()