dfi-led-matrix/client_sw/movingball_example.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-01-11 23:18:38 +00: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__':
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