[英]How to animate pacman mouth opening and closing in pygame?
我想画一个吃豆人,它可以使用 python pygame 打开和关闭嘴巴。我找不到合适的文档。
我尝试使用此代码来回绘制圆弧和圆,它可以正常工作,但生成的形状看起来好像没有像我只绘制圆时那样正确填充黄色。
def draw(self, screen, count):
if count >0 and count <= 50:
pygame.draw.circle(screen, (255,255,0), (int(self.pos.x)+16, int(self.pos.y)+16), 16)
else:
if self.direction == Vector2D(1, 0):
pygame.draw.arc(screen, (255, 255, 0), (int(self.pos.x), int(self.pos.y), 32, 32),
0.7853981634, 5.4977871438, 16)
elif self.direction == Vector2D(-1, 0):
pygame.draw.arc(screen, (255, 255, 0), (int(self.pos.x), int(self.pos.y), 32, 32),
-2.356194490, 2.356194490, 16)
elif self.direction == Vector2D(0, -1):
pygame.draw.arc(screen, (255, 255, 0), (int(self.pos.x), int(self.pos.y), 32, 32),
2.3561944902, 7.0685834706, 16)
elif self.direction == Vector2D(0, 1):
pygame.draw.arc(screen, (255, 255, 0), (int(self.pos.x), int(self.pos.y), 32, 32),
-0.7853981634, 3.926990817, 16)
你的方法在我看来是可行的。 这里有证据:
from math import radians
import pygame, sys
from pygame.locals import *
FPS = 30 # frames per second
MOUTH_EVENT = USEREVENT+1
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
class Point(object):
def __init__(self, x=0, y=0):
self.x, self.y = x, y
class PacMan(object):
closed_angles = radians(0), radians(360)
opened_angles = radians(45), radians(315)
color = YELLOW
thickness = 16
def __init__(self, x=0, y=0, width=1, height=1):
self.pos = Point(x, y)
self.width, self.height = width, height
self.mouth_closed = False
def toggle_mouth(self):
self.mouth_closed = not self.mouth_closed
def draw(self, surface):
if self.mouth_closed:
pygame.draw.arc(surface, self.color,
(int(self.pos.x), int(self.pos.y),
self.width, self.height),
self.closed_angles[0], self.closed_angles[1],
self.thickness)
else:
pygame.draw.arc(surface, self.color,
(int(self.pos.x), int(self.pos.y),
self.width, self.height),
self.opened_angles[0], self.opened_angles[1],
self.thickness)
def main():
pygame.init()
fpsclock = pygame.time.Clock()
screen = pygame.display.set_mode((500,400), 0, 32)
screen.fill(BLACK)
pacman = PacMan(250-25, 200-25, 50, 50)
pygame.time.set_timer(MOUTH_EVENT, 333)
while True: # display update loop
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == MOUTH_EVENT:
pacman.toggle_mouth()
continue
if event.type == QUIT:
pygame.quit()
sys.exit()
pacman.draw(screen)
pygame.display.update()
fpsclock.tick(FPS)
main()
这是物体在嘴巴闭合和张开时的样子:
[
You can access a Live demo with editor
https://codepen.io/wifi/pen/olKxE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.