pygame中闪烁

发布于 2024-12-25 00:23:15 字数 1729 浏览 1 评论 0原文

我编写了一个小程序,通过不同的颜色绘制一个循环图案以及一个用于调节帧速率的类。由于某种原因,每当我运行它时就会随机闪烁,但我不知道为什么。这很简单,所以我怀疑这与屏幕更新得不够快有关。我将不胜感激任何建议。

import pygame, time, random

w, h = 640, 480
screen = pygame.display.set_mode((w, h))
running = 1
inc = 20
m = [1, 1, 1]
c = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]


class FrameRate():
    def __init__(self, rate = 60):
        self.frame_rate = rate
        self.refresh_time = 1.0/self.frame_rate
        self.cur_time = time.time()
        self.prev_time = time.time()
        self.elapsed_time = 0

    def update(self):
        temp = self.cur_time
        self.cur_time = time.time()
        self.elapsed_time = self.cur_time - self.prev_time
        self.prev_time = temp

    def regulate_frame_rate(self):
        if self.elapsed_time < self.refresh_time:
            time.sleep(self.refresh_time - self.elapsed_time)
fr = FrameRate()
pygame.init()

while running:

    fr.update()
    fr.regulate_frame_rate()

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
            running = 0

    screen.fill((0, 0, 0))

    # Reverse color direction
    for i, p in enumerate(c):
        if c[i] > 255 or c[i] < 0:
            m[i] = -m[i]
        c[i] += m[i]

    for i in range(0,w/inc):
        try:
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, 0), (0, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, 0), (w, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, h), (0, i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, h), (w, i*inc))
        except TypeError:
            pass

    pygame.display.flip()

    pygame.quit()

I wrote a little program to draw a pattern a cycle through different color along with a class for moderating the frame rate. For some reason there is random flickering whenever I run it and I have no idea why. It is very simple so I doubt it has to do with the screen updating quickly enough. I would appreciate any suggestions.

import pygame, time, random

w, h = 640, 480
screen = pygame.display.set_mode((w, h))
running = 1
inc = 20
m = [1, 1, 1]
c = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]


class FrameRate():
    def __init__(self, rate = 60):
        self.frame_rate = rate
        self.refresh_time = 1.0/self.frame_rate
        self.cur_time = time.time()
        self.prev_time = time.time()
        self.elapsed_time = 0

    def update(self):
        temp = self.cur_time
        self.cur_time = time.time()
        self.elapsed_time = self.cur_time - self.prev_time
        self.prev_time = temp

    def regulate_frame_rate(self):
        if self.elapsed_time < self.refresh_time:
            time.sleep(self.refresh_time - self.elapsed_time)
fr = FrameRate()
pygame.init()

while running:

    fr.update()
    fr.regulate_frame_rate()

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
            running = 0

    screen.fill((0, 0, 0))

    # Reverse color direction
    for i, p in enumerate(c):
        if c[i] > 255 or c[i] < 0:
            m[i] = -m[i]
        c[i] += m[i]

    for i in range(0,w/inc):
        try:
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, 0), (0, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, 0), (w, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, h), (0, i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, h), (w, i*inc))
        except TypeError:
            pass

    pygame.display.flip()

    pygame.quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2025-01-01 00:23:15

一方面,将“pygame.quit()”从主循环中取出。

闪烁是由于您的绘图代码定期崩溃所致。当 try-except 块中的 except 被命中时,该帧将不会绘制任何内容,并且屏幕将“闪烁”黑色。

For one thing, get that "pygame.quit()" out of your main loop.

The flickering is due to the fact that your drawing code is periodically crashing. When the except in the try-except block is hit, then nothing will be drawn that frame, and the screen will "flicker" black.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文