1

我一直在尝试为绘图元素设置动画但没有成功。我可以为导入的图像设置动画,但是当我尝试为 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)

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

我究竟做错了什么?你如何为绘图元素设置动画?

谢谢你的时间。

4

2 回答 2

3

您没有更新 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)
于 2012-11-23T11:57:05.893 回答
1
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]
    ball_pos =(ballx, bally)

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

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

翻转 = 更新()

于 2014-07-16T21:38:02.677 回答