def disconnect_sprite_from_body(sprite, body): end_x = body.position.x + body.linearVelocity.x end_y = body.position.y + body.linearVelocity.y end_rot = rad_to_deg(body.angle + body.angularVelocity) sprite.x = rabbyt.lerp(end=end_x, dt=1., extend='extrapolate') sprite.y = rabbyt.lerp(end=end_y, dt=1., extend='extrapolate') sprite.rot = rabbyt.lerp(end=end_rot, dt=1., extend='extrapolate')
def __init__(self, x, y): dst = 50 num = 10 dt = .6 fscale = 1 by = 360.0/num self.images = [] for i in range(num): ang = i*by rad = ang / 180.0 * math.pi s = simage.SImage('wedge.png', x, y) s.sp.x = rabbyt.lerp(end=math.cos(rad)*dst*fscale+x, dt=dt) s.sp.y = rabbyt.lerp(end=math.sin(rad)*dst*fscale+y, dt=dt) '''cool things: #1 s.sp.rot = ang - 90 #2 s.sp.rot = ang #3 s.sp.rot = ang + 90 ''' s.sp.rot = rabbyt.lerp(ang, ang - 90.0, dt=dt/2) s.sp.rot = rabbyt.lerp(ang + 90, ang - 90.0, dt=dt) #s.sp.rot = ang - 90.0 s.sp.scale = rabbyt.lerp(0,fscale,dt=dt) self.images.append(s) self.on = True def tmp(dt): l = rabbyt.lerp(1.0,0.0,dt=dt) for i in self.images: i.sp.alpha = l#rabbyt.lerp(1.0,0.0,dt=1) clock.schedule_once(self.off, dt) clock.schedule_once(tmp, dt/2)
def do_score(self): # First we find all rows of the same gem type that this gem is in that # is (by default) at least three long. row = self.client.game.find_gems_row(self.grid_xy, self.id) if row: # As there is a row(s) we need to add self to it as the # find_gems_row function only checks and returns gems NEXT to this # one. row.add(self) else: return False # Ok, it is in a row! Now let's take each gem amd remove it. This is # where you would also put the scoring stuff - but for this example # we don't implement it. for gem in row: self.client.game.remove_gem(gem) # We don't want to remove this gem from the gui right away because # it would stop being drawn immediately - and we want to do some # cool fading effects :D # So we schedule with pyglet's clock to remove it is .7 seconds. clock.schedule_once(lambda dt:(self.client.gui.remove(gem)), .7) # And now we apply our fading and scaling effects... in exactly two # lines... gem.scale = rabbyt.lerp(end=0, startt=self.client.time+.5, dt=.2) gem.alpha = rabbyt.lerp(end=0, startt=self.client.time+.2, dt=.5) # HOLY COW THAT IS SO SIMPLE!1!! OMG!!!!1!1!11one! # For learning how rabbyt anims work and all you can do with them, # check out the online documentation for them at rabbyt's website: # http://matthewmarshall.org/projects/rabbyt/docs/rabbyt/anims/ # That whole page is filled with all the awesomeness you can do # with the rabbyt anims. return True
def __init__(self, screen, start_x, start_y): rabbyt.Sprite.__init__(self, '1energy.png') self.x = start_x end_y = start_y - 800 self.y = rabbyt.lerp(start_y, end_y, dt=3) self.rot = rabbyt.lerp(0, 180, dt=2, extend="extrapolate") self.time_last = pygame.time.get_ticks() self.screen = screen
def make_shape_animations(self): x, y = get_xy_positions(self.win.width, self.win.height) s = SImage(get_random_image(), x, y) s.sp.x = rabbyt.lerp(end=random.uniform(0, self.win.width), dt=1) s.sp.y = rabbyt.lerp(end=random.uniform(0, self.win.height), dt=1) s.sp.rot = rabbyt.lerp(start=0, end=360, dt=1) s.sp.scale = rabbyt.lerp(.25, 1, dt=1) self.world.objects.append(s) clock.schedule_once(lambda dt:self.world.objects.remove(s), 1)
def make_shape_animations(self): x, y = get_xy_positions(self.win.width, self.win.height) s = SImage(get_random_image(), x, y) s.sp.x = rabbyt.lerp(end=random.uniform(0, self.win.width), dt=1) s.sp.y = rabbyt.lerp(end=random.uniform(0, self.win.height), dt=1) s.sp.rot = rabbyt.lerp(start=0, end=360, dt=1) s.sp.scale = rabbyt.lerp(.25, 1, dt=1) self.world.objects.append(s) clock.schedule_once(lambda dt: self.world.objects.remove(s), 1)
def lerp ( start = None, end = None, ** kws ) : assert end != None if kws [ 'dt' ] > TOLERANCE : if start == None : return rabbyt.lerp ( end = end, ** kws ) else : return rabbyt.lerp ( start, end, ** kws ) else : return end
def sleep(self): self.blackout.alpha = chain( lerp(end=1, dt=3), lerp(end=0, dt=3)) for crew in self.model.crew: crew.update(100) self.model.player.health_shrink(100) self.model.player.sleep = False
def g(): x = random.randrange(w) y = random.randrange(h) sprite = rabbyt.Sprite(DAT_IMG) sprite.shape.width = 64 sprite.shape.height = 64 # TODO: Not exact! #sprite.tex_shape.width = 1.0 #sprite.tex_shape.width = 32.0/64.0 #sprite.tex_shape.height = 0.5 #sprite.tex_shape.width = 32.0/64.0 #sprite.tex_shape.height = 1.0/5.0 # So, who knows what the hell these values are actually being set to, or rather, why... # What I do know is that we can do everything as multiples of these crazy values... sprite.lol_w = sprite.tex_shape.width sprite.lol_h = sprite.tex_shape.height # Dividing by the number of frames in the sprite sheet sprite.tex_shape.width = sprite.lol_w/2.0 sprite.tex_shape.height = sprite.lol_h/1.0 sprite.tex_shape.left = 0 sprite.tex_shape.bottom = 0 # TODO: WHY DOES THIS WORK #sprite.u = rabbyt.lerp(0, 1.0/4.0, dt=4, extend="constant") sprite.xy = (x, y) #sprite.scale = rabbyt.ease(0.25, 1.50, dt=1, extend="reverse") sprite.scale = rabbyt.ease(0.75, 1.25, dt=2, extend="reverse") sprite.rot = rabbyt.ease(0.00, 360.00, dt=8, extend="extrapolate") #sprite.rot = 45 sprite.x = rabbyt.ease_out(x-50, x+50, dt=6, extend="reverse") sprite.y = rabbyt.ease_in( y+50, y-50, dt=6, extend="reverse") rgb1 = (1.0, 0.0, 0.0) rgb2 = (0.0, 1.0, 0.0) rgb3 = (0.0, 0.0, 1.0) #sprite.rgb = rabbyt.chain( # rabbyt.lerp(rgb1, rgb2, dt=5), # rabbyt.lerp(rgb2, rgb3, dt=5, extend="reverse") #) sprite.red = rabbyt.lerp(0.50, 1.00, dt=2, extend="reverse") sprite.green = rabbyt.lerp(0.50, 1.00, dt=4, extend="reverse") sprite.blue = rabbyt.lerp(0.50, 1.00, dt=8, extend="reverse") sprite.alpha = rabbyt.lerp(0.25, 0.75, dt=1, extend="reverse") return sprite
def update(self, dt): #checks screen bounds and reflects if self.x > 1200 or self.x < 0: self.vector.norm_x = -self.vector.norm_x self.rgb = lerp((1, 0, 0), (1, 1, 1), dt=.4) if self.y > 800 or self.y < 0: self.vector.norm_y = -self.vector.norm_y self.rgb = lerp((1, 0, 0), (1, 1, 1), dt=.4) self.x = self.x + self.vector.speed * self.vector.norm_x * dt self.y = self.y + self.vector.speed * self.vector.norm_y * dt
def get_rotation(choice, ang, dt): if choice == 1: return ang - 90 elif choice == 2: return ang elif choice == 3: return ang + 90 elif choice == 4: return rabbyt.lerp(ang, ang - 90.0, dt=dt/2) else: return rabbyt.lerp(ang + 90, ang - 90.0, dt=dt/2)
def update(self,dt): (angle,speed) = self.vector if self.x > 1200 or self.x < 0: self.vector = [180*(math.pi/180)-angle,speed] self.rgb = lerp((1,0,0),(1,1,1), dt=.4) if self.y > 800 or self.y < 0: self.vector = [-angle,speed] self.rgb = lerp((1,0,0),(1,1,1), dt=.4) (angle,speed) = self.vector self.x = self.x + speed*math.cos(angle)*dt self.y = self.y + speed*math.sin(angle)*dt
def update(self,dt): #checks screen bounds and reflects if self.x > 1200 or self.x < 0: self.vector.norm_x = -self.vector.norm_x self.rgb = lerp((1,0,0),(1,1,1), dt=.4) if self.y > 800 or self.y < 0: self.vector.norm_y = -self.vector.norm_y self.rgb = lerp((1,0,0),(1,1,1), dt=.4) self.x = self.x + self.vector.speed*self.vector.norm_x*dt self.y = self.y + self.vector.speed*self.vector.norm_y*dt
def run(self, game, state_stack): """Makes the Cutscene run""" game.set_state_time() scene = rabbyt.Sprite("1cutscene.png") scene.scale = 0.95 scene.y = -60 scene.alpha = rabbyt.lerp(0.0, 0.8, startt=1, endt=8) scene.x = rabbyt.lerp(-20, 60, startt=1, endt=6) scene2 = rabbyt.Sprite("1cutscene2.png") scene2.alpha = rabbyt.lerp(0.8, 0.0, startt=6, endt=13) scene2.scale = 0.95 scene2.xy = (60, -60) words = FontSprite(game.font, "Scientist: I'll be back soon.") words.xy = (-195, -250) #set music pygame.mixer.music.stop() pygame.mixer.music.load('scene1.wav') pygame.mixer.music.play() game.done = False while not game.done: rabbyt.clear() rabbyt.set_time(game.get_ticks()/1000.0) ticks = game.get_ticks()/1000.0 if ticks < 6: scene.render() if ticks >= 6: scene2.render() if ticks >= 14: game.done = True state_stack.append(states.name.Name()) words.render() for event in pygame.event.get(): if event.type == QUIT: game.done = True fdata = open("RabbitHighScores", 'w') for i in range(5): fdata.write(game.high_score_names[i] + " " \ + str(game.high_scores[i]) + "\n") if event.type == KEYDOWN: if event.key == K_ESCAPE or event.key == K_RETURN: game.done = True state_stack.append(states.name.Name()) elif event.type == pygame.JOYBUTTONDOWN and game.joystick.get_button(game.controls.settings["Escape"]): game.done = True state_stack.append(states.name.Name()) pygame.display.flip()
def make_string(self, string): x, y = get_xy_positions(self.win.width, self.win.height) s = SpriteText(self.ft, string) s.rgba = rcolor() s.x = rabbyt.lerp(x, random.uniform(0, self.win.width), dt=1) s.y = rabbyt.lerp(y, random.uniform(0, self.win.height), dt=1) s.rot = rabbyt.lerp(start=0, end=360, dt=1) s.scale = rabbyt.lerp(.5, 1, dt=1) self.world.objects.append(s) def tmp(dt): s.alpha = rabbyt.lerp(1.0, 0, dt=2) clock.schedule_once(lambda dt:self.world.objects.remove(s),2) clock.schedule_once(tmp, 2)
def g(): for i in xrange(num_sprites): x = random.randrange(0, 1024) y = random.randrange(0, 576) #sprite = rabbyt.Sprite("player.png") #sprite = rabbyt.Sprite("player.png") sprite = rabbyt.Sprite(DAT_IMG) sprite.xy = (x, y) sprite.x = rabbyt.lerp(x-100, x+100, dt=2, extend="reverse") sprite.y = rabbyt.lerp(y-100, y+100, dt=2, extend="reverse") sprite.rot = rabbyt.lerp(0,360, dt=10, extend="extrapolate") sprite.rgb = rabbyt.lerp((1,0.5,0.5), (0.5,1,0.5), dt=2, extend="reverse") ##sprite.alpha = rabbyt.lerp(0, 1, dt=0.25, extend="reverse") sprite.alpha = rabbyt.lerp(0.25, 1, dt=1, extend="reverse") yield sprite
def make_string(self, string): x, y = get_xy_positions(self.win.width, self.win.height) s = SpriteText(self.ft, string) s.rgba = rcolor() s.x = rabbyt.lerp(x, random.uniform(0, self.win.width), dt=1) s.y = rabbyt.lerp(y, random.uniform(0, self.win.height), dt=1) s.rot = rabbyt.lerp(start=0, end=360, dt=1) s.scale = rabbyt.lerp(.5, 1, dt=1) self.world.objects.append(s) def tmp(dt): s.alpha = rabbyt.lerp(1.0, 0, dt=2) clock.schedule_once(lambda dt: self.world.objects.remove(s), 2) clock.schedule_once(tmp, 2)
def on_mouse_press(x, y, b, m): if len(bullets_my) < 3: bullet = rabbyt.Sprite("bullet.png") bullet.x = gun.x bullet.y = rabbyt.lerp(gun.y+gun.texture.height/2+bullet.texture.height/2, win.height+10, dt=2.0) bullets_my.append(bullet)
def run(queue): running = True game = Game(1240, 780) while running: clock.tick(40) for event in pygame.event.get(): running &= game.handle_event(event) try: item = queue.get(False) update = simplejson.loads(item) for (key, value) in update.iteritems(): print Piece._items[update['id']], key, value current = getattr(Piece._items[update['id']], key) setattr(Piece._items[update['id']], key, rabbyt.lerp(current, value, dt=200)) except Empty: pass #game.render() rabbyt.set_time(pygame.time.get_ticks()) rabbyt.clear() rabbyt.render_unsorted(game.boards) rabbyt.render_unsorted(game.pieces) rabbyt.scheduler.pump() pygame.display.flip()
def do_fade(self, start_rgba, end_rgba): snowui.Button.do_fade(self, start_rgba, end_rgba) if self.is_hovering: # We are fading out. self.rot = rabbyt.lerp(end=0, dt=.2) else: self.rot = rabbyt.ease_out(end=-180, dt=1, extend="repeat", method="bounce")
def shoot(dt): if aliens: shooter = random.choice(aliens) bullet = rabbyt.Sprite("bullet-alien.png") bullet.x = shooter.x bullet.rot = 180 bullet.y = rabbyt.lerp(shooter.y-shooter.texture.height/2-bullet.texture.height/2, -10, dt=2.0) bullets_alien.append(bullet)
def update(dt): collisions = rabbyt.collisions.aabb_collide(points) for group in collisions: for s in group: s.x_dir = -s.x_dir s.y_dir = -s.y_dir s.rgb = lerp((0,1,0),(1,1,1), dt=.4) for i in points: i.update(dt) return False
def update(dt): collisions = rabbyt.collisions.aabb_collide(points) for group in collisions: for s in group: s.x_dir = -s.x_dir s.y_dir = -s.y_dir s.rgb = lerp((0, 1, 0), (1, 1, 1), dt=.4) for i in points: i.update(dt) return False
def update(self, dt): if self.x > 1200 or self.x < 0: self.x_dir = -self.x_dir self.rgb = lerp((1, 0, 0), (1, 1, 1), dt=.4) if self.y > 800 or self.y < 0: self.y_dir = -self.y_dir #self.x_dir = math.cos(self.direction) #self.y_dir = math.sin(self.direction) self.speed += self.acceleration * dt self.x = self.x + self.speed * self.x_dir * dt self.y = self.y + self.speed * self.y_dir * dt
def update(dt): collisions = rabbyt.collisions.brute_force(points) for group in collisions: try: new_vectors = elastic(group[0], group[1]) group[0].rgb = lerp((0, 1, 0), (1, 1, 1), dt=.4) group[1].rgb = lerp((0, 1, 0), (1, 1, 1), dt=.4) group[0].vector = new_vectors[0] group[1].vector = new_vectors[1] except UnboundLocalError: if group[0].y > group[1].y: group[0].y += 2 group[1].y -= 2 else: group[1].y += 2 group[0].y -= 2 for i in points: i.update(dt) return False
def update(dt): collisions = rabbyt.collisions.brute_force(points) for group in collisions: try: new_vectors = elastic(group[0],group[1]) group[0].rgb = lerp((0,1,0),(1,1,1), dt=.4) group[1].rgb = lerp((0,1,0),(1,1,1), dt=.4) group[0].vector = new_vectors[0] group[1].vector = new_vectors[1] except UnboundLocalError: if group[0].y > group[1].y: group[0].y += 2 group[1].y -= 2 else: group[1].y += 2 group[0].y -= 2 for i in points: i.update(dt) return False
def update(self,dt): if self.x > 1200 or self.x < 0: self.x_dir = -self.x_dir self.rgb = lerp((1,0,0),(1,1,1), dt=.4) if self.y > 800 or self.y < 0: self.y_dir = -self.y_dir #self.x_dir = math.cos(self.direction) #self.y_dir = math.sin(self.direction) self.speed += self.acceleration*dt self.x = self.x + self.speed*self.x_dir*dt self.y = self.y + self.speed*self.y_dir*dt
def create_dust_particle(self, offset): s = rabbyt.Sprite("star.png") lifetime = 4 x, y = self.convert_offset(offset) r = random.random # (shortcut) s.x = rabbyt.lerp(x+r()*10-5, x+r()*60-30, dt=lifetime) s.y = rabbyt.lerp(y+r()*10-5, y+r()*60-30, dt=lifetime) s.rot = rabbyt.lerp(0, 90*random.choice(range(-2,3)), dt=6) s.scale = rabbyt.lerp(1, 4, dt=lifetime) now = rabbyt.get_time() s.rgba = .7, .5, 0, rabbyt.lerp(.1, 0, now+lifetime/2, now+lifetime) Car.dust_particles.add(s) rabbyt.scheduler.add(rabbyt.get_time()+lifetime, lambda:Car.dust_particles.remove(s))
def do_fade(self, start_rgba, end_rgba): """ ``do_fade(start_rgba, end_rgba)`` Uses ``rabbyt.lerp`` to fade the button's color from ``start_rgba`` to ``end_rgba``. This is not meant to be called directly but is separated out here so you can overwrite it to easily have your own effects. """ self.rgba = rabbyt.lerp(start=start_rgba, end=end_rgba, dt=self.fade_time)
def g(): x = random.randrange(w) y = random.randrange(h) sprite = rabbyt.Sprite(DAT_IMG) sprite.shape.width = 32 sprite.shape.height = 32 # TODO: Not exact! #sprite.tex_shape.width = 1.0/3.0/3.0 sprite.tex_shape.width = 1.0/8.0 sprite.tex_shape.height = 1.0/8.0 sprite.tex_shape.left = 0 sprite.tex_shape.bottom = 0 # TODO: WHY DOES THIS WORK #sprite.u = rabbyt.lerp(0, 1.0/4.0, dt=4, extend="constant") sprite.xy = (x, y) sprite.scale = rabbyt.ease(0.25, 1.50, dt=1, extend="reverse") sprite.rot = rabbyt.ease(0.00, 360.00, dt=1, extend="extrapolate") sprite.x = rabbyt.ease_out(x-50, x+50, dt=1, extend="reverse") sprite.y = rabbyt.ease_in( y+50, y-50, dt=1, extend="reverse") rgb1 = (1.0, 0.0, 0.0) rgb2 = (0.0, 1.0, 0.0) rgb3 = (0.0, 0.0, 1.0) #sprite.rgb = rabbyt.chain( # rabbyt.lerp(rgb1, rgb2, dt=5), # rabbyt.lerp(rgb2, rgb3, dt=5, extend="reverse") #) sprite.red = rabbyt.lerp(0.50, 1.00, dt=2, extend="reverse") sprite.green = rabbyt.lerp(0.50, 1.00, dt=4, extend="reverse") sprite.blue = rabbyt.lerp(0.50, 1.00, dt=8, extend="reverse") sprite.alpha = rabbyt.lerp(0.25, 0.75, dt=1, extend="reverse") return sprite
def create_boost_particle(self): s = rabbyt.Sprite(self.texture_id, self.shape) lifetime = .5 s.xy = self.xy s.rot = self.rot s.scale = rabbyt.lerp(1, 2, dt=lifetime) s.alpha = rabbyt.lerp(.8, 0, dt=lifetime) Car.boost_particles.add(s) rabbyt.scheduler.add(rabbyt.get_time()+lifetime, lambda:Car.boost_particles.remove(s)) lt = .8 star = rabbyt.Sprite("star2.png") x = random.random()*80-40 y = random.random()*80-40 star.x = rabbyt.lerp(self.x+x, self.convert_offset((-20,0))[0]+x, dt=lt) star.y = rabbyt.lerp(self.y+y, self.convert_offset((-20,0))[1]+y, dt=lt) star.rot = rabbyt.lerp(0, 190*random.choice([-2,-1,1,2]), dt=5, extend="extrapolate") star.scale = rabbyt.lerp(random.random()+.2,0, rabbyt.get_time()+lt/2, dt=lt/2) star.rgb = 0, .5, .9 Car.boost_particles.add(star) rabbyt.scheduler.add(rabbyt.get_time()+lt, lambda:Car.boost_particles.remove(star))
def __init__(self, start, angle, speed): rabbyt.Sprite.__init__(self, "1rock_bullet.png") game_object.GameObject.__init__(self) self.xy = start self.rot = angle self.speed = speed self.bounding_radius = 16 self.health = 1 self.damage = 1 x_end_position = cos(radians(self.rot + 90)) * 1000 + start[0] y_end_position = sin(radians(self.rot + 90)) * 1000 + start[1] self.xy = rabbyt.lerp(start, (x_end_position, y_end_position), dt=speed)
def on_mouse_press(x, y, b, m): # Check the chimp and hand positions if rabbyt.collisions.aabb_collide([chimp, fist]): # Play some sound sound.play() # Make the chimp angry chimp.texture = 'ca.png' # Make the chimp go faster! global dt dt /= 1.5 chimp.x = rabbyt.lerp(50, 750, dt=dt, extend="reverse")
def update(self,dt): #checks screen bounds and reflects if self.x > 1200 or self.x < 0: self.vector.norm_x = -self.vector.norm_x self.rgb = lerp((1,0,0),(1,1,1), dt=.4) if self.y < 0: self.off_screen = True self.x = self.x + self.vector.speed*self.vector.norm_x*dt self.y = self.y + self.vector.speed*self.vector.norm_y*dt if self.vector.speed < 150: self.vector.speed += self.acceleration*dt
def update(dt): # Change the direction of the aliens if aliens: if aliens[0].x-aliens[0].texture.width/2 < 0: print "going ---->" for i, alien in enumerate(aliens): alien.x = rabbyt.lerp(alien.x, win.width+alien.x, dt=5.0) alien.y = rabbyt.lerp(alien.y, alien.y-alien.texture.height/2, dt=0.5) if aliens[-1].x+aliens[-1].texture.width/2 > win.width: print "going <---" for i, alien in enumerate(aliens): alien.x = rabbyt.lerp(alien.x, -win.width+alien.x, dt=5.0) alien.y = rabbyt.lerp(alien.y, alien.y-alien.texture.height/2, dt=0.5) else: print "You win man!" # Check for collisions for a, b in rabbyt.collisions.aabb_collide([gun]+bullets_alien): if a is gun or b is gun: print "Game over man!" # Game over man! gun.alpha = 0 for a, b in rabbyt.collisions.aabb_collide(aliens+bullets_my): if b in aliens: bullets_my.remove(a) aliens.remove(b) if a in aliens: bullets_my.remove(b) aliens.remove(a) # Clean up any bullets which have left the screen for bullets in bullets_my, bullets_alien: for bullet in list(bullets): if bullet.y > win.height: bullets.remove(bullet) if bullet.y < 0: bullets.remove(bullet)
def on_mouse_press(x, y, b, m): # Check the chimp and hand positions if rabbyt.collisions.aabb_collide([chimp, fist]): # Play some sound sound.play() # Make the chimp angry chimp.texture = 'ca.png' # Stop the chimp chimp.x = float(chimp.x) # Spin the chimp! chimp.rot = rabbyt.lerp(0, 360, dt=1.3)
def __init__(self, x, y): dst = 50 num = 10 dt = .6 fscale = 1 by = 360.0/num choice = random.randint(1, 5) self.images = [] for i in range(num): ang = i*by rad = ang / 180.0 * math.pi s = SImage('res/wedge.png', x, y) s.sp.x = rabbyt.lerp(end=math.cos(rad)*dst*fscale+x, dt=dt) s.sp.y = rabbyt.lerp(end=math.sin(rad)*dst*fscale+y, dt=dt) s.sp.rot = get_rotation(choice, ang, dt) s.sp.scale = rabbyt.lerp(0,fscale,dt=dt) self.images.append(s) self.on = True def tmp(dt): l = rabbyt.lerp(1.0,0.0,dt=dt) for i in self.images: i.sp.alpha = l#rabbyt.lerp(1.0,0.0,dt=1) clock.schedule_once(self.off, dt) clock.schedule_once(tmp, dt/2)
def __init__(self, **kwargs): super(MyWindow, self).__init__(**kwargs) self.set_exclusive_mouse(self.fullscreen) self.set_exclusive_keyboard(self.fullscreen) rabbyt.set_default_attribs() glClearColor(1, 1, 1, 0) self.background = load_tileable_texture('background.png') self.ship_texture = pyglet.resource.texture('ship.png') self.ship = rabbyt.Sprite(self.ship_texture, alpha=rabbyt.ease(0, 1, dt=2), rot=rabbyt.lerp(end=60, dt=1, extend='extrapolate')) self.shadow = create_shadow(self.ship, self.ship_texture, x=20, y=-30, alpha=0.5) self.time = 0.
def update(self, dt): if self.weapon[-1].__class__ != BasicTurret: self.weapon_timer -= dt if self.weapon_timer <= 0.0: self.swap_weapon(0) if not self.alive: self.weapon[-1].disengage() self.weapon[-1].update(dt) return if self.invuln > 0: self.invuln -= dt self.vel_target.zero() if self.gamepad: # we might want to move this into the pyglet event loop self.gamepad.update() self.vel_target.x = self.gamepad.axis[0] self.vel_target.y = -self.gamepad.axis[1] if not self.mouse_engaged: if self.gamepad.axis[2] or self.gamepad.axis[3]: if not self.weapon[-1].engaged: self.weapon[-1].engage() rel_target = vector.Vec2d( self.gamepad.axis[2], -self.gamepad.axis[3]).normal * 200 self.target = self.pos + rel_target else: self.weapon[-1].disengage() if self.keys[pyglet.window.key.A]: if self.keys[pyglet.window.key.W]: self.vel_target.y = 0.7071 self.vel_target.x = -0.7071 elif self.keys[pyglet.window.key.S]: self.vel_target.y = -0.7071 self.vel_target.x = -0.7071 else: self.vel_target.x = -1.0 elif self.keys[pyglet.window.key.D]: if self.keys[pyglet.window.key.W]: self.vel_target.y = 0.7071 self.vel_target.x = 0.7071 elif self.keys[pyglet.window.key.S]: self.vel_target.y = -0.7071 self.vel_target.x = 0.7071 else: self.vel_target.x = 1.0 else: if self.keys[pyglet.window.key.W]: self.vel_target.y = 1.0 elif self.keys[pyglet.window.key.S]: self.vel_target.y = -1.0 else: pass # normalize target velocity to length 1 if self.vel_target.magnitude_sq > 1: self.vel_target.normalize() self.vel_target *= self.speed # smooth velocity changes self.vel += (self.vel_target - self.vel) * self.__class__.vel_smooth if self.weapon[-1].__class__ == FireHose and self.weapon[-1].engaged: norm = (self.target - self.pos).normal self.vel -= norm * 100 self.rot = -norm.angle else: # modify rotation self.rot = -self.vel.angle # do regular euler updates self.pos += self.vel * dt #self.sprite.xy = self.pos.x, self.pos.y # # update turrets # rot = -(self.target - self.pos).angle # for s in self.sprites[1:]: # s.rot = rot obj.CompoundGameObject.update(self, dt) self.weapon[-1].update(dt, self.pos, self.target) self.trail_acc -= dt if self.trail_acc <= 0: self.trail_acc = 0.05 self.trail_counter = (self.trail_counter + 1) % len(self.trail) self.trail[self.trail_counter].xy = self.sprites[0].xy self.trail[self.trail_counter].alpha = rabbyt.lerp(0.3, 0, dt=0.8)
rabbyt.data_directory = os.path.dirname(__file__) pygame.init() pygame.display.set_mode((800, 480), pygame.OPENGL | pygame.DOUBLEBUF) rabbyt.set_viewport((800, 480)) rabbyt.set_default_attribs() font = Font(pygame.font.Font(pygame.font.get_default_font(), 20)) sprites = [] sprites.append(FontSprite(font, "lerp", x=-275, y=480/2-10)) sprites.append(rabbyt.Sprite(x=-275, y=480/2-50)) sprites.append(rabbyt.Sprite(x=-125, y=480/2-50)) s = rabbyt.Sprite() s.x = rabbyt.lerp(-275, -125, dt=3, extend="repeat") s.y = 480/2-50 sprites.append(s) sprites.append(FontSprite(font, "ease", x=-275, y=480/2-70)) sprites.append(FontSprite(font, "ease_out", x=-20, y=480/2-70)) sprites.append(FontSprite(font, "ease_in", x=235, y=480/2-70)) methods = ["sine", "quad", "cubic", "circ", "back", "bounce"] ypos = 480/2-100 for method in methods: sprites.append(FontSprite(font, method, x=-390, y=ypos)) for func, start, end in [(rabbyt.ease, -275, -125), (rabbyt.ease_out, -20, 130), (rabbyt.ease_in, 235, 385)]:
def add(self, x, y): s = SImageStatic('res/ring.png', x, y) s.sp.scale = rabbyt.lerp(start=.1, end=2, dt=1) s.sp.alpha = rabbyt.lerp(end=0, dt=1) self.world.objects.append(s) clock.schedule_once(lambda dt: self.world.objects.remove(s), 1)
def tmp(dt): s.alpha = rabbyt.lerp(1.0, 0, dt=2) clock.schedule_once(lambda dt: self.world.objects.remove(s), 2)
def drop_gems(self): # We need to keep track of all gems that have moved so we can detect # chain reaction scores. moved_gems = set() # Here we go through all the columns to see if they need to be dropped. for k,row in self.empty_slots.items(): if not row: # This row is devoid of emptiness. Skip it. continue # We sort the row so that the bottom most empty slot on this column # is first in the list. The row is structured with x being the key # and the value being a list of all the empty coordinates on the # board that need filled. row.sort(key=lambda obj:obj[1]) # We are only interested in the bottom most one. empty_xy = row[0] # We are going to start at the bottom most one and move the next # closest gem that is above it to be right on top of it. We then # work our way up until we have filled all empty spots in this # column (or row as the variable calls it). while True: if not self.gems.has_key(empty_xy): # Oops, we passed the top! This is it for this row. break if self.gems[empty_xy] is None: # This spot is empty so let's drop the closest one above it! # First step is to find it. ng = self.find_next_gem_up(empty_xy) # Move the gem we are dropping. Notice that where the ng gem # used to be it is now empty. That is one reason we work our # way up and not down! self.gems[ng.grid_xy] = None ng.grid_xy = empty_xy self.gems[empty_xy] = ng # Move the gem vissually. Above we just moved it in the # game's gem store. ng.y = rabbyt.lerp(end=ng.grid_y*62.5+52+26, dt=.6) moved_gems.add(ng) # Now we prepare for the next loop to check the slot about this # one. empty_xy = (empty_xy[0], empty_xy[1]+1) # This row is full! self.empty_slots[k] = [] # Don't be thrown off by this; we are still in the drop_gems function! # This is what's called a factory. This is probably one of the most # confusing places. What we are doing here is waiting until all our gems # have dropped and then checking to see if there are any chain reaction # scoring. If there is we drop it all over again. def _score(dt): _s = False for g in moved_gems: if g.do_score(): _s = True if _s: clock.schedule_once(lambda dt:(self.drop_gems()), .4) clock.schedule_once(_score, .6)
def fade_in(self): dt = self.fade_dt * (1. - self.sprite.alpha) self.sprite.alpha = rabbyt.lerp(end=1., dt=dt)
rabbyt.data_directory = os.path.dirname(__file__) pygame.init() pygame.display.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF) rabbyt.set_viewport((640, 480)) rabbyt.set_default_attribs() sprites = [] r = lambda: random.random() - .5 for i in range(2400): s = rabbyt.Sprite("rounded_square.png") s.rgba = lerp((.5, .2, 1, .2), (0, .8, 0, .6), dt=3 * r() + 2, extend="reverse") s.x = wrap([-320, 320], lerp(r() * 640, r() * 640, dt=2, extend="extrapolate")) s.y = wrap([-240, 240], lerp(r() * 480, r() * 480, dt=2, extend="extrapolate")) s.scale = lerp(.1, 1, dt=r() + .75, extend="reverse") s.rot = lerp(0, 360, dt=2, extend="extrapolate") sprites.append(s) print "Drawing 2400 sprites..."
import pygame pygame.init() pygame.display.set_mode((800, 600), pygame.OPENGL | pygame.DOUBLEBUF) rabbyt.set_viewport((800, 600)) rabbyt.set_default_attribs() sprites = [] r = lambda: random.random() - .5 for i in range(400): s = rabbyt.Sprite(shape=(-2, 2, 2, -2)) s.x = wrap([-400, 400], lerp(r() * 800, r() * 800, dt=4, extend="extrapolate")) s.y = wrap([-300, 300], lerp(r() * 600, r() * 600, dt=4, extend="extrapolate")) sprites.append(s) collision_times = [] c = pygame.time.Clock() last_fps = 0 while not pygame.event.get(pygame.QUIT): c.tick() if pygame.time.get_ticks() - last_fps > 1000: print "FPS: ", c.get_fps() last_fps = pygame.time.get_ticks() if collision_times: average = sum(collision_times) / len(collision_times)
rabbyt.set_default_attribs() class SpriteText(rabbyt.BaseSprite): def __init__(self, ft, text="", *args, **kwargs): rabbyt.BaseSprite.__init__(self, *args, **kwargs) self._text = font.Text(ft, text) def set_text(self, text): self._text.text = text def render_after_transform(self): self._text.color = self.rgba self._text.draw() ft = font.load('Arial', 24) sprite = SpriteText(ft, "Hello World", xy=(320, 240)) sprite.rot = rabbyt.lerp(0, 360, dt=5, extend="extrapolate") sprite.rgb = rabbyt.lerp((1, 0, 0), (0, 1, 0), dt=2, extend="reverse") while not window.has_exit: clock.tick() window.dispatch_events() rabbyt.clear() sprite.render() window.flip()
# But note that all times will be in *seconds*, not milliseconds like when we # use pygame.time.get_ticks(). clock.schedule(rabbyt.add_time) window = Window(width=640, height=480) rabbyt.set_default_attribs() car = rabbyt.Sprite("car.png") # Rabbyt automatically detected that we are using pyglet, and used pyglet # to load the texture. assert isinstance(car.texture, image.Texture) car.xy = (320, 240) # Fade the car in after one second. car.alpha = rabbyt.lerp(0.0, 1.0, startt=1, endt=2) # Rotate the car from 0 to 360 over three seconds, then repeat. car.rot = rabbyt.lerp(0, 360, dt=3, extend="repeat") while not window.has_exit: clock.tick() window.dispatch_events() rabbyt.clear((1, 1, 1)) car.render() window.flip()
def fade_out(self): dt = self.fade_dt * self.sprite.alpha self.sprite.alpha = rabbyt.lerp(end=0., dt=self.fade_dt)
l, t, r, b = self.font.get_char_tex_shape(char) self.char_sprites.append( rabbyt.Sprite(texture=self.texture_id, shape=(x, 0, x + w, -h), tex_shape=self.font.get_char_tex_shape(char), rgba=self.attrgetter('rgba'))) x += w + 1 text = property(_get_text, _set_text, doc="the text to be displayed") def render_after_transform(self): rabbyt.render_unsorted(self.char_sprites) if __name__ == "__main__": pygame.init() pygame.display.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF) rabbyt.set_viewport((640, 480)) rabbyt.set_default_attribs() font = Font(pygame.font.Font(pygame.font.get_default_font(), 20)) sprite = FontSprite(font, "hello world") sprite.rot = rabbyt.lerp(0, 360, dt=6, extend="repeat") sprite.x = 100 sprite.rgb = 0, 1, 1 while not pygame.event.get(pygame.QUIT): rabbyt.clear((0, .5, .5, 1)) rabbyt.set_time(pygame.time.get_ticks() / 1000) sprite.render() pygame.display.flip()
import rabbyt import pygame pygame.init() pygame.display.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF) rabbyt.set_viewport((640, 480)) rabbyt.set_default_attribs() sprites = [rabbyt.Sprite(x=x) for x in range(-100, 100, 50)] # Constant is the default extend mode. It will not go beyond start or end. sprites[0].y = rabbyt.lerp(-100, 100, 1, 3, extend="constant") # With extrapolate, it just keeps going. sprites[1].y = rabbyt.lerp(-100, 100, 1, 3, extend="extrapolate") # With repeat, it starts at start again after reaching end. sprites[2].y = rabbyt.lerp(-100, 100, 1, 3, extend="repeat") # Reverse is like repeat, only every other time it moves from end to start. sprites[3].y = rabbyt.lerp(-100, 100, 1, 3, extend="reverse") while not pygame.event.get(pygame.QUIT): rabbyt.clear() rabbyt.set_time(pygame.time.get_ticks() / 1000.0) rabbyt.render_unsorted(sprites) pygame.display.flip()
class Game: def __init__(self, client): self.client = client # Generate our board. self.gems = {} # empty_slot stores all the vertical rows as lists. This makes it easy # to make the rows drop later on. self.empty_slots = {} ids = [0,1,2,3,4,5,6] for x in range(8): self.empty_slots[x] = [] for y in range(8): random.shuffle(ids) for id in ids: # Make sure we aren't putting any 2 gems right next to each # other of the same type. row = self.find_gems_row((x,y),id,2) if not row: break gem = Gem(self.client, id=id, x=x, y=y) self.gems[(x,y)] = gem # Alright, this may get a bit confusing, but who said making games was easy? def drop_gems(self): # We need to keep track of all gems that have moved so we can detect # chain reaction scores. moved_gems = set() # Here we go through all the columns to see if they need to be dropped. for k,row in self.empty_slots.items(): if not row: # This row is devoid of emptiness. Skip it. continue # We sort the row so that the bottom most empty slot on this column # is first in the list. The row is structured with x being the key # and the value being a list of all the empty coordinates on the # board that need filled. row.sort(key=lambda obj:obj[1]) # We are only interested in the bottom most one. empty_xy = row[0] # We are going to start at the bottom most one and move the next # closest gem that is above it to be right on top of it. We then # work our way up until we have filled all empty spots in this # column (or row as the variable calls it). while True: if not self.gems.has_key(empty_xy): # Oops, we passed the top! This is it for this row. break if self.gems[empty_xy] is None: # This spot is empty so let's drop the closest one above it! # First step is to find it. ng = self.find_next_gem_up(empty_xy) # Move the gem we are dropping. Notice that where the ng gem # used to be it is now empty. That is one reason we work our # way up and not down! self.gems[ng.grid_xy] = None ng.grid_xy = empty_xy self.gems[empty_xy] = ng # Move the gem vissually. Above we just moved it in the # game's gem store. ng.y = rabbyt.lerp(end=ng.grid_y*62.5+52+26, dt=.6) moved_gems.add(ng) # Now we prepare for the next loop to check the slot about this # one. empty_xy = (empty_xy[0], empty_xy[1]+1) # This row is full! self.empty_slots[k] = [] # Don't be thrown off by this; we are still in the drop_gems function! # This is what's called a factory. This is probably one of the most # confusing places. What we are doing here is waiting until all our gems # have dropped and then checking to see if there are any chain reaction # scoring. If there is we drop it all over again. def _score(dt): _s = False for g in moved_gems: if g.do_score(): _s = True if _s: clock.schedule_once(lambda dt:(self.drop_gems()), .4) clock.schedule_once(_score, .6) def find_next_gem_up(self, (x,y)): # How this works is we start at the given xy position and work our way # up until we find a gem (and then we return it) or the top of the board # (and then we create a new gem and add it to the game). while True: y += 1 if self.gems.has_key((x,y)): if self.gems[(x,y)]: return self.gems[(x,y)] else: g = Gem(self.client, random.randint(0,6), x, y-1) g.alpha = rabbyt.lerp(start=0, end=1, dt=1) self.gems[g.grid_xy] = g return g