鱼C论坛

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

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

[复制链接]
发表于 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
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-29 10:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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