2017-04-02 111 views
1

我试图在pygame中绘制一个振荡的矩形。math.pi在pygame中没有像预期的那样工作

当我使用

particle.pos[0] = 100 * math.sin(188.5 * t) + screen_width/2 

它的作品,因为我希望它,但是当我使用

omega = 2*math.pi*fps 
particle.pos[0] = 100 * math.sin(omega * t) + screen_width/2 

的绘制矩形,但不动。我已经证实欧米茄约为188.5,欧米茄和188.5都是漂浮物。我唯一能想到的就是math.pi会以某种方式导致问题,但我不知道为什么。

编辑: 整个事情

import sys 
import math 
import pygame 
from pygame.locals import * 

pygame.init() 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 

fps = 30 
fpsClock = pygame.time.Clock() 

screen_width, screen_height = 640, 480 
screen = pygame.display.set_mode((screen_width, screen_height)) 


class Particle: 
    """Particle""" 
    def __init__(self, size, pos, particlecolor): 
     self.size = size 
     self.pos = pos 
     self.particlecolor = particlecolor 

    def draw(self): 
     pygame.draw.rect(screen, GREEN, [self.pos, self.size]) 

particle = Particle([10, 10], [screen_width * .25, screen_height * .5], GREEN) 

t = 0 
omega = 2*math.pi*fps 

while True: 
    t += 1 
    screen.fill(BLACK) 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    particle.pos[0] = 100 * math.sin(omega * t) + screen_width/2 
    # particle.pos[0] = 100 * math.sin(188.5 * t) + screen_width/2 

    particle.draw() 

    pygame.display.flip() 
fpsClock.tick(fps) 
+1

[更多代码请](http://stackoverflow.com/help/mcve)。 – skrx

+0

@skrx添加代码 – user44557

+0

考虑到当't'为0时,结果为320,'t'为100时为'319.9999999997962',我并不感到惊讶,当你在时间。 – zondo

回答

2

的问题是,您使用的2个* math.pi弧度倍数为你的角度(这将是360°(一个完整的圆)),所以你得到几乎表达式100 * math.sin(omega * t) + screen_width/2的结果相同。

print 100 * math.sin(omega * t) + screen_width/2 

输出:

319.99999999999784 
319.9999999999957 
319.99999999998784 
319.99999999999136 
319.9999999999949 
319.99999999997567 
319.99999999997925 

尝试omega = 0.1弧度得到一个不错的结果。

相关问题