Book Image

Raspberry Pi LED Blueprints

By : Agus Kurniawan
Book Image

Raspberry Pi LED Blueprints

By: Agus Kurniawan

Overview of this book

Table of Contents (14 chapters)
Raspberry Pi LED Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building a ball reflection game


A dot matrix display module consists of LEDs. Each LED can act as a pixel and can be used as a ball. In this section, we build a ball reflection game. If a ball hits a corner, it will bounce back. The ball can move with a specific speed, which is extracted as horizontal speed (vx) and vertical speed (vy). The following is the formula for moving the ball:

pos_x = pos_x + (vx * direction)
pos_y = pos_y + (vy * direction)

This formula uses object moving-based vectors with speeds vx and vy direction is a direction orientation. If the direction value is 1, the ball will move from left to right. Otherwise, it will move from right to left.

Let's start to build a program. Create a file named ch04_04.py. Write the following completed code:

# ch04_04.py file

import max7219.led as led
import time


device = led.matrix(cascaded=1)

# you can change these initial data
pos_x = 4  # current position x
pos_y = 4  # current position y
last_x = pos_x # last position x
last_y ...