繁体   English   中英

Python:Pygame:如何为球设置动画以使其像sin()一样运动?

[英]Python:Pygame: How to animate a ball to move in a sin() like pattern?

像sin()图那样,如何使球以重复的波浪状运动?

您可以使用计数器,pygame的Clock或仅使用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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM