[英]How to animate drawings in pygame (movement)
我一直在尝试为绘图元素制作动画但没有成功。 我可以对导入的图像进行动画处理,但是当我尝试对 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)
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)
flip = update()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.