# controller.py # Walker White (wmw2), and Steve Marschner (srm2) # April 29, 2014 """Breakout-like Controller module for lecture demos.""" import colormodel import random import math from game2d import * ### CONSTANTS ### # Window Size WINDOW_WIDTH = 768 WINDOW_HEIGHT = 512 NUM_PARTICLES = 20 PSIZE = 4.0 BG_COLOR = colormodel.BLACK MAX_INIT_VEL = 20.0 # 60fps: 10.0 INIT_VX = 10.0 INIT_VY = 0.0 G = -3 # 60fps: -0.75 ### CONTROLLER CLASS ### class Controller(Game): """Demo controller class for CS1110 game2d framework. Instance Attributes: view: the view (inherited from Game) [GView] particles [list of Particle] """ # THREE MAIN METHODS def init(self): """Initialize the program state.""" self.particles = [] self.bg = GRectangle(x=0, y=0, width=self.width, height=self.height, fillcolor=BG_COLOR) def update(self,dt): """Animate a single frame. Called every 1/60 of a second.""" if self.view.touch is not None: for i in range(NUM_PARTICLES): p = Particle(self.view.touch.x, self.view.touch.y) self.particles.append(p) new_particles = [] for particle in self.particles: if particle.update(): new_particles.append(particle) self.particles = new_particles def draw(self): """Draw all particles in the view.""" self.bg.draw(self.view) for particle in self.particles: particle.draw(self.view) # HELPER METHODS ### MODEL CLASSES ### class Particle(GLine): """an ellipse with a velocity""" def __init__(self, x, y): color = colormodel.RGB(random.randint(0,255), random.randint(0,255), random.randint(0,255)) GLine.__init__(self, points=[x, y, x, y], linecolor=color) self.vx = random.uniform(-MAX_INIT_VEL, MAX_INIT_VEL) self.vy = random.uniform(-MAX_INIT_VEL, MAX_INIT_VEL) def update(self): """UYpdate particle, return True iff it is still alive""" x_new = self.points[2] y_new = self.points[3] x_newer = x_new + self.vx y_newer = y_new + self.vy self.points = [x_new, y_new, x_newer, y_newer] self.vy += G return y_new > 0 # Script Code if __name__ == '__main__': Controller(width=WINDOW_WIDTH,height=WINDOW_HEIGHT,fps=30.0).run()