Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygame中设置图形动画(移动)_Python_Animation_Pygame_Draw - Fatal编程技术网

Python 如何在pygame中设置图形动画(移动)

Python 如何在pygame中设置图形动画(移动),python,animation,pygame,draw,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 = pyga

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

您没有更新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)
flip=update()


flip=update()

你说的动画是什么意思?一个精灵的不同图像?还是简单的运动?运动。让那个圆圈动起来。根据速度向量,圆圈在每次更新时应移动+1帧y和+1帧x。你说的动画是什么意思?一个精灵的不同图像?还是简单的运动?运动。让那个圆圈动起来。根据速度向量,圆圈在每次更新时应移动+1帧y和+1帧x。