简体   繁体   English

Pygame 仅在第二次运行程序时闪烁

[英]Pygame flickers only on second run of program

I have a simple pygame program that I've written that I am running on a raspberry pi CM4 using the raspberry pi lite OS (buster).我有一个我编写的简单的 pygame 程序,我正在使用 raspberry pi lite OS (buster) 在 raspberry pi CM4 上运行。 As such I had to install x manually so that pygame would have something to output to.因此我不得不手动安装 x 以便 pygame 可以输出一些东西。 This was done using the following command:这是使用以下命令完成的:

sudo apt-get install --no-install-recommends xserver-xorg xinit x11-xserver-utils openbox

I start the program at startup by having the following in my.bash_profile:我在启动时通过在 my.bash_profile 中包含以下内容来启动程序:

[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor

As well as this code in /etc/xdg/openbox/autostart :以及/etc/xdg/openbox/autostart中的这段代码:

# Disable any form of screen saver / screen blanking / power management
xset s off
xset s noblank
xset -dpms

# Allow quitting the X server with CTRL-ATL-Backspace
setxkbmap -option terminate:ctrl_alt_bksp

cd project_dir && python project.py

Here's the contents of project_dir/project.py:这是 project_dir/project.py 的内容:

import pygame as pg

class Game:
    def __init__(self):
        pg.init()

        self.__clock = pg.time.Clock()

        self.__window_size = (1024, 768)
        self.__project_size = (392, 291)

        self.screen = pg.display.set_mode(self.__window_size, pg.FULLSCREEN)

        self.blank_background = pg.transform.scale(pg.image.load("assets/default_background.png"),
                self.__window_size).convert()
        self.grid_image = pg.transform.scale(pg.image.load("assets/new_project_thumb.png"),
                self.__project_size).convert()

        self.project_selected = 0

        self.rect_pixels = 3

        self.grid_locations = self.calculate_grid_locations()

    def run(self) -> None:
        pg.mouse.set_visible(False)

        playing = True

        while playing:
            events = pg.event.get()

            button_event = None

            self.screen.blit(self.blank_background, (0, 0))

            for event in events:
                if event.type == pg.KEYDOWN:
                    button_event = event.key

            if button_event == pg.K_LEFT:
                self.project_selected = max((self.project_selected - 1), 0)

            if button_event == pg.K_RIGHT:
                self.project_selected = min((self.project_selected + 1), 3)

            if button_event == pg.K_ESCAPE:
                playing = False

            self.draw_menu()
            pg.display.update()
            self.__clock.tick(6)

    def draw_menu(self):
        for i in range(4):
            if i == self.project_selected:
                rect_loc = (self.grid_locations[i][0] - self.rect_pixels,
                            self.grid_locations[i][1] - self.rect_pixels)
                pg.draw.rect(self.screen,
                             (237, 28, 36),
                             pg.Rect(rect_loc,
                                     (self.__project_size[0] + self.rect_pixels * 2,
                                      self.__project_size[1] + self.rect_pixels * 2)))

            self.screen.blit(self.grid_image, self.grid_locations[i])
                                                        

    def calculate_grid_locations(self):
        w=int((self.__window_size[0] - 2 * self.__project_size[0]) / 3)
        h=int((self.__window_size[1] - 2 * self.__project_size[1]) / 3)

        return [(w, h),
                (w * 2 + self.__project_size[0], h),
                (w, h * 2 + self.__project_size[1]),
                (w * 2 + self.__project_size[0], h * 2 + self.__project_size[1])]

if __name__ == "__main__":
    game = Game()
    game.run()

The program works fine on autostart, but if I quit the program and x after auto-startup using ctrl-alt-backspace and then restart it from the command line using python project.py , the display intermittently flickers between frames of the pygame program.该程序在自动启动时运行良好,但如果我在使用 ctrl-alt-backspace 自动启动后退出程序和 x,然后使用python project.py从命令行重新启动它,则显示会在 pygame 程序的帧之间间歇性闪烁。 This does not occur when running on my ubuntu laptop, just on the CM4.在我的 ubuntu 笔记本电脑上运行时不会发生这种情况,只是在 CM4 上。

Does anyone know what might be causing this?有谁知道这可能是什么原因造成的?

Figured it out - the issue was that I was killing x server.想通了 - 问题是我正在杀死 x 服务器。 Python / pygame can somehow still output to the screen if x server is killed, but the performance is really degraded.如果 x 服务器被杀死,Python / pygame 仍然可以以某种方式输出到屏幕,但性能确实下降了。 If instead of python project.py I run startx -- -nocursor again, it starts x server again and also runs the whole script in /etc/xdg/openbox/autostart as part of that startup, which causes the program to launch as well.如果我再次运行startx -- -nocursor而不是python project.py ,它会再次启动 x server 并在/etc/xdg/openbox/autostart中运行整个脚本作为启动的一部分,这也会导致程序启动.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM