为什么在 pyglet 上 window.clear() 之后屏幕会闪烁?

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

当我运行这个(按开始后)屏幕从菜单屏幕到下一个屏幕来回闪烁,并且它还将球员和草地的图像放在闪烁的菜单屏幕上。我不知道是什么原因造成的,我尝试了一些方法,但似乎没有任何效果,甚至没有任何改变。任何帮助将不胜感激。

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。如果您测试代码,您将需要图像来替换我正在使用的 PNG

我通过制作 3 批次并且一次只绘制一个来修复它。