def __init__(self, space, window): self.sprite_bot = get_sprite('pipe_bot.png') self.sprite_top = get_sprite('pipe_top.png') self.dx = self.sprite_bot.width self.dy = self.sprite_bot.height self.space = space self.x = window.width self.vx = -50 * settings.scale self.wx = window.width self.wy = window.height self.y0 = -self.dy / 2 + random.uniform(0, self.dy / 2.)
def __init__(self, window): # Load bird in different states self.sprite_lib = [get_sprite("bird1.png"), get_sprite("bird2.png"), get_sprite("bird3.png")] self.sprite_lib.append(self.sprite_lib[1]) self.window = window self.reset() self.dx = self.sprite.width self.dy = self.sprite.height
def __init__(self): self.sprite = get_sprite('floor.png') self.dx = self.sprite.width self.dy = self.sprite.height self.x = 0 self.y = -25 * settings.scale self.vx = -50 * settings.scale
def __init__(self): self.sprite = get_sprite('background.png') self.dx = self.sprite.width self.dy = self.sprite.height self.x = 0 self.y = 0 self.vx = -25 * settings.scale
def main(callback=None): #global score set to -1 because on first pipe score is increased global score score = -1 # Initialize window window = pyglet.window.Window(width=settings.window_width * settings.scale, height=settings.window_height * settings.scale, resizable=False) window.clear() scoreLabel = pyglet.text.Label("0", font_size=40, x=window.width//2, y=window.height, anchor_x='center', anchor_y='top') # To pass to the callback def click(): window.dispatch_event('on_mouse_press') # Set up sprites bird = Bird(window=window) background = Background() floor = Floor() pipes = [] tap_to_start = get_sprite('tap.png') gameover = get_sprite('gameover.png') # Set up game state, which indicates whether the game has started and how long # we have to wait until the next pipe appears. class GameState(object): def __init__(self): self.reset() def reset(self): self.started = False self.t_to_next_pipe = 2 # reset score label scoreLabel._set_text("0") state = GameState() def update(dt): global score if not state.started: return if bird.alive: state.t_to_next_pipe -= dt if state.t_to_next_pipe < 0: pipe = Pipe(space=75 * settings.scale, window=window) pipes.append(pipe) state.t_to_next_pipe += 2 # update score -- problem is for the first one score += 1 # directly setting text on scoreLabel._set_text(str(score)) for pipe in copy(pipes): if not pipe.visible: pipes.remove(pipe) # Move everything background.update(dt) for pipe in pipes: pipe.update(dt) floor.update(dt) # Check for collisions collision = check_collision(bird, floor) or any([check_collision(bird, pipe) for pipe in pipes]) if collision or bird.y > window.height: bird.die() if not bird.dead: bird.update(dt) if bird.dying and bird.y < -100: bird.stop() # reset the score on death score = -1 # function to be used in key & mouse events def still_playing(): if bird.alive: bird.flap() elif not state.started: state.started = True bird.start() bird.flap() elif bird.dead: bird.reset() pipes.clear() state.reset() @window.event def on_mouse_press(*args): still_playing() @window.event def on_key_press(*args): still_playing() @window.event def on_draw(): window.clear() background.blit() for pipe in pipes: pipe.blit() floor.blit() bird.blit() if not state.started: tap_to_start.blit(0.5 * (window.width - tap_to_start.width * 0.37), 0.43 * window.height) if bird.dying or bird.dead: gameover.blit(0.5 * (window.width - gameover.width), 0.5 * window.height) if callback is not None: import numpy as np buf = (gl.GLubyte * (3 * window.width * window.height))(0) gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf) array = np.frombuffer(buf, dtype='<u1') array = array.reshape(window.height, window.width, 3) array = array[::settings.scale, ::settings.scale] callback(array, click, alive=bird.alive) # draw score scoreLabel.draw() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) pyglet.clock.schedule_interval(update, 0.01) pyglet.app.run()