Python math.pi在pygame中未按预期工作
我想在pygame中画一个振荡矩形 当我使用Python math.pi在pygame中未按预期工作,python,pygame,Python,Pygame,我想在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不
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)
问题是使用2*math.pi弧度的倍数作为角度(即360°(一个整圆)),因此表达式
100*math.sin(ω*t)+屏幕宽度/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
弧度以获得一个好的结果。@skrx添加了编码考虑到t
为0时,结果为320,t
为100时,结果为319。999999999 7962
,当一次上升1时,它的移动非常小,我并不感到惊讶。