Question: For this assignment we are using a rasberry pi with breadboards attached and an 8x8 LED matrix controlled by a joystick. We must figure out
For this assignment we are using a rasberry pi with breadboards attached and an 8x8 LED matrix controlled by a joystick. We must figure out how to make a pong game on the LED matrix. When run, there is a paddle on the bottom of the matrix length of 3 dots, and a ball lenght of 1 dot will drop from the top and you must hit the ball with the paddle and the ball will go up then back down and repeat until you miss the ball then game over. Need help finishing the code below to get the paddle and ball created can anyone help?
import RPi.GPIO as GPIO import time import random
# from joystick and PCF8591 import smbus address = 0x48 bus=smbus.SMBus(1) cmd=0x40 Z_Pin = 18 #define pin for Z_Pin
####### # PCF 8591 ####### def analogRead(chn): #read ADC value bus.write_byte(address,cmd+chn) value = bus.read_byte(address) #value = bus.read_byte_data(address,cmd+chn) return value
# Matrix display orientation LSBFIRST = 1 MSBFIRST = 2
#define the pins connect to 74HC595 dataPin = 17 #DS Pin of 74HC595(Pin14) latchPin = 27 #ST_CP Pin of 74HC595(Pin12) clockPin = 22 #SH_CP Pin of 74HC595(Pin11)
####### # Data ####### pic = [0x1c,0x22,0x51,0x45,0x45,0x51,0x22,0x1c]# data of smiling face #data = [ 0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00,] # "B"
data = [#data of "0-F" 0x00, 0x00, 0x3E, 0x41, 0x41, 0x3E, 0x00, 0x00, # "0" 0x00, 0x00, 0x21, 0x7F, 0x01, 0x00, 0x00, 0x00, # "1" 0x00, 0x00, 0x23, 0x45, 0x49, 0x31, 0x00, 0x00, # "2" 0x00, 0x00, 0x22, 0x49, 0x49, 0x36, 0x00, 0x00, # "3" 0x00, 0x00, 0x0E, 0x32, 0x7F, 0x02, 0x00, 0x00, # "4" 0x00, 0x00, 0x79, 0x49, 0x49, 0x46, 0x00, 0x00, # "5" 0x00, 0x00, 0x3E, 0x49, 0x49, 0x26, 0x00, 0x00, # "6" 0x00, 0x00, 0x60, 0x47, 0x48, 0x70, 0x00, 0x00, # "7" 0x00, 0x00, 0x36, 0x49, 0x49, 0x36, 0x00, 0x00, # "8" 0x00, 0x00, 0x32, 0x49, 0x49, 0x3E, 0x00, 0x00, # "9" 0x00, 0x00, 0x3F, 0x44, 0x44, 0x3F, 0x00, 0x00, # "A" 0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00, # "B" 0x00, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x00, # "C" 0x00, 0x00, 0x7F, 0x41, 0x41, 0x3E, 0x00, 0x00, # "D" 0x00, 0x00, 0x7F, 0x49, 0x49, 0x41, 0x00, 0x00, # "E" 0x00, 0x00, 0x7F, 0x48, 0x48, 0x40, 0x00, 0x00, # "F" ]
EMPTY = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # " " ]
# 6 BAR positions BAR = [ [ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 ], [ 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 ], [ 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00 ], [ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00 ], [ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00 ], [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 ], ]
def setup(): # 74H GPIO.setmode(GPIO.BCM) GPIO.setup(dataPin, GPIO.OUT) GPIO.setup(latchPin, GPIO.OUT) GPIO.setup(clockPin, GPIO.OUT) # Joystick GPIO.setup(Z_Pin,GPIO.IN,GPIO.PUD_UP) #set Z_Pin to pull-up mode
def shiftOut(dPin,cPin,order,val): for i in range(0,8): GPIO.output(cPin,GPIO.LOW); if(order == LSBFIRST): GPIO.output(dPin,(0x01&(val>>i)==0x01) and GPIO.HIGH or GPIO.LOW) elif(order == MSBFIRST): GPIO.output(dPin,(0x80&(val<
def myloop(): pos = 3 dir = 0 image = [] MID_VAL_X = 128 diff = 20
## game logic to be in place later while True: ## show the bar image = BAR[pos]
## LEDMatrix main control for j in range(0,20): ## for each row x=0x80 for i in range(0,8): GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,image[i]) shiftOut(dataPin,clockPin,MSBFIRST,~x) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.001) x>>=1
## joystick control new_val_X = analogRead(1) print ('value_X: %d ' %(new_val_X))
## Game bar control, using joystick if new_val_X > MID_VAL_X + diff : dir = 1 elif new_val_X < MID_VAL_X - diff : dir = -1 else: dir = 0 if not dir == 0: pos = pos + dir if( pos < 0 ): pos = 0 elif( pos > 5): pos = 5
time.sleep(0.1)
def destroy(): # When 'Ctrl+C' is pressed, the function is executed. bus.close() GPIO.cleanup()
if __name__ == '__main__': # Program starting from here print ('[main] Program is starting...' ) setup()
myloop() print ('[main] Program ended.' )
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
