def __init__(self): self.highlight = spryte.Sprite(highlight_image, 0, 0) self.show_highlight = False # CREATE THE MAP self.field = tilemap.Map() self.play_field = {} l = [] for y, line in enumerate(field_rows): m = [] l.append(m) for x, cell in enumerate(line): if cell == '#': m.append( spryte.Sprite(wall_image, x * cw, y * ch, batch=self.field)) content = path.Blocker else: m.append( spryte.Sprite(blank_image, x * cw, y * ch, batch=self.field)) if cell == 'E': content = path.End elif cell == 'S': content = path.Start elif cell == '+': content = path.Blocker else: content = None self.play_field[x * 2, y * 2] = content self.play_field[x * 2 + 1, y * 2] = content self.play_field[x * 2, y * 2 + 1] = content self.play_field[x * 2 + 1, y * 2 + 1] = content self.field.set_cells(cw, ch, l) # PATH FOR ENEMIES self.path = path.Path.determine_path(self.play_field, map_width * 2, map_height * 2) #self.path.dump() self.constructions = spryte.SpriteBatch() self.enemies = spryte.SpriteBatch() self.bullets = spryte.SpriteBatch()
def animate(dt): # update car rotation & speed r = car.rotation r += (keyboard[key.RIGHT] - keyboard[key.LEFT]) * 200 * dt if r < 0: r += 360 elif r > 360: r -= 360 car.rotation = r car.speed = (keyboard[key.UP] - keyboard[key.DOWN]) * 200 * dt # ... and the rest spryte.update_kinematics(car, dt) # handle balls for i, s in enumerate(balls): # update positions s.x += s.dx * dt s.y += s.dy * dt if s.right > win.width and s.dx > 0: s.dx *= -1 s.right = win.width elif s.left < 0 and s.dx < 0: s.dx *= -1 s.left = 0 if s.top > win.height and s.dy > 0: s.dy *= -1 s.top = win.height elif s.bottom < 0 and s.dy < 0: s.dy *= -1 s.bottom = 0 # handle collisions if not s.intersects(car): continue if s.scale > 2: # pop! explosion = EffectSprite(explosion_animation, 0, 0, batch=explosions) explosion.center = s.center explosion.push_handlers s.delete() spryte.Sprite(ball, win.width * random.random(), win.height * random.random(), batch=balls, dx=-50 + 100 * random.random(), dy=-50 + 100 * random.random()) else: s.scale += .1 n = min(1, max(0, 2 - s.scale)) s.color = (1, 1, 1, .5 + n / 2)
def from_imagegrid(cls, im, cells, file=None, origin=None): """Initialise the map using an image the image grid. Both the image grid and the map cells have y=0 at the bottom of the grid / map. Return a Map instance.""" texture_sequence = im.texture_sequence l = list() cw, ch = texture_sequence.item_width, texture_sequence.item_height inst = cls() for y, row in enumerate(cells): m = list() l.append(m) for x, num in enumerate(row): m.append(spryte.Sprite(texture_sequence[num], x * cw, y * ch, map=inst, batch=inst)) inst.set_cells(cw, ch, l, origin) return inst
import spryte NUM_CARS = 100 if len(sys.argv) > 1: NUM_CARS = int(sys.argv[1]) win = window.Window(vsync=False) fps = clock.ClockDisplay(color=(1, 1, 1, 1)) cars = spryte.SpriteBatch() car = resource.image('car.png') car.anchor_x = 16 car.anchor_y = 20 for i in range(NUM_CARS): s = spryte.Sprite(car, win.width * random.random(), win.height * random.random(), batch=cars, dr=-45 + random.random() * 90) while not win.has_exit: win.dispatch_events() clock.tick() win.clear() for car in cars: car.rotation = (car.rotation + car.dr) % 360 cars.draw() fps.draw() win.flip()
from pyglet import resource from pyglet.window import key import spryte win = window.Window(width=640, height=400, vsync=False) fps = clock.ClockDisplay(color=(1, 1, 1, 1)) balls = spryte.SpriteBatch() ball = resource.image('ball.png') ball.anchor_x = 16 ball.anchor_y = 16 for i in range(200): spryte.Sprite(ball, (win.width - 64) * random.random(), (win.height - 64) * random.random(), batch=balls, dx=-50 + 100 * random.random(), dy=-50 + 100 * random.random()) car = resource.image('car.png') car.anchor_x = 16 car.anchor_y = 20 car = spryte.Sprite(car, win.width / 2, win.height / 2) class EffectSprite(spryte.Sprite): def on_animation_end(self): self.delete() explosions = spryte.SpriteBatch()