-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlinking_window.py
More file actions
75 lines (61 loc) · 2.18 KB
/
Blinking_window.py
File metadata and controls
75 lines (61 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 6 10:10:36 2019
@author: ALU
"""
import threading
import pygame
import time
from math import sin, pi
NUM_OF_THREAD = 6
b = threading.Barrier(NUM_OF_THREAD)
def blinking_block(points, frequency):
COUNT = 1
CLOCK = pygame.time.Clock()
''' FrameRate '''
FrameRate = 144
b.wait() #Synchronize the start of each thread
while True: #execution block
CLOCK.tick(FrameRate)
tmp = sin(2*pi*frequency*(COUNT/FrameRate))
color = 255*(tmp>0)
block = pygame.draw.polygon(win, (color, color, color), points, 0)
pygame.display.update(block) #can't update in main thread which will introduce delay in different block
COUNT += 1
if COUNT == FrameRate:
COUNT = 0
# print(CLOCK.get_time()) #check the time between each frame (144HZ=7ms; 60HZ=16.67ms)
if __name__ == '__main__':
pygame.init()
pygame.TIMER_RESOLUTION = 1 #set time resolutions
win = pygame.display.set_mode((1280,640))
#background canvas
bg = pygame.Surface(win.get_size())
bg = bg.convert()
bg.fill((0,0,0)) # black background
#display
win.blit(bg, (0,0))
pygame.display.update()
pygame.display.set_caption("Blinking")
''' frequency '''
frequency = [8,9,10,11,12,13] #frequency bank
''' POINTS '''
POINTS = [[(1175,0),(1070,210),(1280,210)], #takeoff
[(1175,640),(1070,430),(1280,430)], #land
[(425,0),(530,210),(320,210)], #forward
[(425,640),(530,430),(320,430)], #backward
[(0,320),(210,425),(210,215)], #left
[(850,320),(640,425),(640,215)]] #right
threads = []
for i in range(6):
threads.append(threading.Thread(target=blinking_block, args=(POINTS[i],frequency[i])))
threads[i].setDaemon(True)
threads[i].start()
RUN = True
while RUN:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
pygame.time.delay(100)
pygame.quit()
quit()