2017-05-11 92 views
1

我正在嘗試製作投影運動的動畫,但它不起作用。 只出現一個黑色的窗口。投影對象的動畫使用pygame

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() 

我認爲問題是我的公式聲明,但我不知道如何解決它。

回答

1

閱讀評論。我改變了你計算時間和角度的方式。

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() 
+0

非常感謝。 –

+0

圓圈比我的預期慢。發生了什麼? –

+0

我需要更多信息。你給了什麼速度?角度?公式或常量可能存在問題。 – GLaDOS