我怎样才能编写一个代码,让我的游戏每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()
我一直在寻找一种方法来做到这一点,到目前为止没有运气,这就是我在这里问的原因
答案 0 :(得分:0)
你在这里没有给我太多帮助,但我会试一试。
创建一个名为framesSinceColourSwitch的int,一个将所有颜色存储在被调用的backgroundColours中的数组,以及一个名为currentColourIndex的int。
在你的代码中的某个地方,你正在刷新你的框架,重新绘制,重绘,类似的东西。重绘时添加一个到framesSinceColourSwitch。写一个if语句来检查它何时到达30.
在其中,您将背景颜色更改为currentColourIndex中backgroundColours中的颜色。您还需要将framesSinceColourSwitch再次设置为0并将其添加到currentColourIndex。你要做的最后一件事是按照背景数组中的颜色数修改currentColourIndex。
我将所有细节留给你,因为我猜你还在学习,我不想让你慢下来。我也没有你的代码。
答案 1 :(得分: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()