Entry 3242
hotshot
Submitted by anonymous
on Feb. 23, 2010 at 9:54 p.m.
Language: Python. Code size: 2.9 KB.
import pyglet from pyglet import resource import collide import random resource.path.append('res') resource.reindex() resource.add_font('saved_by_zero.ttf') gunshot = resource.media('44magnum.wav', streaming=False) clang = resource.media('metal_clang_rcl.wav', streaming=False) window = pyglet.window.Window(640, 480) class Target(pyglet.sprite.Sprite): '''Movable, collidable entity class''' def __init__(self, batch, group): image = resource.image('target.png') x = random.random() * (window.width - image.width) y = random.random() * (window.height - image.height) pyglet.sprite.Sprite.__init__(self, image, x, y, batch=batch, group=group) self.collision = collide.SpriteCollision(self) self.dx = (random.random() - 0.5) * 400 self.dy = (random.random() - 0.5) * 400 def hittest(self,x,y): return collide.hittest(self.collision,x,y) def update(self, dt): if self.x <= 0 or self.x + self.width >= window.width: self.dx *= -1 if self.y <= 0 or self.y + self.height >= window.height: self.dy *= -1 self.x += self.dx * dt self.y += self.dy * dt self.x = min(max(self.x, 0), window.width - self.width) self.y = min(max(self.y, 0), window.height - self.height) FONT_NAME = 'Saved By Zero' fps_display = pyglet.clock.ClockDisplay(font=pyglet.font.load(FONT_NAME, 24)) image = resource.image('cursor.png') backgrnd = resource.image('greybg.png') bulletholeimg = resource.image('bullethole.png') cursor = pyglet.window.ImageMouseCursor(image, 50, 50) window.set_mouse_cursor(cursor) bulletholes_batch = pyglet.graphics.Batch() bulletholes = [] target_group = pyglet.graphics.OrderedGroup(1) targets_batch = pyglet.graphics.Batch() targets = [] for x in xrange(12): targets.append(Target(targets_batch,target_group)) @window.event def on_mouse_press(x, y, button, modifiers): if button==1: gunshot.play() hits = [t for t in targets if t.hittest(x,y)] hits.sort(lambda x,y:y.group.order-x.group.order) if len(hits) > 0: clang.play() targets.remove(hits[0]) hits[0].delete() else: bulletholes.append(pyglet.sprite.Sprite(bulletholeimg,x-25,y-25,batch=bulletholes_batch)) @window.event def on_draw(): window.clear() backgrnd.blit(0,0,0) bulletholes_batch.draw() targets_batch.draw() fps_display.draw() def update(dt): for t in targets: t.update(dt) pyglet.clock.schedule_interval(update, 1/30.) #window.push_handlers(pyglet.window.event.WindowEventLogger()) label = pyglet.text.Label('Label Text', font_size=14, x=window.width // 2, y=10, anchor_x='center') if __name__ == '__main__': pyglet.app.run()
This snippet took 0.02 seconds to highlight.
Back to the Entry List or Home.