python - 修复 Pygame 玩家输入响应时间

标签 python python-3.x function pygame controls

(抱歉,如果这个问题很难理解,这是我的第一个问题。我很乐意澄清任何需要澄清的事情。提前致谢)

我正在研究一个简单的 2048 game 。到目前为止,它似乎进展顺利,我相信我遇到的问题是我的代码在我的 control 函数上缩放得如此之快,以至于它没有更新列表。该列表开头为: ['无'、'无'、'无'、'无'、'无'、'无'、'无'、'无'、'无'、'无'、'无'、'无', '无', '无', '无', '无']

但是当您按下按钮时,列表不会更新。我尝试过调整 control 函数代码,但没有取得任何进展。

import pygame
import sys
import random

pygame.init()
fps = pygame.time.Clock()

screen_size = screen_width, screen_height = 800, 800
display = pygame.display.set_mode(screen_size)

game_state = ['none', 'none', 'none', 'none',
              'none', 'none', 'none', 'none',
              'none', 'none', 'none', 'none',
              'none', 'none', 'none', 'none']

class game_func:
    def __init__(self):
        self.input = False
        self.count = 0

    def controls(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                self.input = True

    def random(self):
        var = random.randrange(0, 16)
        if game_state[var] == 'none':
            return var
        else:
            self.random()

    def spawn(self):
        if self.input:
            pos = self.random()
            if random.randrange(0, 2) == 1:
                num = 'two'
            else:
                num = 'four'
            game_state[pos] = num
            self.input = False

    def blit(self):
        x, y = 0, 0

        idx = self.count
        pos = game_state[idx]

        if 0 < idx < 4:
            y = 0
            x = idx
        elif 3 < idx < 8:
            y = 200
            x = idx - 4
        elif 7 < idx < 12:
            y = 400
            x = idx - 8
        elif 11 < idx < 16:
            y = 600
            x = idx - 12
        x *= 200
        cord = x, y
        print(idx)
        print(cord)
        print(game_state)

        self.count += 1
        if self.count == 16:
            self.count = 0

def end():
    pygame.quit()
    sys.exit()

def main():
    game = game_func()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                end()
        game.blit()
        game.controls()
        game.spawn()
        pygame.display.update()
        fps.tick(30)


main()
end()

最佳答案

Pygame 从来不会为 game.controls() 函数提供任何事件,因为它们都被主循环中的子句消耗。

def main():
    game = game_func()
    while True:
        for event in pygame.event.get():    # CONSUME EVERY EVENT
            if event.type == pygame.QUIT:
                end()
        game.blit()
        game.controls()  # NO EVENTS LEFT FOR THIS

也许如果事件不是pygame.QUIT将其传递给game.controls()。例如:

def main():
    game = game_func()
    while True:
        game.blit()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                end()
            else:
                game.controls( event )  

控件是:

class game_func:

    ...

    def controls(self, event):
        if event.type == pygame.KEYDOWN:
            self.input = True

关于python - 修复 Pygame 玩家输入响应时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338209/

相关文章:

python - 如何使用keras定义模型架构?

python - 匹配表示正则表达式集的模式

python-3.x - Qt : Session management error in opencv python

python - 使用 Python 在数据库中保留带有西类牙口音的记录

javascript - 如何在 javascript 函数中设置新的变量值?

python - Python 的 HackerRank "filled orders"问题

python - 如何在cygwin中运行的python中拦截ctrl + c命令

python - 导入错误 : No module named _gdbm matplotlib pickle

应该返回函数的 JavaScript 函数变成了函数

无法弄清楚为什么我得到奇怪的输出