示例#1
0
    def __init__(self, run):
        super(Window, self).__init__(1024, 600, caption="Copycat", vsync=False)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self.clock = pyglet.clock.ClockDisplay()
        self.show_fps = False

        self.done = False
        self.time = 0
        self.speed = 15
        self.playing = False

        self.run = run

        background = pyglet.resource.image("blackboard.png")
        self.background = pyglet.sprite.Sprite(background)
        self.saved_temp = 0
        self.batch = pyglet.graphics.Batch()

        self.play = pyglet.resource.image("play.png")
        self.pause = pyglet.resource.image("pause.png")
        self.play.anchor_x = self.play.width / 2.0
        self.play.anchor_y = self.play.height / 2.0
        self.pause.anchor_x = self.pause.width / 2.0
        self.pause.anchor_y = self.pause.height / 2.0
        self.button = Button(self.play, 30, 580,
                             self.on_play_button, self.batch)

        self.timer = pyglet.text.Label("0", "EraserDust", 18, x=512, y=574,
                                       color=(255,255,255, 125), batch=self.batch,
                                       halign="center", anchor_x="center")

        self.rule = pyglet.text.Label("", "EraserDust", 16, x=512, y=445,
                                      color=(255,255,255, 190), batch=self.batch,
                                      halign="center", anchor_x="center")

        self.slipnet = Slipnet(self.run.slipnet, 0, 0, 512, 300, self.batch)
        self.coderack = Coderack(self.run.coderack, 512, 0, 512, 300, self.batch)
        self.workspace = Workspace(self.run.workspace, 0, 300, 1024, 300, self.batch)

        pyglet.clock.schedule(self.update)
示例#2
0
class Window(pyglet.window.Window):
    """The main window keeps track of what scene is currently being viewed,
    manages the gui elements that are always on screen, and takes care of
    updating the simulation."""

    def __init__(self, run):
        super(Window, self).__init__(1024, 600, caption="Copycat", vsync=False)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self.clock = pyglet.clock.ClockDisplay()
        self.show_fps = False

        self.done = False
        self.time = 0
        self.speed = 15
        self.playing = False

        self.run = run

        background = pyglet.resource.image("blackboard.png")
        self.background = pyglet.sprite.Sprite(background)
        self.saved_temp = 0
        self.batch = pyglet.graphics.Batch()

        self.play = pyglet.resource.image("play.png")
        self.pause = pyglet.resource.image("pause.png")
        self.play.anchor_x = self.play.width / 2.0
        self.play.anchor_y = self.play.height / 2.0
        self.pause.anchor_x = self.pause.width / 2.0
        self.pause.anchor_y = self.pause.height / 2.0
        self.button = Button(self.play, 30, 580,
                             self.on_play_button, self.batch)

        self.timer = pyglet.text.Label("0", "EraserDust", 18, x=512, y=574,
                                       color=(255,255,255, 125), batch=self.batch,
                                       halign="center", anchor_x="center")

        self.rule = pyglet.text.Label("", "EraserDust", 16, x=512, y=445,
                                      color=(255,255,255, 190), batch=self.batch,
                                      halign="center", anchor_x="center")

        self.slipnet = Slipnet(self.run.slipnet, 0, 0, 512, 300, self.batch)
        self.coderack = Coderack(self.run.coderack, 512, 0, 512, 300, self.batch)
        self.workspace = Workspace(self.run.workspace, 0, 300, 1024, 300, self.batch)

        pyglet.clock.schedule(self.update)

    def on_key_press(self, symbol, modifiers):
        if symbol == pyglet.window.key.ESCAPE:
            pyglet.app.exit()
        elif symbol == pyglet.window.key.F and modifiers == pyglet.window.key.MOD_CTRL:
            self.show_fps = not self.show_fps
        elif symbol == pyglet.window.key.SPACE:
            self.button.on_key_press(symbol, modifiers)
    
    def on_mouse_press(self, x, y, button, modifiers):
        self.button.on_mouse_press(x, y, button, modifiers)
        
    def on_mouse_release(self, x, y, button, modifiers):
        self.button.on_mouse_release(x, y, button, modifiers)

    def on_play_button(self):
        self.playing = not self.playing
        if self.playing:
            self.button.sprite.image = self.pause
        else:
            self.button.sprite.image = self.play

    def update(self, dt):
        self.button.update(dt) 

        if self.done or not self.playing:
            return

        # Update each graphical module.
        self.slipnet.update(dt)
        self.coderack.update(dt)
        self.workspace.update(dt)

        # Check for completion.
        if self.run.workspace.answer_string:
            self.rule.text = self.run.workspace.rule.to_string()
            self.done = True

        # Update the timestep display.
        time = str(self.run.coderack.time / self.run.timestep)
        if self.timer.text != time:
            self.timer.text = time

        # Update the temperature display.
        target_temp = self.run.workspace.temperature * 2.55
        if abs(self.saved_temp - target_temp) > .001:
            self.saved_temp += (target_temp - self.saved_temp) * dt
            self.background.color = (240,
                                     255 - self.saved_temp / 1.4,
                                     255 - self.saved_temp / 1.15)
        
        # Update the simulation at the given speed.
        self.time += dt
        if self.time >= 1.0 / self.speed:
            self.run.step()
            self.time = 0

    def on_draw(self):
        self.clear()
        self.background.draw()
        self.batch.draw()
        if self.show_fps:
            self.clock.draw()