|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
复制代码
问题描述:矩形移动时出现闪烁问题。
解决方案:在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)
|
|