#MonthOfCode day 21 - wave
import pygame | |
import math | |
from time import sleep | |
SCREEN_W = 640 | |
SCREEN_H = 480 | |
class MovingPix(): | |
def __init__(self , x,y ): | |
self.x = x | |
self.y = y | |
def adjustPosition(self, period, max_period ): | |
self.y =SCREEN_H/2 + 32 * math.cos( | |
( float(period)/max_period + ( float(self.x) /SCREEN_W) ) * | |
2*math.pi ) | |
self.y = int(self.y) | |
def refreshScreen(): | |
window.fill( pygame.Color('black') ) | |
for pix in l_pix: | |
window.set_at( (pix.x, pix.y), pygame.Color('red')) | |
pygame.display.flip() | |
# init. pygame libr; create the screen and diplays a help message in the console | |
pygame.init() | |
window = pygame.display.set_mode( (SCREEN_W,SCREEN_H) ) | |
# init. list of all moving pixels | |
l_pix =list() | |
for i in xrange( SCREEN_W): | |
l_pix.append( MovingPix(i, SCREEN_H/2 )) | |
program_done = False | |
period = 0 | |
while not program_done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
program_done = True | |
break | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
l_pix[ 10 ].y = 30 | |
# simulating energy transmission | |
for movingpix in l_pix: | |
movingpix.adjustPosition( period, 16) | |
period+=1 | |
period=period%16 | |
refreshScreen() | |
sleep(0.05) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment