DFI/client_sw/movingball_example.py
2018-01-13 19:31:08 +01:00

47 lines
1.6 KiB
Python

#!/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__':
_fps=0
_time_startsending=0
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))
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))
_fps=1/(time.time()-_time_startsending)
_time_startsending=time.time()
matrix.send(im) #construct udp packet and send to matrix
_time_endsending=time.time()
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)))
#time.sleep(.1) #wait