def __init__(self, screen, switch_state_func): pg.init() pg.font.init() self.switch_state = switch_state_func self.screen = screen self.clock = pg.time.Clock() self.running = True self.char_buffer = [Character(' ', (0, 0), 3)] * 5 # Is this a separate game?? :) self.part_gen = ParticleGenerator(0, 0, 1, self.screen, rand_color())
def update(self): x, y = pg.mouse.get_pos() for event in pg.event.get(): if event.type == KEYDOWN: char = event.unicode.upper() if char == "" or char not in "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ": continue new_char = self.non_overlapping_character(char) self.update_char_buffer(new_char) if event.type == MOUSEBUTTONDOWN: self.part_gen = ParticleGenerator(x, y, 1, self.screen, rand_color()) if event.type == MOUSEBUTTONUP: self.part_gen = NullGenerator() self.fading_buffer = [c for c in self.fading_buffer if c.alpha] self.part_gen.update(x, y) self.check_quit()
class Letters(): def __init__(self, screen, switch_state_func): pg.init() pg.font.init() self.switch_state = switch_state_func self.screen = screen self.clock = pg.time.Clock() self.running = True self.char_buffer = [Character(' ', (0, 0), 3)] * 5 # Is this a separate game?? :) self.part_gen = ParticleGenerator(0, 0, 1, self.screen, rand_color()) def non_overlapping_character(self, char): """Return a character_object that does not overlap with any of the ones currently in the buffer.""" while True: # TODO: This loop might never terminate! size = randint(cons.MIN_LETTER_SIZE, cons.MAX_LETTER_SIZE) # Get a random position within the confines of the screen pos = rand_screen_pos(cons.WINDOW_WIDTH, cons.WINDOW_HEIGHT, size, size) new_char_object = Character(char, pos, size) # Chekc for overlapp index = new_char_object.get_rect().collidelist( self.buffer_to_rects()) if index == -1: break return new_char_object def update(self): x, y = pg.mouse.get_pos() self.part_gen.reposition(x, y) for event in pg.event.get(): if event.type == KEYDOWN: char = get_character(event.key) new_char = self.non_overlapping_character(char) self.update_char_buffer(new_char) self.char_buffer[0].fade_out() if event.type == MOUSEBUTTONDOWN: self.part_gen.active = True self.part_gen.set_color(rand_color()) if event.type == MOUSEBUTTONUP: self.part_gen.active = False self.part_gen.clear() self.part_gen.update() self.check_quit() def draw(self): self.screen.fill(pg.color.Color(cons.BACKGROUND_COLOR)) for letter in self.char_buffer: letter.draw(self.screen) self.part_gen.draw() pg.display.update() def buffer_to_string(self): return ''.join(str(char) for char in self.char_buffer) def buffer_to_rects(self): return [char.get_rect() for char in self.char_buffer] def update_char_buffer(self, new_char): self.char_buffer.append(new_char) self.char_buffer.pop(0) def check_quit(self): if self.buffer_to_string()[-4:] == 'QUIT': self.switch_state('MAINMENU')
def explode(self): def emitter(t): while True: dx = 2.*random.random()-1. dy = 2.*random.random()-1. dz = 2.*random.random()-1. r = dx*dx+dy*dy+dz*dz if r < 1.0 and r>0.5: d = (dx,dy,dz) return self._pos - (dx*3,dy*3,dz*3), d p = ParticleGenerator("billboard","particle4.png") p.setEmitter(emitter) p.setEvolveTime(1) p.setAcceleration(0) p.setSpawnSpeed(100) p.setEasing(0.5,0.8,0,0.5,0.5) p.setMode("ONCE") self._scene.getParticleManager().manageGenerator(p) self.close(self.DESTROYED)
def close(self): pg = ParticleGenerator("billboard","particle2.png") pg.setMode("ONCE") pg.setPosition(self._pos) self._scene.getParticleManager().manageGenerator(pg) self._closing = True
def __init__(self, scene, pos, direction, missile_type, follow): Entity.__init__(self) self._t0 = None self._follow = follow self._scene = scene self._map = scene.getMap() self.moveTo(pos) self._thrust = 0.01 self._speed = 0.1 self._destroyed = False self._graphics = R.loadObject(missile_type+".obj","diffuse") self._pos_m = T.translation_matrix(pos) axis = T.random_vector(3) self._d_rotation = T.quaternion_about_axis(0.01, axis) self._rotation = T.random_quaternion() self._dir = direction scale = 0.05 self._scaling_m = T.scale_matrix(scale) bounds = self._graphics.getObj().getBounds() * scale Entity.setBounds(self,bounds) self._trail= ParticleGenerator("billboard","puff.jpg") self._trail.setEvolveTime(0.5) self._trail.setAcceleration(0) self._trail.setEasing(0.5, 0.5, 0.0, 0.05, 0.2) self._trail.setBrightness(0.5) pg = scene.getParticleManager() def em(t): while True: dx = 2.*random.random()-1. dy = 2.*random.random()-1. dz = 2.*random.random()-1. r = dx*dx+dy*dy+dz*dz if r < 1.0: break return self._pos,(dx*0.2,dy*0.2,dz*0.2) self._trail.setEmitter(em) self._trail.setMode("DYNAMIC") pg.manageGenerator(self._trail) self._last_z = 0
class Missile(Entity): _gravity = N.array((0,0,-0.008)) def __init__(self, scene, pos, direction, missile_type, follow): Entity.__init__(self) self._t0 = None self._follow = follow self._scene = scene self._map = scene.getMap() self.moveTo(pos) self._thrust = 0.01 self._speed = 0.1 self._destroyed = False self._graphics = R.loadObject(missile_type+".obj","diffuse") self._pos_m = T.translation_matrix(pos) axis = T.random_vector(3) self._d_rotation = T.quaternion_about_axis(0.01, axis) self._rotation = T.random_quaternion() self._dir = direction scale = 0.05 self._scaling_m = T.scale_matrix(scale) bounds = self._graphics.getObj().getBounds() * scale Entity.setBounds(self,bounds) self._trail= ParticleGenerator("billboard","puff.jpg") self._trail.setEvolveTime(0.5) self._trail.setAcceleration(0) self._trail.setEasing(0.5, 0.5, 0.0, 0.05, 0.2) self._trail.setBrightness(0.5) pg = scene.getParticleManager() def em(t): while True: dx = 2.*random.random()-1. dy = 2.*random.random()-1. dz = 2.*random.random()-1. r = dx*dx+dy*dy+dz*dz if r < 1.0: break return self._pos,(dx*0.2,dy*0.2,dz*0.2) self._trail.setEmitter(em) self._trail.setMode("DYNAMIC") pg.manageGenerator(self._trail) self._last_z = 0 def destroy(self): self._destroyed = True Entity.destroy(self) if self._trail: self._trail.destroy() self._trail = None def explode(self): self.destroy() p = ParticleGenerator("billboard","particle3.png") p.setPosition(self._pos) p.setEvolveTime(0.5) p.setAcceleration(0) p.setSpawnSpeed(100) p.setMode("ONCE") self._scene.getParticleManager().manageGenerator(p) def checkColliders(self, colliders): hits = [] for c in colliders: if self.collidesWith(c): hits.append(c) return hits def update(self, time): if self._destroyed: return False if self._t0 is None: self._t0 = time if self._follow is not None and not self._follow.closing() : d = self._follow._pos - self._pos T.unit_vector(d,d) self._dir = self._dir * (1-self._thrust) + d * self._thrust if time - self._t0 > 3000 or self._follow._destroyed: print "Not following anymore" self._follow = None Entity.moveTo(self, self._pos + self._dir * self._speed) # also updates _pos else: # too simple to use Verlet Entity.moveTo(self, self._pos + self._dir * self._speed) # also updates _pos self._dir += self._gravity self._xform = mmult(self._pos_m,self._scaling_m, T.quaternion_matrix(self._rotation)) self._rotation = T.quaternion_multiply(self._d_rotation, self._rotation) # hit the floor if self._map(self._pos[0], self._pos[1], False) > self._pos[2]: self.explode() return False # tell the scene the missile is toast return True def draw(self, scene): self._graphics.draw(scene, self._xform)
def explode(self): self.destroy() p = ParticleGenerator("billboard","particle3.png") p.setPosition(self._pos) p.setEvolveTime(0.5) p.setAcceleration(0) p.setSpawnSpeed(100) p.setMode("ONCE") self._scene.getParticleManager().manageGenerator(p)