bigdoubt
bigdoubt

Reputation: 21

Why is the screen flickering after window.clear() on pyglet?

When I run this (after pressing start) the screen flickers back and forth from the menu screen to the next screen, and it also places the images of the player and grass on the flickering menu screen. I don't know what is causing this, and I've tried some things but nothing seems to work or even change it. Any help would be greatly appreciated.

import random
import pyglet
from pyglet.window import key
from pyglet.window import mouse
on = {}
off = {}
on[1] = pyglet.text.Label('Start', font_name='Arial', font_size=50, x=0, y=735, color=(255, 255, 255, 255))
off[1] = pyglet.text.Label('Start', font_name='Arial', font_size=50, x=0, y=735, color=(100, 100, 100, 255))
on[2] = pyglet.text.Label('Settings', font_name='Arial', font_size=50, x=0, y=680, color=(255, 255, 255, 255))
off[2] = pyglet.text.Label('Settings', font_name='Arial', font_size=50, x=0, y=680, color=(100, 100, 100, 255))
on[3] = pyglet.text.Label('Exit', font_name='Arial', font_size=50, x=0, y=625, color=(255, 255, 255, 255))
off[3] = pyglet.text.Label('Exit', font_name='Arial', font_size=50, x=0, y=625, color=(100, 100, 100, 255))
gd = False
direction = "f"
plx = 384
ply = 384
grass_image = pyglet.image.load("grass.png")
fwd_image = pyglet.image.load("fwd.png")
bck_image = pyglet.image.load("bck.png")
lft_image = pyglet.image.load("lft.png")
rgt_image = pyglet.image.load("rgt.png")
menu = True
window = pyglet.window.Window(width=800, height=800, caption="RPG")
selected = 0
start = on[1]
settings = off[2]
exitt = off[3]
@window.event
def on_draw():
    global on
    global off
    global grass_image
    global menu
    global start
    global settings
    global exitt
    global grass
    global plx
    global ply
    global direction
    global fwd_image
    global bck_image
    global lft_image
    global rgt_image
    global gd
    global window
    if menu == True:
        start.draw()
        settings.draw()
        exitt.draw()
        info = pyglet.text.Label('Use the up and down arrows to navigate the menu, and Z to select.', font_name='Arial', font_size=20, x=0, y=575, color=(255, 255, 255, 255))
        info.draw()
    elif menu == False:
        if gd == False:
            x = 0
            y = 0
            window.clear()
            while y < 800:
                grass = pyglet.sprite.Sprite(grass_image, x=x, y=y)
                grass.draw()
                x += 32
                if x > 800:
                    x = 0
                    y += 32
            gd = True
        if direction == "f":
            fwd = pyglet.sprite.Sprite(fwd_image, x=plx, y=ply)
            fwd.draw()
        if direction == "b":
            bck = pyglet.sprite.Sprite(bck_image, x=plx, y=ply)
            bck.draw()
        if direction == "l":
            lft = pyglet.sprite.Sprite(lft_image, x=plx, y=ply)
            lft.draw()
        if direction == "r":
            rgt = pyglet.sprite.Sprite(rgt_image, x=plx, y=ply)
            rgt.draw()

@window.event
def on_key_press(symbol, modifier):
    global on
    global off
    global menu
    global start
    global settings
    global exitt
    global selected
    global plx
    global ply
    global direction
    global grass_image
    if symbol == key.UP:
        if menu == True:
            if selected == 1:
                selected = 0
                start = on[1]
                settings = off[2]
            elif selected == 2:
                selected = 1
                settings = on[2]
                exitt = off[3]
            elif selected == 0:
                selected = 2
                start = off[1]
                exitt = on[3]
        else:
            grass = pyglet.sprite.Sprite(grass_image, x=plx, y=ply)
            grass.draw()
            ply += 32
            direction = "b"
    if symbol == key.DOWN:
        if menu == True:
            if selected == 0:
                selected = 1
                start = off[1]
                settings = on[2]
            elif selected == 1:
                selected = 2
                settings = off[2]
                exitt = on[3]
            elif selected == 2:
                selected = 0
                start = on[1]
                exitt = off[3]
        else:
            grass = pyglet.sprite.Sprite(grass_image, x=plx, y=ply)
            grass.draw()
            ply -= 32
            direction = "f"
    if symbol == key.LEFT:
        if menu == True:
            pass
        else:
            grass = pyglet.sprite.Sprite(grass_image, x=plx, y=ply)
            grass.draw()
            plx -= 32
            direction = "l"
    if symbol == key.RIGHT:
        if menu == True:
            pass
        else:
            grass = pyglet.sprite.Sprite(grass_image, x=plx, y=ply)
            grass.draw()
            plx += 32
            direction = "r"
    if symbol == key.Z:
        if menu == True:
            if selected == 0:
                menu = False
                print("starting")
    if symbol == key.X:
        if menu == True:
            pass

pyglet.app.run()

P.S. If you test the code you will need images to replace the PNGs I'm using

Upvotes: 1

Views: 132

Answers (1)

bigdoubt
bigdoubt

Reputation: 21

I fixed it by making 3 batches and only drawing one at a time.

Upvotes: 1

Related Questions