user7486737
user7486737

Reputation: 1

How do I run the pyglet.window.clear function through a on_button_press event?

Long story short I'm making a text based game in python 3 using Pyglet and I'm having trouble clearing the window. I want to clear out everything so I can put new words and new pictures in. I was hoping to set it up like this:

@window.event
def on_key_press(key.escape,modifier)
    window.clear

Or something along those lines but it doesn't seem to work. Does anyone have any suggestions, ideas? the only other idea would be to convert the words to black and any images to black and layer it up but then the window will still be loading that stuff and I could see as the game goes on it starts eating up more and more ram to run the window with it loading all the previous pictures and text.

I'd prefer to avoid that and just clear it out, also then I don't have to basically copy the same code over and over again making it black.

Thanks, Ben

Upvotes: 0

Views: 1321

Answers (1)

Torxed
Torxed

Reputation: 23480

import pyglet
from pyglet.gl import *

from time import time # Used for FPS calc

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self):
        super(main, self).__init__(800, 800, fullscreen = False, vsync = True)

        self.running = True

        self.fps_counter = 0
        self.last_fps = time()
        self.fps_text = pyglet.text.Label(str(self.fps_counter), font_size=12, x=10, y=10)

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.running = False
        else:
            self.clear() # However, this is done in the render() logic anyway.

    def on_draw(self):
        self.render()

    def render(self):
        self.clear()

        # And flip the GL buffer
        self.fps_counter += 1
        if time() - self.last_fps > 1:
            self.fps_text.text = str(self.fps_counter)
            self.fps_counter = 0
            self.last_fps = time()

        self.fps_text.draw()

        self.flip()

    def run(self):
        while self.running is True:
            self.render()

            # -----------> This is key <----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()
            if event and event.type == pygame.QUIT:
                self.running = False

x = main()
x.run()

This is a quick and dirty copy-and-paste logic of a FPS counter against a black screen.
Every key-press will force a clear of the screen.

In your example, you're probably store pyglet.window.Window() as myWindow = py... or something similar. And on that global variable you'd call myWindow.clear() from your decorator.

import pyglet
myWindow = pyglet.window.Window()

@myWindow.event
def on_key_press(symbol, modifier):
    myWindow.clear()

pyglet.all.run()

(You lacked a : at the end, key.escape should just be a variable and clear is a function not a variable.)

However, if you're going to continue writing large graphical applications, i'd strongly suggest you classify your code and make it "OOP" right off the bat, graphical libraries tend to get messy rather quick otherwise.

Upvotes: 1

Related Questions