如何在pygame中打开和关闭pacman口的动画?

时间:2015-08-19 17:52:07

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

我想绘制一个可以使用python pygame打开和关闭它的嘴的pacman。我无法找到适当的文档。

我尝试使用此代码来回绘制圆弧和圆圈,它可以正常工作,但是当我绘制一个圆圈时,所得到的形状看起来就像没有正确填充黄色一样。

func widthForCell(_ maxWidth: CGFloat, _ requiredWidth: CGFloat, _ notification: Notification) -> CGFloat {
  switch notification.type {
    case "vote": return maxWidth - 80
    default: return requiredWidth
  }
}
let cellWidth = widthForCell(maxWidth, requiredWidth, notification)
println("cellWidth is \(cellWidth)")

2 个答案:

答案 0 :(得分:0)

在我看来,你的方法似乎有用。这是证据:

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

这是物体在嘴巴闭合和打开时的样子:

[screenshot of pacman show two states[1]

答案 1 :(得分:0)

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