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)
[更多代码请](http://stackoverflow.com/help/mcve)。 – skrx
@skrx添加代码 – user44557
考虑到当't'为0时,结果为320,'t'为100时为'319.9999999997962',我并不感到惊讶,当你在时间。 – zondo