2018-01-12 00:18:38 +01:00
#!/usr/bin/env python3
from PIL import Image , ImageDraw , ImageFont
from matrixSender import MatrixSender
import time
import numpy as np
from random import randint
matrix = MatrixSender ( udphost = " localhost " , udpport = 2323 , img_size = ( 160 , 24 * 6 ) , bitsperpixel = 2 )
if __name__ == ' __main__ ' :
2018-01-13 19:31:08 +01:00
_fps = 0
_time_startsending = 0
2018-01-12 00:18:38 +01:00
im = Image . new ( " RGBA " , matrix . _img_size , MatrixSender . C_BLACK ) #create image
draw = ImageDraw . Draw ( im ) #get draw instance
ball_pos = np . array ( ( 20 , 20 ) )
ball_size = 10
ball_vel = np . array ( ( - 5 , 5 ) )
2018-01-13 19:31:08 +01:00
2018-01-12 00:18:38 +01:00
while ( True ) :
#------- Update Movements ------
#move ball
ball_pos + = ball_vel
#check collisions with wall
if ball_pos [ 0 ] < = ball_size / 2 or ball_pos [ 0 ] > = im . size [ 0 ] - ball_size / 2 :
ball_vel [ 0 ] * = - 1 #x movement
if ball_pos [ 1 ] < = ball_size / 2 or ball_pos [ 1 ] > = im . size [ 1 ] - ball_size / 2 :
ball_vel [ 1 ] * = - 1 #y movement
#------- Graphics ------
#clear screen
draw . rectangle ( ( 0 , 0 , im . size [ 0 ] , im . size [ 1 ] ) , fill = ( 0 , 0 , 0 ) )
#draw ball
draw . ellipse ( ( ball_pos [ 0 ] - ball_size / 2 , ball_pos [ 1 ] - ball_size / 2 , ball_pos [ 0 ] + ball_size / 2 , ball_pos [ 1 ] + ball_size / 2 , ) , fill = ( 255 , 255 , 255 ) )
2018-01-13 19:31:08 +01:00
_fps = 1 / ( time . time ( ) - _time_startsending )
2018-01-13 19:11:17 +01:00
_time_startsending = time . time ( )
2018-01-12 00:18:38 +01:00
matrix . send ( im ) #construct udp packet and send to matrix
2018-01-13 19:11:17 +01:00
_time_endsending = time . time ( )
2018-01-13 19:31:08 +01:00
print ( " Sendtime= " + str ( round ( _time_endsending - _time_startsending , 4 ) ) + " s, maxFPS= " + str ( round ( 1 / ( _time_endsending - _time_startsending ) , 2 ) ) + " , actual FPS= " + str ( round ( _fps , 2 ) ) )
2018-01-12 00:18:38 +01:00
2018-01-13 19:31:08 +01:00
#time.sleep(.1) #wait