修复 Pygame 玩家输入响应时间

我正在开发一个简单的2048 游戏。到目前为止,它似乎进展顺利,我认为我遇到的问题是我的代码在我的control函数上缩放如此之快,以至于它没有更新列表。该列表开始于: ['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']


但是当您按下按钮时,列表不会更新。我曾尝试调整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()

杨魅力
浏览 177回答 1
1回答

慕桂英4014372

Pygame 从来没有该game.controls()函数的任何事件,因为它们都被主循环中的子句消耗掉了。def main():&nbsp; &nbsp; game = game_func()&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; # CONSUME EVERY EVENT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end()&nbsp; &nbsp; &nbsp; &nbsp; game.blit()&nbsp; &nbsp; &nbsp; &nbsp; game.controls()&nbsp; # NO EVENTS LEFT FOR THIS也许如果事件不是pygame.QUIT,则将其传递给game.controls(). 例如:def main():&nbsp; &nbsp; game = game_func()&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; game.blit()&nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game.controls( event )&nbsp;&nbsp;和控制是:class game_func:&nbsp; &nbsp; ...&nbsp; &nbsp; def controls(self, event):&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.input = True
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python