我正在尝试在pygame中创建一个矩形的动画,但我很难在给定的帧速率下对其进行动画处理。
class Muscle(pygame.sprite.Sprite):
def __init__(self, screen, posX=200, posY=200, contraction=0.5, extension=1.5, length=10, thickness=5, min_len=0.2,max_len=1.5, power=20):
super(Muscle, self).__init__()
self.screen = screen
self.contraction = contraction
self.relaxation = relaxation
self.length = length
self.thickness = thickness
if power < 1:
self.power = 1
power = 1
else:
self.power = power
self.image = pygame.Surface((length, thickness))
color_index = 5 * power
self.image.fill((255, color_index, color_index))
self.rect = self.image.get_rect()
self.rect.x = posX
self.rect.y = posY
def render(self):
self.screen.blit(self.image, (self.rect.x, self.rect.y))
def contract(self):
expected_width = self.length * self.contraction
counter = 0
while self.image.get_width() > expected_width:
self.image = pygame.transform.scale(self.image, (self.rect.width, self.image.get_height()))
self.rect.x += 1;
self.rect.width -= 2
self.render()
counter += 1
def extend(self):
expected_width = self.length * self.relaxation
counter = 0
while self.image.get_width() < expected_width:
self.image = pygame.transform.scale(self.image, (self.rect.width, self.image.get_height()))
self.rect.x -= 1;
self.rect.width += 2
self.render()
#print("Relaxation:" + str(counter))
counter += 1
如果我单独调用合约并扩展方法,他们会成功调整矩形的大小,但我想随着时间的推移执行它,而不会中断绘制环境和精灵的主循环。
答案 0 :(得分:1)
最简单的解决方案是每帧调用extend
或contract
一次,但动画将受到帧速率限制。
import pygame
class Muscle(pygame.sprite.Sprite):
def __init__(self, screen, posX=200, posY=200, contraction=0.5,
extension=1.5, length=40, thickness=20, min_len=0.2,max_len=1.5, power=20):
super(Muscle, self).__init__()
self.screen = screen
self.contracting = False # To check if the muscle is contracting or extending.
self.contraction = contraction
self.relaxation = 1
self.length = length
self.thickness = thickness
if power < 1:
self.power = 1
power = 1
else:
self.power = power
self.image = pygame.Surface((length, thickness))
color_index = 5 * power
self.image.fill((255, color_index, color_index))
self.rect = self.image.get_rect()
self.rect.x = posX
self.rect.y = posY
# This method gets called every frame.
def update(self):
if self.contracting:
self.contract()
else:
self.extend()
def render(self):
self.screen.blit(self.image, (self.rect.x, self.rect.y))
def contract(self):
expected_width = self.length * self.contraction
counter = 0
if self.image.get_width() > expected_width:
self.image = pygame.transform.scale(self.image, (self.rect.width, self.image.get_height()))
self.rect.x += 1
self.rect.width -= 2
self.render()
counter += 1
def extend(self):
expected_width = self.length * self.relaxation
counter = 0
if self.image.get_width() < expected_width:
self.image = pygame.transform.scale(self.image, (self.rect.width, self.image.get_height()))
self.rect.x -= 1
self.rect.width += 2
self.render()
counter += 1
def main():
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
muscle = Muscle(screen)
all_sprites = pygame.sprite.Group(muscle)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Change the state of the muscle.
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
muscle.contracting = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_c:
muscle.contracting = False
all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
pygame.init()
main()
pygame.quit()
或者,您可以在一段时间间隔后致电extend
或contract
。查看these answers以了解如何实施计时器。