Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
import random
import sys
from pyglet.gl import *
import pyglet
from pyglet.window import key
BALL_IMAGE = 'ball.png'
BALL_SOUND = 'ball.wav'
if len(sys.argv) > 1:
BALL_SOUND = sys.argv[1]
window = pyglet.window.Window(640, 480)
sound = pyglet.resource.media(BALL_SOUND, streaming=False)
balls_batch = pyglet.graphics.Batch()
balls = []
label = pyglet.text.Label('Press space to add a ball, backspace to remove',
font_size=14,
x=window.width // 2, y=10,
anchor_x='center')
class Ball(pyglet.sprite.Sprite):
ball_image = pyglet.resource.image(BALL_IMAGE)
width = ball_image.width
height = ball_image.height
def __init__(self):
center_anchor(explosion_image)
explosion_animation = \
pyglet.image.Animation.from_image_sequence(explosion_images,
EXPLOSION_ANIMATION_PERIOD,
loop=False)
pointer_image = resource.image('pointer.png')
pointer_image.anchor_x = pointer_image.width // 2
pointer_image.anchor_y = pointer_image.height // 2
pointer_image_flip = resource.image('pointer.png', flip_x=True)
explosion_sound = resource.media('explosion.wav', streaming=False)
bullet_sound = resource.media('bullet.wav', streaming=False)
starfield = Starfield(resource.image('starfield.jpg'))
player = Player(resource.image('ship.png'), wrapping_batch)
win.push_handlers(player)
# --------------------------------------------------------------------------
# Global game state vars
# --------------------------------------------------------------------------
overlay = None
in_game = False
paused = False
score = 0
difficulty = 2
show_fps = False
enable_sound = True
score_text = pyglet.text.Label('',
def __init__(self, image, position=(0, 0), rotation=0, scale=1,
opacity=255, color=(255, 255, 255), anchor=None, **kwargs):
if isinstance(image, string_types):
image = pyglet.resource.image(image)
self.transform_anchor_x = 0
self.transform_anchor_y = 0
self._image_anchor_x = 0
self._image_anchor_y = 0
# These need to be forward-defined here because pyglet<1.3 sprites don't have them.
self._scale_x = 1
self._scale_y = 1
pyglet.sprite.Sprite.__init__(self, image, **kwargs)
BatchableNode.__init__(self)
if anchor is None:
if isinstance(self.image, pyglet.image.Animation):
anchor = (image.frames[0].image.width // 2,
y=ly, color=(200, 200, 200, 255))
self.tilesets_label.x = w
h = max(h, self.tilesets_label.content_height)
w += self.tilesets_label.content_width
self._width = (int(w) // self.TILE_SIZE) * self.TILE_SIZE + 8
if int(w) % self.TILE_SIZE: self._width += self.TILE_SIZE
self._height = h + 16 + self.TILE_SIZE * 8
# set up the background
bg = pyglet.image.SolidColorImagePattern((0, 0, 0, 200))
bg = pyglet.image.create(self._width, self._height, pattern=bg)
# set up the tiles
self.active_batch = self.tiles_batch = pyglet.graphics.Batch()
self.bg = pyglet.sprite.Sprite(bg, group=pyglet.graphics.OrderedGroup(0),
batch=self.active_batch)
self.tile_sprites = []
x = y = 4
for n, k in enumerate(self.tileset):
s = pyglet.sprite.Sprite(self.tileset[k].image, y=y, x=x,
batch=self.tiles_batch)
if not n:
self.current = s
s.tile_id = k
self.tile_sprites.append(s)
s.scale = self.TILE_SIZE / s.width
if not n % 8:
y = 4; x += self.TILE_SIZE
else:
y += self.TILE_SIZE
def _bind_texture(self, i):
gl.glActiveTexture((gl.GL_TEXTURE0, gl.GL_TEXTURE1, gl.GL_TEXTURE2)[i])
gl.glEnable(gl.GL_TEXTURE_2D)
gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture[i].id)
gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE)
gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_ALPHA, gl.GL_REPLACE if i == 0 else gl.GL_ADD)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
def render_rect_filled(shape, x, y):
""" Render the shape at the right spot. """
# Set color
GL.glDisable(GL.GL_BLEND)
GL.glColor4ub(shape.color[0], shape.color[1], shape.color[2], 255)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, shape.vbo_id)
GL.glVertexPointer(2, GL.GL_FLOAT, 0, 0)
GL.glLoadIdentity()
GL.glTranslatef(x + shape.width / 2, y + shape.height / 2, 0)
GL.glDrawArrays(GL.GL_QUADS, 0, shape.size)
import glooey
import run_demos
class TestFrame(glooey.Frame): #
custom_alignment = 'fill'
class Decoration(glooey.Background): #
custom_center = pyglet.image.load('assets/64x64/green.png')
class Box(glooey.Bin): #
custom_padding = 16
class TestBackground(glooey.Background): #
custom_center = pyglet.image.load('assets/64x64/orange.png')
window = pyglet.window.Window()
gui = glooey.Gui(window)
@run_demos.on_space(gui) #
def test_image():
frame = TestFrame()
widget = TestBackground()
frame.add(widget)
gui.add(frame)
yield "A orange tiled background in green tiled frame."
frame.padding = 32
yield "Pad around the frame."
frame.box.padding = 48
yield "Pad inside the frame."
#!/usr/bin/env python3
import pyglet
import glooey.themes.golden as golden
import run_demos
colors = 'red', 'green', 'blue', 'grey'
icons = None, 'save', 'chat', 'zoom', 'yes', 'no', 'plus', 'minus', 'up', 'right', 'down', 'left'
window = pyglet.window.Window()
gui = golden.Gui(window)
button = golden.RoundButton()
gui.add(button)
@run_demos.on_space(gui) #
def test_round_button():
for color in colors:
button.color = color
for icon in icons:
button.icon = icon
yield f"{color} button with {icon or 'no'} icon."
pyglet.app.run()
def test_caption(self):
w1 = window.Window(400, 200, resizable=True)
w2 = window.Window(400, 200, resizable=True)
count = 1
w1.set_caption('Window caption %d' % count)
w2.set_caption(u'\u00bfHabla espa\u00f1ol?')
last_time = time.time()
while not (w1.has_exit or w2.has_exit):
if time.time() - last_time > 1:
count += 1
w1.set_caption('Window caption %d' % count)
last_time = time.time()
w1.dispatch_events()
w2.dispatch_events()
w1.close()
w2.close()
#!/usr/bin/env python3
import pyglet
import glooey
import run_demos
window = pyglet.window.Window()
gui = glooey.Gui(window)
widget = glooey.Image()
gui.add(widget)
@run_demos.on_space(gui) #
def test_image():
widget.image = pyglet.image.load('assets/misc/star_5.png')
yield "Show a green star."
widget.image = pyglet.image.load('assets/misc/star_7.png')
yield "Show a orange star."
pyglet.app.run()