鱼C论坛

 找回密码
 立即注册
查看: 7694|回复: 6

[已解决]关于pygame运行卡顿的问题

[复制链接]
发表于 2018-10-15 17:22:16 | 显示全部楼层 |阅读模式

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

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

x
代码和书中P280的一致,实现小乌龟的贴边行走。
没有报错,但图片运动得非常卡,走走停停的。之前书中的几个例子都没有出现这样的问题。打开任务管理器看了,CPU、内存和磁盘都在50%以下。求教大佬什么原因以及解决方案。
下面贴上我的代码,照着书敲的,是在pycharm里运行的。
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()
  5. size=width,height=600,400
  6. screen=pygame.display.set_mode(size)  #这里如果不分2句写会报错
  7. bg=(255,255,255)
  8. pygame.display.set_caption("Turtle Go")
  9. turtle=pygame.image.load("cat_200_300.jpg")

  10. clock=pygame.time.Clock()
  11. position=turtle_rect=turtle.get_rect()  #获得图像的位置矩形
  12. # 小乌龟顺时针行走
  13. speed=[5,0]
  14. #先把旋转命令定义好
  15. turtle_right=pygame.transform.rotate(turtle,90)  #靠右走时图片逆时针转了90°
  16. turtle_top=pygame.transform.rotate(turtle,180)
  17. turtle_left=pygame.transform.rotate(turtle,270)
  18. #turtle_bottom=pygame.transform.rotate(turtle,0)
  19. turtle_bottom=turtle  #这两句试验下来效果等同
  20. #刚开始走顶部
  21. turtle=turtle_top

  22. while True:
  23.     for event in pygame.event.get():
  24.         if event.type==QUIT:
  25.             pygame.quit()
  26.             sys.exit()
  27.         position=position.move(speed)
  28.         if position.right>width:
  29.             turtle=turtle_right  #旋转
  30.             position=turtle_rect=turtle.get_rect()
  31.             position.left=width-turtle_rect.width
  32.             speed=[0,5]
  33.         if position.bottom>height:
  34.             turtle=turtle_bottom
  35.             position=turtle_rect=turtle.get_rect()
  36.             position.top=height-turtle_rect.height
  37.             position.left=width-turtle_rect.width
  38.             speed=[-5,0]
  39.         if position.left<0:
  40.             turtle = turtle_left
  41.             position = turtle_rect = turtle.get_rect()
  42.             position.top = height-turtle_rect.height
  43.             speed = [0,-5]
  44.         if position.top<0:
  45.             turtle = turtle_top
  46.             position = turtle_rect = turtle.get_rect()
  47.             speed = [5,0]

  48.         screen.fill(bg)
  49.         screen.blit(turtle,position)
  50.         pygame.display.flip()
  51.         clock.tick(30)
复制代码
最佳答案
2020-7-13 17:30:51
  1. position=position.move(speed)
复制代码

从这一句开始,后面的代码全部加一个Tab缩进
会大大提升效率
修改后代码:
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()
  5. size=width,height=600,400
  6. screen=pygame.display.set_mode(size)  #这里如果不分2句写会报错
  7. bg=(255,255,255)
  8. pygame.display.set_caption("Turtle Go")
  9. turtle=pygame.image.load(r"C:\Programstudy\Python\mytest\Pygamestudy\images\turtle.png")

  10. clock=pygame.time.Clock()
  11. position=turtle_rect=turtle.get_rect()  #获得图像的位置矩形
  12. # 小乌龟顺时针行走
  13. speed=[5,0]
  14. #先把旋转命令定义好
  15. turtle_right=pygame.transform.rotate(turtle,90)  #靠右走时图片逆时针转了90°
  16. turtle_top=pygame.transform.rotate(turtle,180)
  17. turtle_left=pygame.transform.rotate(turtle,270)
  18. #turtle_bottom=pygame.transform.rotate(turtle,0)
  19. turtle_bottom=turtle  #这两句试验下来效果等同
  20. #刚开始走顶部
  21. turtle=turtle_top

  22. while True:
  23.     for event in pygame.event.get():
  24.         if event.type==QUIT:
  25.             pygame.quit()
  26.             sys.exit()
  27.     position=position.move(speed)
  28.     if position.right>width:
  29.         turtle=turtle_right  #旋转
  30.         position=turtle_rect=turtle.get_rect()
  31.         position.left=width-turtle_rect.width
  32.         speed=[0,5]
  33.     if position.bottom>height:
  34.         turtle=turtle_bottom
  35.         position=turtle_rect=turtle.get_rect()
  36.         position.top=height-turtle_rect.height
  37.         position.left=width-turtle_rect.width
  38.         speed=[-5,0]
  39.     if position.left<0:
  40.         turtle = turtle_left
  41.         position = turtle_rect = turtle.get_rect()
  42.         position.top = height-turtle_rect.height
  43.         speed = [0,-5]
  44.     if position.top<0:
  45.         turtle = turtle_top
  46.         position = turtle_rect = turtle.get_rect()
  47.         speed = [5,0]

  48.     screen.fill(bg)
  49.     screen.blit(turtle,position)
  50.     pygame.display.flip()
  51.     clock.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-15 17:29:58 | 显示全部楼层
最后 clock.tick(30) ??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-16 08:57:30 | 显示全部楼层
claws0n 发表于 2018-10-15 17:29
最后 clock.tick(30) ??

应该不是这个原因,注释掉后还是卡,但是不卡时走得更快了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-8 16:20:45 | 显示全部楼层
循环加入事件监听,神秘代码
event = pygame.event.poll()
    if event.type == pygame.QUIT:
        pygame.quit()
        exit()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-10 10:03:11 | 显示全部楼层
一起爬山嘛 发表于 2020-7-8 16:20
循环加入事件监听,神秘代码
event = pygame.event.poll()
    if event.type == pygame.QUIT:

在for event in pygame.event.get():这句后面加上event = pygame.event.poll()对吧?
没有用,还是很卡
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 17:30:51 | 显示全部楼层    本楼为最佳答案   
  1. position=position.move(speed)
复制代码

从这一句开始,后面的代码全部加一个Tab缩进
会大大提升效率
修改后代码:
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()
  5. size=width,height=600,400
  6. screen=pygame.display.set_mode(size)  #这里如果不分2句写会报错
  7. bg=(255,255,255)
  8. pygame.display.set_caption("Turtle Go")
  9. turtle=pygame.image.load(r"C:\Programstudy\Python\mytest\Pygamestudy\images\turtle.png")

  10. clock=pygame.time.Clock()
  11. position=turtle_rect=turtle.get_rect()  #获得图像的位置矩形
  12. # 小乌龟顺时针行走
  13. speed=[5,0]
  14. #先把旋转命令定义好
  15. turtle_right=pygame.transform.rotate(turtle,90)  #靠右走时图片逆时针转了90°
  16. turtle_top=pygame.transform.rotate(turtle,180)
  17. turtle_left=pygame.transform.rotate(turtle,270)
  18. #turtle_bottom=pygame.transform.rotate(turtle,0)
  19. turtle_bottom=turtle  #这两句试验下来效果等同
  20. #刚开始走顶部
  21. turtle=turtle_top

  22. while True:
  23.     for event in pygame.event.get():
  24.         if event.type==QUIT:
  25.             pygame.quit()
  26.             sys.exit()
  27.     position=position.move(speed)
  28.     if position.right>width:
  29.         turtle=turtle_right  #旋转
  30.         position=turtle_rect=turtle.get_rect()
  31.         position.left=width-turtle_rect.width
  32.         speed=[0,5]
  33.     if position.bottom>height:
  34.         turtle=turtle_bottom
  35.         position=turtle_rect=turtle.get_rect()
  36.         position.top=height-turtle_rect.height
  37.         position.left=width-turtle_rect.width
  38.         speed=[-5,0]
  39.     if position.left<0:
  40.         turtle = turtle_left
  41.         position = turtle_rect = turtle.get_rect()
  42.         position.top = height-turtle_rect.height
  43.         speed = [0,-5]
  44.     if position.top<0:
  45.         turtle = turtle_top
  46.         position = turtle_rect = turtle.get_rect()
  47.         speed = [5,0]

  48.     screen.fill(bg)
  49.     screen.blit(turtle,position)
  50.     pygame.display.flip()
  51.     clock.tick(30)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-14 10:38:23 | 显示全部楼层
wuqramy 发表于 2020-7-13 17:30
从这一句开始,后面的代码全部加一个Tab缩进
会大大提升效率
修改后代码:

就是这个原因,看了下书,确实我缩进敲错了,看来pygame.event.get()这句很耗时啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 09:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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