鱼C论坛

 找回密码
 立即注册
查看: 2020|回复: 2

Python pygame动画精灵问题

[复制链接]
发表于 2020-7-30 09:40:23 | 显示全部楼层 |阅读模式

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

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

x
在小甲鱼pygame动画精灵那一章,我跟着小甲鱼写代码,但是写完后运行起来十分卡顿,请问这是为什么呢?
代码如下
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. class Ball(pygame.sprite.Sprite):
  6.    
  7.     def __init__(self,image,position,speed,bg_size):
  8.         pygame.sprite.Sprite.__init__(self)
  9.         
  10.         self.image = pygame.image.load(image).convert_alpha()
  11.         self.rect = self.image.get_rect()
  12.         self.rect.left,self.rect.top = position
  13.         self.speed = speed
  14.         self.width,self.height = bg_size[0],bg_size[1]

  15.     def move(self):
  16.         self.rect = self.rect.move(self.speed)

  17.         if self.rect.right < 0:
  18.             self.rect.left = self.width
  19.             
  20.         if self.rect.left > self.width:
  21.             self.rect.right = 0

  22.         if self.rect.bottom < 0:
  23.             self.rect.top = self.height
  24.             
  25.         if self.rect.top > self.height:
  26.             self.rect.bottom = 0

  27.         


  28. def main():
  29.     pygame.init()

  30.     ball_image = 'W020130917263500532559.png'#素材
  31.     bg_image = '780 (1).png'#素材

  32.     running = True

  33.     bg_size = width,height = 780,520#1024,681
  34.     screen = pygame.display.set_mode(bg_size)
  35.     pygame.display.set_caption('玩球')

  36.     background = pygame.image.load(bg_image).convert_alpha()

  37.     balls = []

  38.     for i in range(5):
  39.         position = randint(0,width-100),randint(0,height-100)
  40.         speed = [randint(-10,10),randint(-10,10)]
  41.         ball = Ball(ball_image,position,speed,bg_size)
  42.         balls.append(ball)

  43.     clock = pygame.time.Clock()
  44.    
  45.     while running:
  46.         for event in pygame.event.get():
  47.             if event.type == QUIT:
  48.                 sys.exit()

  49.             screen.blit(background,(0,0))
  50.             for each in balls:
  51.                 each.move()
  52.                 screen.blit(each.image,each.rect)

  53.             pygame.display.flip()
  54.             clock.tick(10)
  55.             

  56. if __name__ == "__main__":
  57.     main()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-30 09:42:35 | 显示全部楼层
把 FPS 调大一点试试:

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. class Ball(pygame.sprite.Sprite):
  6.    
  7.     def __init__(self,image,position,speed,bg_size):
  8.         pygame.sprite.Sprite.__init__(self)
  9.         
  10.         self.image = pygame.image.load(image).convert_alpha()
  11.         self.rect = self.image.get_rect()
  12.         self.rect.left,self.rect.top = position
  13.         self.speed = speed
  14.         self.width,self.height = bg_size[0],bg_size[1]

  15.     def move(self):
  16.         self.rect = self.rect.move(self.speed)

  17.         if self.rect.right < 0:
  18.             self.rect.left = self.width
  19.             
  20.         if self.rect.left > self.width:
  21.             self.rect.right = 0

  22.         if self.rect.bottom < 0:
  23.             self.rect.top = self.height
  24.             
  25.         if self.rect.top > self.height:
  26.             self.rect.bottom = 0

  27.         


  28. def main():
  29.     pygame.init()

  30.     ball_image = 'W020130917263500532559.png'#素材
  31.     bg_image = '780 (1).png'#素材

  32.     running = True

  33.     bg_size = width,height = 780,520#1024,681
  34.     screen = pygame.display.set_mode(bg_size)
  35.     pygame.display.set_caption('玩球')

  36.     background = pygame.image.load(bg_image).convert_alpha()

  37.     balls = []

  38.     for i in range(5):
  39.         position = randint(0,width-100),randint(0,height-100)
  40.         speed = [randint(-10,10),randint(-10,10)]
  41.         ball = Ball(ball_image,position,speed,bg_size)
  42.         balls.append(ball)

  43.     clock = pygame.time.Clock()
  44.    
  45.     while running:
  46.         for event in pygame.event.get():
  47.             if event.type == QUIT:
  48.                 sys.exit()

  49.             screen.blit(background,(0,0))
  50.             for each in balls:
  51.                 each.move()
  52.                 screen.blit(each.image,each.rect)

  53.             pygame.display.flip()
  54.             clock.tick(60)
  55.             

  56. if __name__ == "__main__":
  57.     main()

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

使用道具 举报

 楼主| 发表于 2020-7-30 09:46:44 | 显示全部楼层
我懂了,是缩进的问题,我把
  1. screen.blit(background,(0,0))
  2.         for each in balls:
  3.             each.move()
  4.             screen.blit(each.image,each.rect)

  5.         pygame.display.flip()
  6.         clock.tick(10)
复制代码

这段代码放进了
  1. for event in pygame.event.get():
复制代码
里,造成了卡顿
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 04:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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