add simple movingball example

This commit is contained in:
Fisch 2018-01-12 00:18:38 +01:00
parent 5dec7f5884
commit c065795134
1 changed files with 40 additions and 0 deletions

View File

@ -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