Python Forum
[Pyglet] pyglet freezing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [Pyglet] pyglet freezing (/thread-18926.html)



pyglet freezing - pfaber11 - Jun-06-2019

Hi just starting piglet and when I try and run this code it freezes but If I press a key on the keyboard it moves on a frame here's the code could somebody please help.

import pyglet
#from pyglet.window import key

a1 = 600
b1 = 300
t = 1
image1 = pyglet.resource.image("Aba2 Title.png")
image2 = pyglet.resource.image("picy.png")
sprite1 = pyglet.sprite.Sprite(image2, a1, b1)


window = pyglet.window.Window(1366, 768)
@window.event
def on_draw():
    global a1
    a1 = a1 + 2    
    window.clear()
    image1.blit(0,0)
    sprite1 = pyglet.sprite.Sprite(image2, a1, b1)
    sprite1.draw()
    if a1 >= 1300 :
        a1 = 200
        
        
@window.event        
def update(dt):
    global a1
    a1 = a1 + 2     
    
    

@window.event

def main():
    carryon = False
    while not carryon :        
        global t
        global a1
        if a1 >= 1300 : t = 0  
        if t == 0 : carryon = True
        on_draw()
        global sprite1
        a1 += 2
        update()

    
 
pyglet.app.run()
main()
I know there are errors but it runs but stops after one iteration and if I press a key on the keyboard it runs for another frame then stops . Thanks in advance for your help.


RE: pyglet freezing - Windspar - Jun-06-2019

Example
import pyglet
from pyglet.window import key

window = pyglet.window.Window()
image = your image data here
sprite = pyglet.sprite.Sprite(image, 10, 10)
speed = 3

@window.event
def on_draw():
    window.clear()
    sprite.draw()

@window.event
def on_key_press(symbol, mod):
    if symbol == key.RIGHT:
        sprite.x += speed

    if symbol == key.LEFT:
        sprite.x -= speed

def update(delta):
    sprite.y += delta * 40
    if sprite.y + sprite.height / 2 > window.height:
        sprite.y -= window.height

pyglet.clock.schedule_interval(update, 1 / 60)
pyglet.app.run()