0

如何编写代码,让我的游戏每 30 帧每秒一次通过这些颜色转换?

self.bg_color = 0, 0, 0
self.bg_color = 255, 0, 0
self.bg_color = 0, 255, 0
self.bg_color = 0, 0, 255
self.bg_color = 255, 165, 0
self.bg_color = 255, 255, 0
self.bg_color = 199, 97, 20

编辑:

如果我展示骨架会有帮助吗?

from __future__ import division
import math
import sys
import pygame


class MyGame(object):
    def __init__(self):
        pygame.mixer.init()
        pygame.mixer.pre_init(44100, -16, 2, 2048)
        pygame.init()

        self.width = 800
        self.height = 600
        self.screen = pygame.display.set_mode((self.width, self.height))

            self.bg_color = 0, 0, 0

        self.FPS = 30
        self.REFRESH = pygame.USEREVENT+1
        pygame.time.set_timer(self.REFRESH, 1000//self.FPS)


    def run(self):
        running = True
        while running:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                running = False

            elif event.type == self.REFRESH:
                self.draw()

            else:
                pass

    def draw(self):
        self.screen.fill(self.bg_color)

        pygame.display.flip()


MyGame().run()
pygame.quit()
sys.exit()

我一直在寻找一种方法来做到这一点,到目前为止没有运气,这就是我在这里问的原因

4

2 回答 2

0
from __future__ import division
import math
import sys
import pygame


class MyGame(object):
    def __init__(self):
        self.listofcolors = [[0, 0, 0], [255, 0, 0], [0, 255, 0],
                            [0, 0, 255], [255, 165, 0], [255, 255, 0],
                            [199, 97, 20]]
        pygame.mixer.init()
        pygame.mixer.pre_init(44100, -16, 2, 2048)
        pygame.init()

        self.width = 800
        self.height = 600
        self.screen = pygame.display.set_mode((self.width, self.height))

        self.bg_color = self.listofcolors[0]

        self.FPS = 30
        self.REFRESH = pygame.USEREVENT+1
        pygame.time.set_timer(self.REFRESH, 1000//self.FPS)


    def run(self):
        timer_cap = 30     # Frequenzy to change the background color
        cur_timer = 0      # Current timer to check against the timer_cap
        current_color = 0  # Current color index
        running = True
        while running:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                running = False

            elif event.type == self.REFRESH:
                self.draw()

            else:
                pass

            # Here we control the background color
            if cur_timer == timer_cap:
                cur_timer = 0
                if current_color == len(self.listofcolors)-1:
                    current_color = 0
                else:
                    current_color += 1
                self.bg_color = self.listofcolors[current_color] 
            else:
                cur_timer += 1

    def draw(self):
        self.screen.fill(self.bg_color)

        pygame.display.flip()


MyGame().run()
pygame.quit()
sys.exit()
于 2013-10-31T17:36:25.220 回答
0

你没有给我太多继续在这里,但我会试一试。

创建一个名为 framesSinceColourSwitch 的 int,一个将所有颜色存储在名为 backgroundColours 的数组,以及一个名为 currentColourIndex 的 int。

在你的代码中的某个地方,你正在刷新你的框架,重新绘制,重新绘制,类似的东西。重绘时向 framesSinceColourSwitch 添加一个。编写一个 if 语句来检查它何时达到 30。

在其中,您将背景颜色更改为 currentColourIndex 处的 backgroundColours 中的颜色。您还需要再次将 framesSinceColourSwitch 设置为 0 并将 1 添加到 currentColourIndex。您要做的最后一件事是通过背景数组中的颜色数修改 currentColourIndex。

我将所有细节留给你,因为我猜你还在学习,我不想让你慢下来。我也没有你的代码。

于 2013-10-31T16:20:48.313 回答