Python 用pygame生成和绘制正弦波

Python 用pygame生成和绘制正弦波,python,pygame,Python,Pygame,我试图产生并绘制一个罪恶波。我使用的是我在网上找到的这个公式y=Amp*sin(2*PI*频率*时间+移位) 导入pygame 输入数学 导入时间 window=pygame.display.set_模式((600600)) 课程点: 定义初始化(自): self.x=0 self.y=0 类别行: 定义初始化(自): self.points=[] def生成线(起始点、N点、长度、y): 行=行() 对于范围内的i(n点): p=点() p、 x=startX+((i/N点)*长度) p、 y

我试图产生并绘制一个罪恶波。我使用的是我在网上找到的这个公式
y=Amp*sin(2*PI*频率*时间+移位)

导入pygame
输入数学
导入时间
window=pygame.display.set_模式((600600))
课程点:
定义初始化(自):
self.x=0
self.y=0
类别行:
定义初始化(自):
self.points=[]
def生成线(起始点、N点、长度、y):
行=行()
对于范围内的i(n点):
p=点()
p、 x=startX+((i/N点)*长度)
p、 y=y
line.points.append(p)
回流线;
nPoints=100
直线=发电机线(10个点,590300)
开始=时间。时间()
accNPoints=0
频率=100
振幅=30
总体=300
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
窗口填充((255,255,255))
keys=pygame.key.get_pressed()
如果(键[pygame.K_a]):频率-=0.002
如果(键[pygame.K_d]):频率+=0.002
如果(键[pygame.K_s]):振幅-=0.05
如果(键[pygame.K_w]):振幅+=0.05
如果(键[pygame.K_q]):总体+=0.5
如果(键[pygame.K_e]):总体上-=0.5
如果(键[pygame.K_p]):accNPoints+=0.5
如果(键[pygame.K_o]):accNPoints-=0.5
如果accNPoints>50:
直线=发电机线(10个点,590300)
accNPoints=0
nPoints+=1
elif账户点<-50:
直线=发电机线(10个点,590300)
accNPoints=0
nPoints-=1
对于范围内的i(1,len(线点)):
#基于x计算y
#y=A*sin(2*PI*f*t+shift)
#yStart=(振幅*math.sin(2*math.pi*频率*((time.time()-start)*0.01)+线点[i].x))+总体
#yEnd=(振幅*math.sin(2*math.pi*频率*((time.time()-start)*0.01)+线点[i-1].x))+总体
yStart=(振幅*math.sin(2*math.pi*频率+线点[i].x))+总体
yEnd=(振幅*math.sin(2*math.pi*频率+线点[i-1].x))+总体
pygame.draw.circle(窗口,(255,0,0),(线点[i].x,yStart),1)
pygame.draw.circle(窗口,(255,0,0),(直线点[i-1].x,yEnd),1)
pygame.draw.aaline(
窗口,
(0, 0, 0),
(直线点[i].x,yStart),
(直线点[i-1].x,yEnd)
)
pygame.display.flip()

似乎有两个问题。改变
频率
值似乎并不会真正改变波的频率。频率似乎取决于生成行的函数中的
nPoints
变量,即
def generateLine(startX,nPoints,length,y):
公式错误。x坐标取决于回路的控制变量(
i
)。y坐标需要依赖于x坐标:

e、 g:频率5(5波)

频率=5
振幅=50
总体=300
尽管如此:
# [...]
no_pts=window.get_width()
对于范围内的i(无点):
x=i/no_pts*2*math.pi
y=(振幅*数学余弦(x*频率))+整体
如果i>0:
pygame.draw.aaline(窗口,(0,0,0),上一部分,(i,y))
上一部分=(i,y)
# [...]

import pygame
import math
import time

window = pygame.display.set_mode((600, 600))

class Point:
    def __init__(self):
        self.x = 0
        self.y = 0

class Line:
    def __init__(self):
        self.points = []

def generateLine(startX, nPoints, length, y):
    line = Line()
    for i in range(nPoints):
        p = Point()
        p.x = startX + ((i / nPoints) * length)
        p.y = y
        line.points.append(p)
    return line;

nPoints = 100
line = generateLine(10, nPoints, 590, 300)
start = time.time()
accNPoints = 0
frequency = 100
amplitude = 30
overallY = 300

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    window.fill((255, 255, 255))

    keys = pygame.key.get_pressed()
    
    if (keys[pygame.K_a]): frequency -= 0.002
    if (keys[pygame.K_d]): frequency += 0.002

    if (keys[pygame.K_s]): amplitude -= 0.05
    if (keys[pygame.K_w]): amplitude += 0.05

    if (keys[pygame.K_q]): overallY += 0.5
    if (keys[pygame.K_e]): overallY -= 0.5

    if (keys[pygame.K_p]): accNPoints += 0.5
    if (keys[pygame.K_o]): accNPoints -= 0.5

    if accNPoints > 50:
        line = generateLine(10, nPoints, 590, 300)
        accNPoints = 0
        nPoints += 1
    elif accNPoints < -50:
        line = generateLine(10, nPoints, 590, 300)
        accNPoints = 0
        nPoints -= 1

    for i in range(1, len(line.points)):
        #calculate y based on x
        #y = A * sin(2 * PI * f * t + shift)
        #yStart = (amplitude * math.sin(2 * math.pi * frequency * ((time.time() - start) * 0.01) + line.points[i].x))     + overallY
        #yEnd =   (amplitude * math.sin(2 * math.pi * frequency * ((time.time() - start) * 0.01) + line.points[i - 1].x)) + overallY
        yStart = (amplitude * math.sin(2 * math.pi * frequency + line.points[i].x))     + overallY
        yEnd =   (amplitude * math.sin(2 * math.pi * frequency + line.points[i - 1].x)) + overallY

        
        pygame.draw.circle(window, (255, 0, 0), (line.points[i].x, yStart), 1)
        pygame.draw.circle(window, (255, 0, 0), (line.points[i - 1].x, yEnd), 1)
            
        pygame.draw.aaline(
            window,
            (0, 0, 0),
            (line.points[i].x, yStart),
            (line.points[i - 1].x, yEnd)
            )

    
    pygame.display.flip()