Python pygame动画精灵问题
在小甲鱼pygame动画精灵那一章,我跟着小甲鱼写代码,但是写完后运行起来十分卡顿,请问这是为什么呢?代码如下import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.speed = speed
self.width,self.height = bg_size,bg_size
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.right < 0:
self.rect.left = self.width
if self.rect.left > self.width:
self.rect.right = 0
if self.rect.bottom < 0:
self.rect.top = self.height
if self.rect.top > self.height:
self.rect.bottom = 0
def main():
pygame.init()
ball_image = 'W020130917263500532559.png'#素材
bg_image = '780 (1).png'#素材
running = True
bg_size = width,height = 780,520#1024,681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('玩球')
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
position = randint(0,width-100),randint(0,height-100)
speed =
ball = Ball(ball_image,position,speed,bg_size)
balls.append(ball)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background,(0,0))
for each in balls:
each.move()
screen.blit(each.image,each.rect)
pygame.display.flip()
clock.tick(10)
if __name__ == "__main__":
main()
把 FPS 调大一点试试:
import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.speed = speed
self.width,self.height = bg_size,bg_size
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.right < 0:
self.rect.left = self.width
if self.rect.left > self.width:
self.rect.right = 0
if self.rect.bottom < 0:
self.rect.top = self.height
if self.rect.top > self.height:
self.rect.bottom = 0
def main():
pygame.init()
ball_image = 'W020130917263500532559.png'#素材
bg_image = '780 (1).png'#素材
running = True
bg_size = width,height = 780,520#1024,681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('玩球')
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
position = randint(0,width-100),randint(0,height-100)
speed =
ball = Ball(ball_image,position,speed,bg_size)
balls.append(ball)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background,(0,0))
for each in balls:
each.move()
screen.blit(each.image,each.rect)
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()
我懂了,是缩进的问题,我把screen.blit(background,(0,0))
for each in balls:
each.move()
screen.blit(each.image,each.rect)
pygame.display.flip()
clock.tick(10)
这段代码放进了for event in pygame.event.get():里,造成了卡顿
页:
[1]