pygame性能问题
'''pygame 图形绘制'''import pygame
pygame.init()
fcolck = pygame.time.Clock()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('图形绘制')
GOLD = 255, 251, 0
RED = pygame.Color('red')
white = 255, 255, 255
GREEN = pygame.Color('green')
e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
while True:
pygame.display.flip()
fcolck.tick(40)
为什么我运行以上代码,只我要对窗口进行任何操作窗口就会卡死(未响应),我想应该不是我的电脑配置的问题吧 你写了个while True,当然会卡死 本帖最后由 hrp 于 2021-1-2 20:44 编辑
应该是死循环的问题 qiuyouzhi 发表于 2021-1-2 20:40
你写了个while True,当然会卡死
不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如以下这个
import pygame
import sys
bg = pygame.Color('black')
#bg = (155, 180, 44)
speed =
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 = speed + 1 if speed >= 0 else speed - 1
print(speed, '上')
elif event.key == pygame.K_DOWN:
speed = speed if speed == 0 else (abs(speed) - 1) * int((speed / abs(speed)))
print(speed, '下')
elif event.key == pygame.K_LEFT:
speed = speed if speed == 0 else (abs(speed) - 1) * int((speed / abs(speed)))
print(speed, '左')
elif event.key == pygame.K_RIGHT:
speed = speed + 1 if speed > 0 else speed - 1
print(speed, '右')
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, speed, 1) * 255)
ball_rect = ball_rect.move(speed)
if ball_rect.left < 0 or ball_rect.right > width:
speed = -speed
if ball_rect.right + speed > width:
speed = -(abs(speed))
if ball_rect.top < 0 or ball_rect.bottom > height:
speed = -speed
if ball_rect.bottom + speed > height:
speed = -(abs(speed))
if ball_rect.top + speed < 0:
speed = abs(speed)
# 判断窗口是否被显示界面(屏幕绘制/非图标化,最小化)
# 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) 虽然说 pygame 优化的比较差,但是这个是你的代码问题,没有相应用户关闭事件。
改成这样:
'''pygame 图形绘制'''
import pygame
import sys
pygame.init()
fcolck = pygame.time.Clock()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('图形绘制')
GOLD = 255, 251, 0
RED = pygame.Color('red')
white = 255, 255, 255
GREEN = pygame.Color('green')
e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
pygame.display.flip()
fcolck.tick(40)
qin_yin 发表于 2021-1-2 21:37
不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如 ...
因为你这个有相应用户关闭事件,上面的代码没有
加上就好了:
'''pygame 图形绘制'''
import pygame
import sys
pygame.init()
fcolck = pygame.time.Clock()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('图形绘制')
GOLD = 255, 251, 0
RED = pygame.Color('red')
white = 255, 255, 255
GREEN = pygame.Color('green')
e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
pygame.display.flip()
fcolck.tick(40)
页:
[1]