How exactly does the pyglet game loop work? - python

I've read all that I can find and looked at many examples of a game loop on pyglet, but I'm still not sure HOW it's working or what exactly is happening.
(These are the articles I read...)
http://www.pyglet.org/doc/programming_guide/the_application_event_loop.html
http://www.pyglet.org/doc/api/toc-pyglet.event-module.html
I understand that the basic structure is something like this (this is just an example):
INITIALIZE GAME WINDOW
game_window = pyglet.window.Window(800, 600)
ATTACH EVENT HANDLERS
#game_window.event
def on_draw():
game_window.clear()
player_sprite.draw()
START PYGLET
pyglet.app.run()
I know it all works in practice, but I don't quite understand it. And I feel like until I have a good grip on the mechanics, I won't be able to use pyglet to its full potential.
So you tell pyglet to run and... it finds the objects that have events (the game_window) and it somehow finds and calls those functions that you attached handlers to? How does it know which scope/namespace to find them in? Does it just scan your entire code until it finds where you put the event handlers? Does it loop over them? How does it know where to start and stop the loop? How does it work???
Thank you!

Let's play:
class Window:
def __init__(self, x, y):
global app
app = self
def event(self, func):
self.what_todo = func
def run(self):
self.what_todo()
>>> game_window = Window(800, 600)
>>>
>>> #game_window.event
... def on_draw():
... print("I am drawing!")
...
>>> app.run()
I am drawing!
>>>
Of course, they probably are doing things a bit differently, but I hope you get the basic idea.

Related

Clearing the screen for text based rpg in python

I am trying to make a text based rpg game in python. I just started and this is what my code looks so far:
class Game:
def __init__(self):
self.map = [[" " for i in range(50)]for i in range(30)]
def draw_map(self):
for i in range(len(self.map)):
print(self.map[i])
def add_stuff(self, thing, x, y):
self.map[y][x] = thing
game = Game()
class Player:
def __init__(self):
self.x = 1
self.y = 1
self.player = "ME"
def draw(self):
game.add_stuff(self.player, self.x, self.y)
def move(self):
pass
player = Player()
player.draw()
game.draw_map()
I was trying to find a way to implement a game loop in some way. To do this i was thinking of running the draw_map() and clearing the screen right away and printing the map again, a bit like real game loops. However i am having problems doing this. Based on other answers to other similar questions, i managed to produce the following code(it just shows the main loop and subprocess is imported as sp).
while True:
game.draw_map()
sp.call("cls", shell = True)
However this is not working for me. It simply dosent do anything. I also tried using clear function from clear_screen` module in a similar way and i cant figure out why this wouldnt work. Thanks for any help
so based on your previous comments you want to clear the screen in a Python interpreter. There is no "command" to clear the screen just like this. But you can use a loop to print some new lines until your screen is blank again.
def clear(lines):
for l in range(lines):
print()
while True:
game.draw_map()
clear(35)
time.sleep(0.05)
You may need to increase or decrease the amount of lines cleared. I hope this works for you. P.S.: I would use a normal console.
https://en.wikipedia.org/wiki/Interpreter_(computing)#Efficiency:
Efficiency:
The main disadvantage of interpreters is that an interpreted program typically runs slower than if it had been compiled. The difference in speeds could be tiny or great; often an order of magnitude and sometimes more. It generally takes longer to run a program under an interpreter than to run the compiled code but it can take less time to interpret it than the total time required to compile and run it. This is especially important when prototyping and testing code when an edit-interpret-debug cycle can often be much shorter than an edit-compile-run-debug cycle.
If your problem is clearing the console window, you can just use
import os
os.system('cls')
in place of
sp.call("cls", shell = True)
(Assuming you are working on windows)
EDIT for clarity:
This is the new loop after the change:
while True:
game.draw_map()
os.system('cls')

Is there a way to make the turtles movement smoother

I'm making a game where you control a space ship, and currently, its movement is very hard to look at, because it's quite janky. Is there a command to make the movement smoother and more natural? I'm using
python with turtle on repl.it, and onrelease doesn't seem to work. Any answer is appreciated!
import turtle
you = turtle.Turtle()
keycoms = turtle.Screen()
you.penup()
def w():
you.fd(5)
def s():
you.bk(3)
def a():
you.lt(5)
def d():
you.rt(5)
keycoms.onkey(w,"w")
keycoms.onkey(s,"s")
keycoms.onkey(a,"a")
keycoms.onkey(d,"d")
keycoms.listen()
One way this is often solved is to make more, smaller movements, with a slight time delay between each one. This gives the illusion of flowing movement rather than a jump - something like the following (there may be a better way to actually code it, but this gives you an idea):
def w():
for _ in range(5):
you.fd(1)
sleep(0.1)
Simply change your onkey(...) to onkeypress(...):
import turtle
you = turtle.Turtle()
keycoms = turtle.Screen()
you.penup()
def w():
you.fd(5)
def s():
you.bk(3)
def a():
you.lt(5)
def d():
you.rt(5)
keycoms.onkeypress(w,"w")
keycoms.onkeypress(s,"s")
keycoms.onkeypress(a,"a")
keycoms.onkeypress(d,"d")
keycoms.listen()
keycoms.mainloop()
The Scrren.onkey() method will allow the user to press down on a specific key for as long as the user wants, and the function command to the assigned action will only execute once.
Screen.onkeypress(), on the other hand, will execute the command as many times as the user holds down the key.

trying to draw over sprite or change picture pyglet

I am trying to learn pyglet and practice some python coding with a questionnaire thing, but I am unable to find a way to make the background picture be removed or drawn on top of or something for 10 seconds. I am new and am lacking in a lot of the knowledge I would need, thank you for helping!
import pyglet
from pyglet.window import Window
from pyglet.window import key
from pyglet import image
import time
card1 = False
cat_image = pyglet.image.load("cat.png")
dog_image = pyglet.image.load("dog.png")
image = pyglet.image.load("backg.png")
background_sprite = pyglet.sprite.Sprite(image)
cat = pyglet.sprite.Sprite(cat_image)
dog = pyglet.sprite.Sprite(dog_image)
window = pyglet.window.Window(638, 404, "Life")
mouse_pos_x = 0
mouse_pos_y = 0
catmeme = pyglet.image.load("catmeme.png")
sprite_catmeme = pyglet.sprite.Sprite(catmeme)
#window.event
def on_draw():
window.clear()
background_sprite.draw()
card_draw1(63, 192, 385, 192)
def card1():
while time.time() < (time.time() + 10):
window.clear()
sprite_catmeme.draw()
#window.event
def card_draw1(x1, y1, x2, y2):
cat.set_position(x1, y1)
dog.set_position(x2, y2)
cat.draw()
dog.draw()
def card_draw2():
pass
#window.event
def on_mouse_press(x, y, button, modifiers):
if x > cat.x and x < (cat.x + cat.width):
if y > cat.y and y < (cat.y + cat.height):
card1()
game = True
while game:
on_draw()
pyglet.app.run()
There's a few flaws in the order and in which you do things.
I will try my best to describe them and give you a piece of code that might work better for what your need is.
I also think your description of the problem is a bit of an XY Problem which is quite common when asking for help on complex matters where you think you're close to a solution, so you're asking for help on the solution you've come up with and not the problem.
I'm assuming you want to show a "Splash screen" for 10 seconds, which happens to be your background? And then present the cat.png and dog.png ontop of it, correct?
If that's the case, here's where you probably need to change things in order for it to work:
The draw() function
It doesn't really update the screen much, it simply adds things to the graphical memory. What updates the screen is you or something telling the graphics library that you're done adding things to the screen and it's time to update everything you've .draw()'n. So the last thing you need in the loop would be window.flip() in order for the things you've drawn to actually show.
Your things might show if you try to wiggle the window around, it should trigger a re-draw of the scene because of how the internal mechanics of pyglet work..
If you don't call .flip() - odds are probable that the redraw() call will never occur - which again, is a internal mechanism of Pyglet/GL that tells the graphics card that something has been updated, we're done updating and it's time to redraw the scene.
a scene
This is the word most commonly used for what the user is seeing.
I'll probably throw this around a lot in my text, so it's good to know that this is what the user is seeing, not what you've .draw()'n or what's been deleted, it's the last current rendering of the graphics card to the monitor.
But because of how graphical buffers work we've might have removed or added content to the memory without actually drawing it yet. Keep this in mind.
The pyglet.app.run() call
This is a never ending loop in itself, so having that in a while game: loop doesn't really make sense because .run() will "hang" your entire application, any code you want to execute needs to be in def on_draw or an event that is generated from within the graphical code itself.
To better understand this, have a look at my code, i've pasted it around a couple of times here on SO over the years and it's a basic model of two custom classes that inherits the behavior of Pyglet but lets you design your own classes to behave slightly differently.
And most of the functionality is under on_??? functions, which is almost always a function used to catch Events. Pyglet has a lot of these built in, and we're going to override them with our own (but the names must be the same)
import pyglet
from pyglet.gl import *
key = pyglet.window.key
class CustomSprite(pyglet.sprite.Sprite):
def __init__(self, texture_file, x=0, y=0):
## Must load the texture as a image resource before initializing class:Sprite()
self.texture = pyglet.image.load(texture_file)
super(CustomSprite, self).__init__(self.texture)
self.x = x
self.y = y
def _draw(self):
self.draw()
class MainScreen(pyglet.window.Window):
def __init__ (self):
super(MainScreen, self).__init__(800, 600, fullscreen = False)
self.x, self.y = 0, 0
self.bg = CustomSprite('bg.jpg')
self.sprites = {}
self.alive = 1
def on_draw(self):
self.render()
def on_close(self):
self.alive = 0
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
elif symbol == key.C:
print('Rendering cat')
self.sprites['cat'] = CustomSprite('cat.png', x=10, y=10)
elif symbol == key.D:
self.sprites['dog'] = CustomSprite('dog.png', x=100, y=100)
def render(self):
self.clear()
self.bg.draw()
for sprite_name, sprite_obj in self.sprites.items():
sprite_obj._draw()
self.flip()
def run(self):
while self.alive == 1:
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()
x = MainScreen()
x.run()
Now, this code is kept simple on purpose, the full code I usually paste on SO can be found at Torxed/PygletGui, the gui.py is where most of this comes from and it's the main loop.
What I do here is simply replace the Decorators by using "actual" functions inside a class. The class itself inherits the functions from a traditional pyglet.window.Window, and as soon as you name the functions the same as the inherited onces, you replace the core functionality of Window() with whatever you decide.. In this case, i mimic the same functions but add a few of my own.
on_key_press
One such example is on_key_press(), which normally just contain a pass call and does nothing, here, we check if key.C is pressed, and if so - we add a item to self.sprites.. self.sprites just so happen to be in our render() loop, anything in there will be rendered ontop of a background.
Here's the pictures I used:
(named bg.jpg, cat.png, dog.png - note the different file endings)
class:CustomSprite
CustomSprite is a very simple class designed to make your life easier at this point, nothing else. It's very limited in functionality but the little it do is awesome.
It's soul purpose is to take a file name, load it as an image and you can treat the object like a traditional pyglet.sprite.Sprite, meaning you can move it around and manipulate it in many ways.
It saves a few lines of code having to load all the images you need and as you can see in gui_classes_generic.py you can add a heap of functions that's "invisible" and normally not readily availbale to a normal sprite class.
I use this a bunch! But the code gets complicated real fast so I kept this post simple on purpose.
the flip function
Even in my class, I still need to use flip() in order to update the contents of the screen. This is because .clear() clears the window as you would expect, that also triggers a redraw of the scene.
bg.draw() might in some cases trigger a redraw if the data is big enough or if something else happens, for instance you move the window.
but calling .flip() will tell the GL backend to force a redraw.
Further optimizations
There's a thing called batched rendering, basically the graphic card is designed to take enormous ammounts of data and render it in one go, so calling .draw() on several items will only clog the CPU before the GPU even gets a chance to shine. Read more about Batched rendering and graphics! It will save you a lot of frame rates.
Another thing is to keep as little functionality as possible in the render() loop and use the event triggers as your main source of coding style.
Pyglet does a good job of being fast, especially if you only do things on event driven tasks.
Try to avoid timers, but if you really do need to use time for things, such as removing cat.png after a certain ammount of time, use the clock/time event to call a function that removes the cat. Do not try to use your own t = time() style of code unless you know where you're putting it and why. There's a good timer, I rarely use it.. But you should if you're starting off.
This has been one hell of a wall of text, I hope it educated you some what in the life of graphics and stuff. Keep going, it's a hurdle to get into this kind of stuff but it's quite rewarding once you've mastered it (I still haven't) :)

How is this method called? (Pyglet)

The following code is an alternative to the usual style (using decorators) pyglet makes use of.
Can anyone explain how the on_draw() method is called here?
import pyglet
class HelloWorldWindow(pyglet.window.Window):
def __init__(self):
super(HelloWorldWindow, self).__init__()
self.label = pyglet.text.Label('Hello, world!')
def on_draw(self):
self.clear()
self.label.draw()
if __name__ == '__main__':
window = HelloWorldWindow()
pyglet.app.run()
The code written using decorators can be found here.
You can just dig through the source to find the answer.
The EventLoop class (you use it by pyglet.app.run()) dispatches the on_draw event regulary.
From the source:
Calling run begins the application event loop, which processes
operating system events, calls pyglet.clock.tick to call scheduled
functions and calls pyglet.window.Window.on_draw and
pyglet.window.Window.flip to update window contents.
The Window class subscripes to this event:
BaseWindow.register_event_type('on_draw')
So by subclassing Window, you ensure your on_draw method gets called.
Look at the programming guide for an example of how the event system of pyglet works.

Is there a way to wrap a tkinter GUI in a class that can be created and interacted with from another object? (*without* hanging in mainloop)

Is there a way to wrap a tkinter GUI in a class that can be created and interacted with from another object? For this to be really useful, the class has to work so mainloop or its equivalent doesn't hold the application. If so, can someone point me to a working example?
For context, what I'm trying to do is make a SimpleUI class that I can use in any application to allow it to display information or register functions to be executed on button or key presses. So any threading, queues, etc. I would like to have hidden in the SimpleUI class.
Based on what I've gathered from reading around, the answer is No without re-implementing mainloop. Rather, the GUI should be the main application which farms out work through one method or another. However, this would make any application with tkinter (perhaps other GUIs as well?) feel like the tail is wagging the dog. Hopefully I have misunderstood what I have beeing reading.
I know this may seem like a repost of this and other similar questions but I can't comment on those and the answers seem to be doing the opposite of what I want to do. In addition to that question, I've found bits and pieces of related code in many places, but I have not been able to put them together. If there is a way, I'll learn Python threads or whatever to make it work.
I'm using Python 3.1 if it makes any difference.
Example of how I would like it to work.
ui = SimpleUI()
ui.hide()
ui.show()
ui.add_frame...
ui.add_button...
ui.register_function(button, function)
Is this what you're looking for?
#The threading module allows you to subclass it's thread class to create
#and run a thread, in this case we will be starting SimpleUI in a seperate thread
import threading
from Tkinter import *
def printfunction():
print "ButtonPress"
class NewClass:
def __init__(self):
self.ui = SimpleUI()
self.ui.add_frame("frame1")
self.ui.add_button("button1","frame1")
self.ui.register_function("button1",printfunction)
self.ui.start()
#self.ui = Threader().start()
def PrintSuccess(self):
print "Success!"
class SimpleUI:
def __init__(self):
self.root = Tk()
self.frames = {}
self.buttons = {}
def start(gui):
class Threader(threading.Thread):
def run(self):
gui.root.mainloop()
Threader().start()
def hide(self):
self.root.withdraw()
if(raw_input("press enter to show GUI: ")==""):self.show()
def show(self):
self.root.update()
self.root.deiconify()
def add_frame(self,name):
tmp = Frame(self.root)
tmp.pack()
self.frames[name] = tmp
def add_button(self,name,frame):
tmp = Button(self.frames[frame])
tmp.pack()
self.buttons[name] = tmp
def register_function(self,button,function):
self.buttons[button].config(command=function)
NC = NewClass()
NC.PrintSuccess()

Categories