不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如以下这个import pygame
import sys
bg = pygame.Color('black')
#bg = (155, 180, 44)
speed = [1, 1]
size = width, height = 600, 400
set_full_screen = False
# 实例化Clock,后续将用CLock类的tick方法控制帧数
fcolck = pygame.time.Clock()
# 设置窗口模式
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
# 设置窗口标题
pygame.display.set_caption('Test')
# 导入图片
ball = pygame.image.load(r'D:\python库\PYG02.gif')
# 获得图片的矩形信息
ball_rect = ball.get_rect()
resolving_power = pygame.display.list_modes()
def processing_rgb(value):
return 0 if value < 0 else 255 if value > 255 else int(value + 0.5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.VIDEORESIZE:
side = width, height = event.w, event.h
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] >= 0 else speed[1] - 1
print(speed[1], '上')
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int((speed[1] / abs(speed[1])))
print(speed[1], '下')
elif event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int((speed[0] / abs(speed[0])))
print(speed[0], '左')
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
print(speed[0], '右')
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_F11:
set_full_screen = not set_full_screen
if set_full_screen:
side = width, height = 1920, 870
pygame.display.set_mode(side, pygame.FULLSCREEN)
else:
side = width, height = 600, 400
pygame.display.set_mode(side)
bg.r = processing_rgb((ball_rect.right / width) * 255)
bg.g = processing_rgb((ball_rect.top / height) * 255)
bg.b = processing_rgb(min(speed) / max(speed[0], speed[1], 1) * 255)
ball_rect = ball_rect.move(speed)
if ball_rect.left < 0 or ball_rect.right > width:
speed[0] = -speed[0]
if ball_rect.right + speed[0] > width:
speed[0] = -(abs(speed[0]))
if ball_rect.top < 0 or ball_rect.bottom > height:
speed[1] = -speed[1]
if ball_rect.bottom + speed[1] > height:
speed[1] = -(abs(speed[1]))
if ball_rect.top + speed[1] < 0:
speed[1] = abs(speed[1])
# 判断窗口是否被显示界面(屏幕绘制/非图标化,最小化)
# if pygame.display.get_active():
# ball_rect = ball_rect.move(speed)
screen.fill(bg)
screen.blit(ball, ball_rect)
pygame.display.update()
fcolck.tick(30)
|