Python:Pygame:如何动画球以类似 sin() 的模式移动?

Man*_*ina 1 python animation pygame trigonometry

你会如何让球像 sin() 图那样以重复的波浪模式移动?

ago*_*bel 5

您可以使用计数器、pygame'sClock或只是pygame.time.get_ticks计算时间。下面是一些示例代码,可帮助您入门。

import math
import pygame

pygame.init()
screen = pygame.display.set_mode((400,400))

while True:
    t = pygame.time.get_ticks() / 2  % 400 # scale and loop time
    x = t
    y = math.sin(t/50.0) * 100 + 200       # scale sine wave
    y = int(y)                             # needs to be int

    screen.fill((0,0,0))
    pygame.draw.circle(screen, (255,255,255), (x, y), 40)

    pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)