From c065795134d642e88c9f9098ed340bfcd02a0ae6 Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 12 Jan 2018 00:18:38 +0100 Subject: [PATCH] add simple movingball example --- client_sw/movingball_example.py | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 client_sw/movingball_example.py diff --git a/client_sw/movingball_example.py b/client_sw/movingball_example.py new file mode 100644 index 0000000..ed2bff4 --- /dev/null +++ b/client_sw/movingball_example.py @@ -0,0 +1,40 @@ +#!/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__': + 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)) + + + matrix.send(im) #construct udp packet and send to matrix + + time.sleep(.1) #wait