Question
Modify the sample code so that the background color transitions through the sequence black, red, green, blue, orange, yellow, brown. The background color should change
Modify the sample code so that the background color transitions through the sequence black, red, green, blue, orange, yellow, brown. The background color should change once per second (every 30 frames)
"""Some simple skeleton code for a pygame game/animation
This skeleton sets up a basic 800x600 window, an event loop, and a redraw timer to redraw at 30 frames per second. """ from __future__ import division import math import sys import pygame import random
class MyGame(object): def __init__(self): """Initialize a new game""" pygame.mixer.init() pygame.mixer.pre_init(44100, -16, 2, 2048) pygame.init()
# set up a 640 x 480 window self.width = 800 self.height = 600 self.screen = pygame.display.set_mode((self.width, self.height))
# use a black background self.bg_colour = 0,0,0
# Setup a timer to refresh the display FPS times per second self.FPS = 30 self.REFRESH = pygame.USEREVENT+1 pygame.time.set_timer(self.REFRESH, 1000//self.FPS)
def run(self): """Loop forever processing events""" running = True while running: event = pygame.event.wait()
# player is asking to quit if event.type == pygame.QUIT: running = False
# time to draw a new frame elif event.type == self.REFRESH: self.draw()
else: pass # an event type we don't handle
def draw(self): """Update the display""" # everything we draw now is to a buffer that is not displayed self.screen.fill(self.bg_colour)
# flip buffers so that everything we have drawn gets displayed pygame.display.flip()
MyGame().run() pygame.quit() sys.exit()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started