鱼C论坛

 找回密码
 立即注册
查看: 2577|回复: 5

[已解决]pygame性能问题

[复制链接]
发表于 2021-1-2 20:37:01 | 显示全部楼层 |阅读模式

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

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

x
  1. '''pygame 图形绘制'''
  2. import pygame

  3. pygame.init()
  4. fcolck = pygame.time.Clock()
  5. screen = pygame.display.set_mode((600, 400))
  6. pygame.display.set_caption('图形绘制')
  7. GOLD = 255, 251, 0
  8. RED = pygame.Color('red')
  9. white = 255, 255, 255
  10. GREEN = pygame.Color('green')

  11. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
  12. while True:
  13.     pygame.display.flip()
  14.     fcolck.tick(40)


复制代码

为什么我运行以上代码,只我要对窗口进行任何操作窗口就会卡死(未响应),我想应该不是我的电脑配置的问题吧
最佳答案
2021-1-5 22:06:48
qin_yin 发表于 2021-1-2 21:37
不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如 ...

因为你这个有相应用户关闭事件,上面的代码没有

加上就好了:

  1. '''pygame 图形绘制'''
  2. import pygame
  3. import sys

  4. pygame.init()
  5. fcolck = pygame.time.Clock()
  6. screen = pygame.display.set_mode((600, 400))
  7. pygame.display.set_caption('图形绘制')
  8. GOLD = 255, 251, 0
  9. RED = pygame.Color('red')
  10. white = 255, 255, 255
  11. GREEN = pygame.Color('green')

  12. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
  13. while True:
  14.     for event in pygame.event.get():
  15.         if event.type == pygame.QUIT:
  16.             pygame.quit()
  17.             sys.exit(0)
  18.             
  19.     pygame.display.flip()
  20.     fcolck.tick(40)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-2 20:40:04 | 显示全部楼层
你写了个while True,当然会卡死
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-2 20:42:06 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2021-1-2 20:44 编辑

应该是死循环的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-2 21:37:16 | 显示全部楼层
qiuyouzhi 发表于 2021-1-2 20:40
你写了个while True,当然会卡死

不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如以下这个
  1. import pygame
  2. import sys

  3. bg = pygame.Color('black')
  4. #bg = (155, 180, 44)
  5. speed = [1, 1]
  6. size = width, height = 600, 400
  7. set_full_screen = False

  8. # 实例化Clock,后续将用CLock类的tick方法控制帧数
  9. fcolck = pygame.time.Clock()

  10. # 设置窗口模式
  11. screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  12. # 设置窗口标题
  13. pygame.display.set_caption('Test')
  14. # 导入图片
  15. ball = pygame.image.load(r'D:\python库\PYG02.gif')
  16. # 获得图片的矩形信息
  17. ball_rect = ball.get_rect()

  18. resolving_power = pygame.display.list_modes()

  19. def processing_rgb(value):
  20.     return 0 if value < 0 else 255 if value > 255 else int(value + 0.5)

  21. while True:
  22.     for event in pygame.event.get():
  23.         if event.type == pygame.QUIT:
  24.             sys.exit()

  25.         if event.type == pygame.VIDEORESIZE:
  26.             side = width, height = event.w, event.h

  27.         elif event.type == pygame.KEYDOWN:
  28.             if event.key == pygame.K_UP:
  29.                 speed[1] = speed[1] + 1 if speed[1] >= 0 else speed[1] - 1
  30.                 print(speed[1], '上')

  31.             elif event.key == pygame.K_DOWN:
  32.                 speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int((speed[1] / abs(speed[1])))
  33.                 print(speed[1], '下')

  34.             elif event.key == pygame.K_LEFT:
  35.                 speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int((speed[0] / abs(speed[0])))
  36.                 print(speed[0], '左')

  37.             elif event.key == pygame.K_RIGHT:
  38.                 speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
  39.                 print(speed[0], '右')

  40.             elif event.key == pygame.K_ESCAPE:
  41.                 sys.exit()

  42.             elif event.key == pygame.K_F11:
  43.                 set_full_screen = not set_full_screen

  44.                 if set_full_screen:
  45.                     side = width, height = 1920, 870
  46.                     pygame.display.set_mode(side, pygame.FULLSCREEN)

  47.                 else:
  48.                     side = width, height = 600, 400
  49.                     pygame.display.set_mode(side)

  50.         bg.r = processing_rgb((ball_rect.right / width) * 255)
  51.         bg.g = processing_rgb((ball_rect.top / height) * 255)
  52.         bg.b = processing_rgb(min(speed) / max(speed[0], speed[1], 1) * 255)


  53.     ball_rect = ball_rect.move(speed)
  54.     if ball_rect.left < 0 or ball_rect.right > width:
  55.         speed[0] = -speed[0]
  56.         if ball_rect.right + speed[0] > width:
  57.             speed[0] = -(abs(speed[0]))

  58.     if ball_rect.top < 0 or ball_rect.bottom > height:
  59.         speed[1] = -speed[1]
  60.         if ball_rect.bottom + speed[1] > height:
  61.             speed[1] = -(abs(speed[1]))

  62.         if ball_rect.top + speed[1] < 0:
  63.             speed[1] = abs(speed[1])

  64.     # 判断窗口是否被显示界面(屏幕绘制/非图标化,最小化)
  65. #    if pygame.display.get_active():
  66.     #    ball_rect = ball_rect.move(speed)


  67.     screen.fill(bg)
  68.     screen.blit(ball, ball_rect)
  69.     pygame.display.update()
  70.     fcolck.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-5 22:06:08 | 显示全部楼层
虽然说 pygame 优化的比较差,但是这个是你的代码问题,没有相应用户关闭事件。

改成这样:

  1. '''pygame 图形绘制'''
  2. import pygame
  3. import sys

  4. pygame.init()
  5. fcolck = pygame.time.Clock()
  6. screen = pygame.display.set_mode((600, 400))
  7. pygame.display.set_caption('图形绘制')
  8. GOLD = 255, 251, 0
  9. RED = pygame.Color('red')
  10. white = 255, 255, 255
  11. GREEN = pygame.Color('green')

  12. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
  13. while True:
  14.     for event in pygame.event.get():
  15.         if event.type == pygame.QUIT:
  16.             pygame.quit()
  17.             sys.exit(0)
  18.             
  19.     pygame.display.flip()
  20.     fcolck.tick(40)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-5 22:06:48 | 显示全部楼层    本楼为最佳答案   
qin_yin 发表于 2021-1-2 21:37
不写while True,窗口就一闪而过,而且之前的pygame脚本对窗口的刷新都是while循环里面也没毛病啊,就比如 ...

因为你这个有相应用户关闭事件,上面的代码没有

加上就好了:

  1. '''pygame 图形绘制'''
  2. import pygame
  3. import sys

  4. pygame.init()
  5. fcolck = pygame.time.Clock()
  6. screen = pygame.display.set_mode((600, 400))
  7. pygame.display.set_caption('图形绘制')
  8. GOLD = 255, 251, 0
  9. RED = pygame.Color('red')
  10. white = 255, 255, 255
  11. GREEN = pygame.Color('green')

  12. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
  13. while True:
  14.     for event in pygame.event.get():
  15.         if event.type == pygame.QUIT:
  16.             pygame.quit()
  17.             sys.exit(0)
  18.             
  19.     pygame.display.flip()
  20.     fcolck.tick(40)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 08:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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