鱼C论坛

 找回密码
 立即注册
查看: 1425|回复: 2

[已解决]pygame矩形移动闪烁怎么办

[复制链接]
发表于 2023-9-29 15:59:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")

position = [300, 250,100, 100]
vel = [2, 1]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

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

    #move teh rectangle
    position[0] += vel[0]
    position[1] += vel[1]
    if position[0] > 500 or position[0] < 0:
        vel[0] = -vel[0]
    if position[1] > 400 or position[1] < 0:
        vel[1] = -vel[1]
    #draw the rectangel
    color = 255, 255, 0
    width = 0
    pygame.draw.rect(screen, color, position, width)

    pygame.display.update()
    pygame.time.delay(10)

最佳答案
2023-9-29 15:59:23
问题描述:矩形移动时出现闪烁问题。

解决方案:在pygame中,可以使用双缓冲技术来解决矩形移动时的闪烁问题。双缓冲技术的原理是将要显示的图像先绘制在一个缓冲区中,等到整个图像绘制完成后再将其一次性地显示出来。这样可以避免在绘制过程中出现的闪烁问题。

修改代码如下:

import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")

position = [300, 250,100, 100]
vel = [2, 1]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # create a new surface with the same size as the screen
    # and fill it with the background color
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 200))

    #move the rectangle
    position[0] += vel[0]
    position[1] += vel[1]
    if position[0] > 500 or position[0] < 0:
        vel[0] = -vel[0]
    if position[1] > 400 or position[1] < 0:
        vel[1] = -vel[1]

    #draw the rectangle on the background surface
    color = 255, 255, 0
    width = 0
    pygame.draw.rect(background, color, position, width)

    #copy the background surface to the screen
    screen.blit(background, (0, 0))

    pygame.display.update()
    pygame.time.delay(10)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-29 15:59:23 | 显示全部楼层    本楼为最佳答案   
问题描述:矩形移动时出现闪烁问题。

解决方案:在pygame中,可以使用双缓冲技术来解决矩形移动时的闪烁问题。双缓冲技术的原理是将要显示的图像先绘制在一个缓冲区中,等到整个图像绘制完成后再将其一次性地显示出来。这样可以避免在绘制过程中出现的闪烁问题。

修改代码如下:

import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")

position = [300, 250,100, 100]
vel = [2, 1]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # create a new surface with the same size as the screen
    # and fill it with the background color
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 200))

    #move the rectangle
    position[0] += vel[0]
    position[1] += vel[1]
    if position[0] > 500 or position[0] < 0:
        vel[0] = -vel[0]
    if position[1] > 400 or position[1] < 0:
        vel[1] = -vel[1]

    #draw the rectangle on the background surface
    color = 255, 255, 0
    width = 0
    pygame.draw.rect(background, color, position, width)

    #copy the background surface to the screen
    screen.blit(background, (0, 0))

    pygame.display.update()
    pygame.time.delay(10)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-29 15:59:48 | 显示全部楼层
你好!对于矩形移动闪烁的问题,可以通过控制矩形的显示和隐藏来实现。

首先,我们可以在每次循环开始前判断矩形是否需要显示,如果需要显示,则绘制矩形;如果不需要显示,则跳过绘制步骤。这样可以达到矩形闪烁的效果。

另外,为了使矩形移动更加平滑,可以使用 pygame.time.Clock() 来控制帧率。同时,修改矩形的移动速度可以使其移动得更加流畅。

下面是修改后的代码示例:
import pygameimport sysfrom pygame.locals import *pygame.init()screen = pygame.display.set_mode((600, 500))pygame.display.set_caption("Drawing Rectangles")position = [300, 250, 100, 100]vel = [2, 1]clock = pygame.time.Clock()  # 控制帧率的时钟对象show_rect = True  # 控制矩形是否显示的标志while True:    for event in pygame.event.get():        if event.type == QUIT:            pygame.quit()            sys.exit()    screen.fill((0, 0, 200))    # 控制矩形的显示和隐藏    if show_rect:        # 移动矩形        position[0] += vel[0]        position[1] += vel[1]                # 检查矩形是否超出屏幕边界        if position[0] > 500 or position[0] < 0:            vel[0] = -vel[0]        if position[1] > 400 or position[1] < 0:            vel[1] = -vel[1]                # 绘制矩形        color = 255, 255, 0        width = 0        pygame.draw.rect(screen, color, position, width)        show_rect = not show_rect  # 切换矩形的显示状态    pygame.display.update()    clock.tick(60)  # 设置帧率为60帧/秒
修改后的代码中,我们添加了一个 show_rect 的标志来控制矩形是否显示。在每一次循环中,先判断矩形是否需要显示,如果需要显示则进行矩形的移动和绘制,然后切换 show_rect 的状态。通过控制帧率为60帧/秒,可以使矩形移动和闪烁更加平滑。

希望这能帮到你!如果有任何进一步的问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 11:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表