Back

Harmonic Wave

Sript description

This is a demonstration of the harmonic wave function:

Example

Result

Example

Source Code

import pygame
import math
import time

pygame.init()

WIDTH, HEIGHT = 1800, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Harmonic wave")

A = 100
T = 1/5
λ = 100
def displayText(text, size, color, x,y):
    font = pygame.font.Font('freesansbold.ttf', size)
    text = font.render(str(text), True, (color))
    textRect = text.get_rect()
    textRect.center = (x, y)
    WIN.blit(text, textRect)

def drawballat(x, y, size):
    pygame.draw.circle(WIN, (255, 255, 255), (x+WIDTH/2, y+HEIGHT/2), size)

def y(x,t):
    return A* math.sin(t/T-x/λ)

def renderScreen():
    WIN.fill((0,0,0))
    displayText("FPS: "+ str(round(FPS)), 10, (255,255,255), 20,20)
    pygame.draw.line(WIN, (200, 200, 200), (0, (HEIGHT / 2)), (WIDTH, (HEIGHT / 2)))
    pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2, 0), (WIDTH/2, HEIGHT))
    for i in range(round(WIDTH/2)):
        drawballat(i, y(i, totalFrames/45), 2)

totalFrames = 0
startTime = time.time
running = True
FPS = 0
prevFrame = 0
newFrame = 0
while running:
    totalFrames+=1
    newFrame = time.time()
    FPS = 1/(newFrame-prevFrame)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    renderScreen()
    pygame.display.update()
    pygame.time.delay(16)
    prevFrame = newFrame

pygame.quit()