Code: Select all
#include "Adafruit_LEDBackpack.h" // Inlcude libraries
#include "Adafruit_GFX.h"
#include "gfxfont.h"
#include <Wire.h>
Adafruit_8x8matrix matrix[4] = Adafruit_8x8matrix();// Declare an array of 4 8x8 matrices
int APin = 2; // Set the pin information
int DPin = 11;
int UPin = 13;
int RPin = 10;
int LPin = 12;
int AState = 1; // Set variable for the button states
int DState = 1;
int UState = 1;
int RState = 1;
int LState = 1;
int pixPosX = 0; // Create the variable for Pac-Man's position on the matrix
int pixPosY = 0;
bool isTarCaught = false; // Create the variable for when the target is caught
int tarPosX = random (0, 15); // Set the target to have a random position
int tarPosY = random (0, 15);
int score = 0; // Create a variable for tracking the score.
void setup() {
// Here is where the setup code goes to run once
for (uint8_t i = 0; i < 4; i++) {
matrix[i] = Adafruit_8x8matrix();
matrix[i].begin(0x70 + i);
matrix.setRotation(1); //Set rotation of the screen to the right
matrix.setTextWrap(false); // Set text wrapping to off for the screen
pinMode(APin, INPUT_PULLUP); // Intialise pins
pinMode(DPin, INPUT_PULLUP);
pinMode(UPin, INPUT_PULLUP);
pinMode(RPin, INPUT_PULLUP);
pinMode(LPin, INPUT_PULLUP);
for (int C1 = 15; C1 >= -155; C1--) { // Start welcome text scroll across screen
matrix.print("Pac-Man USW Demo Test 1"); // Print welcome text
matrix.writeDisplay(); // Force the text to stay on the screen
delay(75); // Wait 75 ms
matrix.clear(); // Clear Screen
matrix.setCursor(C1, 0); // Set cursor to the next pixel
}
}
}
void loop() { //main code starts here to repeat
AState = digitalRead(APin); //Button press check
DState = digitalRead(DPin);
UState = digitalRead(UPin);
RState = digitalRead(RPin);
LState = digitalRead(LPin);
if (isTarCaught == true) { //checks if the target is caught
score = score + 1;
matrix.clear();
matrix.drawLine(1, 4, 3, 6, LED_ON); // Draw A Tick
matrix.drawLine(3, 6, 6, 0, LED_ON);
matrix.writeDisplay(); // Force the tick to stay on the screen
delay(1000); // Hold for 1s
tarPosX = random(0, 15); //set random x axis pos for target
tarPosY = random(0, 15); //set random y axis pos for target
isTarCaught = false; //mark target not caught
pixPosX = 0; // Set Pac-mans position to 0,0
pixPosY = 0;
matrix.clear(); // Clear Screen
}
matrix.drawPixel(tarPosX, tarPosY, LED_ON); // Draw the Target on the screen
matrix.writeDisplay(); // Hold the target on the screen until it is caught
if (UState == 0) { // Move Pac-Man Up if U button is pressed
pixPosY = pixPosY - 1;
matrix.clear();
}
if (DState == 0) { // Move Pac-Man Down if D button is pressed
pixPosY = pixPosY + 1;
matrix.clear();
}
if (LState == 0) { // Move Pac-Man Left if L button is pressed
pixPosX = pixPosX - 1;
matrix.clear();
}
if (RState == 0) { // Move Pac-Man Right if L button is pressed
pixPosX = pixPosX + 1;
matrix.clear();
}
matrix.drawPixel(pixPosX, pixPosY, LED_ON); // Draw Pac-Man
matrix.drawPixel((pixPosX + 1), pixPosY, LED_ON);
matrix.drawPixel(pixPosX, (pixPosY + 1), LED_ON);
matrix.drawPixel(pixPosX + 1), (pixPosY + 1), LED_ON;
matrix.writeDisplay(); // Force Pac-Man to remain on the screen
if (pixPosX == tarPosX && pixPosY == tarPosY) { // Checks if Pac-Man's position matches that of the target
isTarCaught = true
}
if ((pixPosX + 1) == tarPosX && pixPosY == tarPosY) {
isTarCaught = true
}
if (pixPosX == tarPosX && (pixPosY + 1) == tarPosY) {
isTarCaught = true
}
if ((pixPosX + 1) == tarPosX && (pixPosY + 1) == tarPosY) {
isTarCaught = true
}
if (score == 10) { // Score check = 10?
for (int c6 = 0; c6 <= 2; c6++) { // Makes fireworks appear 3 times
matrix.clear(); // Clear Screen
matrix.drawPixel(7, 3, LED_ON);
matrix.drawPixel(7, 4, LED_ON);
matrix.drawPixel(8, 3, LED_ON);
matrix.drawPixel(8, 4, LED_ON);
matrix.writeDisplay(); //Make the first pixels to stay on the screen.
delay(500); //Wait 500 milliseconds.
matrix.clear(); //Clear the screen.
matrix.drawPixel(6, 2, LED_ON);
matrix.drawPixel(9, 2, LED_ON);
matrix.drawPixel(6, 5, LED_ON);
matrix.drawPixel(9, 5, LED_ON);
matrix.writeDisplay();
delay(500); //Wait 500 milliseconds.
matrix.clear(); // Clear screen
,
matrix.drawPixel(5, 1, LED_ON);
matrix.drawPixel(10, 1, LED_ON);
matrix.drawPixel(5, 6, LED_ON);
matrix.drawPixel(10, 6, LED_ON);
matrix.writeDisplay();
delay(500);
matrix.clear();
matrix.drawPixel(4, 0, LED_ON);
matrix.drawPixel(11, 0, LED_ON);
matrix.drawPixel(4, 7, LED_ON);
matrix.drawPixel(11, 7, LED_ON);
matrix.writeDisplay();
delay(500);
}
delay(500);
matrix.clear()
for (int C4 = 0; C4 <= 9999999; C4++) { // Repeat almost indefinite times
for (int C5 = 16; C5 >= -210; C5--) { // Scroll text across the screen
matrix.print("Press Reset to start again! :=)" ) // Text to be printed
delay(75);
matrix.clear(); // Clear Screen
matrix.setCursor(C5, 0); // Cursor to the next pixel
}
}
}
delay (100) // Wait 100 milliseconds
}