python - 如何在pygame中为绘图设置动画(运动)

标签 python animation pygame draw

我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。

编辑:“动画”是指“移动”。就像使圆在 x 和 y 方向上移动一样。

这是我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 60
WIDTH = 600
HEIGHT = 500
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ballx = WIDTH / 2
bally = HEIGHT / 2
ball_vel = [1, 1]
ball_pos =(ballx, bally)
RADIUS = 20

# Game Loop:
while True:
    # Check for quit event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Erase the screen (I have tried with and without this step)
    DISPLAYSURF.fill(BLACK)

    # Update circle position
    ballx += ball_vel[0]
    bally += ball_vel[1]

    # Draw Circle (I have tried with and without locks/unlocks)
    DISPLAYSURF.lock()
    pygame.draw.circle(DISPLAYSURF, WHITE, ball_pos, RADIUS, 2)
    DISPLAYSURF.unlock()

    # Update the screen
    pygame.display.update()
    fpsClock.tick(FPS)

我已经尝试过锁定/解锁显示表面和不锁定显示表面(如文档所示)。在更新屏幕之前,我尝试过删除屏幕和不删除屏幕(正如一些教程所建议的那样)。我只是无法让它工作。

我做错了什么?你如何为绘图元素制作动画?

感谢您的宝贵时间。

最佳答案

您没有更新 ball_pos 元组:您将它设置为起始坐标:

ballx = WIDTH / 2
bally = HEIGHT / 2
ball_vel = [1, 1]
ball_pos =(ballx, bally)

您随后更新了 ballx 和 bally,但再也没有将 ball_pos 设置为 ballx 和 bally。 在 while 循环中,设置 ballx 和 bally 后,执行以下操作:

ball_pos = (ballx,bally)

关于python - 如何在pygame中为绘图设置动画(运动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13527402/

相关文章:

python - 将新元组加入 Python 中的第一个元组列表

python-3.x - Pygame中的多个显示

python - 当某些列值为空时,如何合并 Dataframe 中的多行?

python - 新的 Azure 订阅未与 azure python sdk 一起列出

javascript - 如何在fabric js中重写canvas.add()方法

Android翻转图片动画

wpf - 为什么 WPF 中的 TextBox.Text 不可设置动画?

python - 将使用 pygame 的 python 程序编译为可执行文件

Python pygame 角色闲置动画

python - 类型错误:Python 中 + 不支持的操作数: 'dict' 和 'str'