【问题标题】:Animation of a projection object use pygame投影对象的动画使用 pygame
【发布时间】:2017-05-11 01:11:54
【问题描述】:

我正在尝试制作投影运动的动画,但它不起作用。 只出现一个黑色窗口。

import math
import time
import pygame

V0 = int(input("please enter initial speed(m/s)"))
angle = int(input("please enter throwing angle"))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
clock = pygame.time.Clock()

while not done:
    T = time.clock()
    x = int(math.cos(angle)*V0*T)
    y = int((math.sin(angle)*V0*T)-1/2*(9.8*T*T))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(BLACK)
    pygame.draw.circle(screen, WHITE, (x, y), 5, 0)

    pygame.display.flip()
    clock.tick(10)

pygame.quit()

我认为问题在于我的公式语句,但我不知道如何解决它。

【问题讨论】:

    标签: python-3.x animation pygame


    【解决方案1】:

    阅读 cmets。我改变了你计算时间和角度的方式。

    import math
    import time
    import pygame
    
    V0 = int(input("please enter initial speed(m/s)"))
    angle = int(input("please enter throwing angle"))
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    done = False
    clock = pygame.time.Clock()
    T = 0 #initialize
    
    while not done:
        #T = time.clock() #don't use
    
        x = int(math.cos(math.radians(angle))*V0*T) #use math.radians(degree). Don't just provide degrees. cos() and sin() needs input in radians
        y = 300 - int((math.sin(math.radians(angle))*V0*T)-(0.5*9.8*T*T)) #looks better. projectile starts from the ground.
    
        for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        screen.fill(BLACK)
        pygame.draw.circle(screen, WHITE, (x,y), 5, 0)
    
        milliseconds = clock.tick(60)   #try this. count ms. add to s. calculate total time passed.
        seconds = milliseconds / 1000.0 
        T += seconds
    
        pygame.display.flip()
    
    pygame.quit()
    

    【讨论】:

    • 非常感谢。
    • 圆圈飞得比我想象的要慢。发生了什么?
    • 我需要更多信息。你给的速度是多少?角度?公式或常数可能有问题。
    • 我没有给出速度。速度取决于时间 (T) 过得有多快。
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多