【问题标题】:How to animate pacman mouth opening and closing in pygame?如何在pygame中为pacman嘴打开和关闭设置动画?
【发布时间】:2015-08-19 17:52:07
【问题描述】:

我想画一个吃豆人,它可以使用 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)

【问题讨论】:

  • 何不试试看?
  • 我尝试使用此代码绘制弧线,但生成的形状看起来好像没有正确填充颜色。 pygame.draw.arc(screen, (255,255,0), (int(pacman.pos.x), int(pacman.pos.y),width,height), 0.7853981634, 5.4977871438, 16)
  • 如果您已经尝试过并且无法正常工作,请显示您的代码
  • 似乎弧是一条线,而不是一个曲面。

标签: python python-2.7 animation pygame game-development


【解决方案1】:

在我看来,您的方法似乎可行。这是证据:

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()

这是物体张开嘴巴的样子:

[

【讨论】:

  • 是的,它的工作原理。我的形状也和你一样。我的问题是黄色表面内的那些小黑点。正如 Michael Billaud 在上面的 cmets 中指出的那样,我认为这与弧类似于线而不是曲面这一事实有关。
  • 我想您可以在绘画程序中手动创建两张图像——一张张开嘴,另一张张开嘴——然后将blit() 正确的一张在将它们加载到内存后显示在屏幕上。
【解决方案2】:

You can access a Live demo with editorhttps://codepen.io/wifi/pen/olKxE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多