How can I remove the line that runs through the middle of this sine wave?
import pygame, sys, math
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
points = []
screen = pygame.display.set_mode((500, 500))
while True:
clock.tick(60)
screen.fill((0, 0, 0))
# Draw Sine Wave
for i in range(500):
n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
points.append([i, n])
pygame.draw.lines(screen, (0, 255, 55), False, points, 2)
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.update()
You have two mistakes
Use False
instead of True
to draw open polygon
pygame.draw.lines(screen, (0, 255, 55), False, points, 2)
In every loop you add new points to list and then after last point (499,0)
you have (0,0)
which create vertical line from right to left. You can see it if you use print(len(point))
- you will get values 500,1000,1500,etc. You should clear points
in every loop. Or you should generate points
only once - before while
loop.
Clear points
before creating new list - it can be useful if you want animated wave.
while True:
clock.tick(60)
screen.fill((0, 0, 0))
points = [] # <--- clear it
# Draw Sine Wave
for i in range(500):
n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
points.append([i, n])
pygame.draw.lines(screen, (0, 255, 55), False, points, 1)
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.update()
Or generate it before loop - if you want static wave
# generate it only once - before `while` loop
points = []
# Draw Sine Wave
for i in range(500):
n = int(math.sin(i / 500 * 4 * math.pi) * 50 + 240)
points.append([i, n])
while True:
clock.tick(60)
screen.fill((0, 0, 0))
pygame.draw.lines(screen, (0, 255, 55), False, points, 1)
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.update()
EDIT:
Version which uses offset
to animate wave
import sys
import math
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500, 500))
offset = 0
clock = pygame.time.Clock()
while True:
# - all updates (without draws) -
points = []
# Draw Sine Wave
for i in range(500):
n = int(math.sin((i+offset) / 500 * 4 * math.pi) * 50 + 240)
points.append([i, n])
offset += 1
# - all draws (without updates) -
clock.tick(60)
screen.fill((0, 0, 0))
pygame.draw.lines(screen, (0, 255, 55), False, points[:500], 1)
pygame.display.update()
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()