#include <HUSKYLENS.h>
#include <LiquidCrystal_I2C.h>
#include "SoftwareSerial.h"
HUSKYLENS huskylens;
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySerial(2, 3);
bool game[16][16];
bool lcd_line_1_stat = 0, lcd_line_2_stat = 0;
short player_y = 0, difficulty = 10, mob_speed = 1000;
// player x remain 0
// difficulty: 10 easiest, 4 hardest
// mob_speed: 1000 slowest, 300 fastest
// this func let the use of lcd screen easier
void printlcd(int l, String msg) {
lcd.setCursor(0, l - 1);
lcd.print(msg);
}
// this func initiate the game's data
void gameinit() {
player_y = 0;
difficulty = 10;
mob_speed = 1000;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
game[i][j] = 0;
}
}
return;
}
// this function move all the "wall" forward 1 unit
void move_mob() {
if (game[0][0] == 1) {
game[0][0] = 0;
}
if (game[1][0] == 1) {
game[1][0] = 0;
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 16; j++) {
if (game[i][j] == 1) {
game[i][j] = 0;
game[i][j - 1] = 1;
}
}
}
return;
}
// this function gernate the "wall" randomly up and down on the right side of the game
void spawn_mob() {
short spawn_pos = random(0, 2);
game[spawn_pos][15] = 1;
return;
}
// this function invoked the api of huskylen and change the player's position basing on camera's data
void move_player() {
huskylens.request();
HUSKYLENSResult result = huskylens.read();
if (result.command == COMMAND_RETURN_BLOCK) {
Serial.println(String()+F("Face x-Pos:")+result.xCenter);
if (result.xCenter > 160) {
player_y = 0;
} else {
player_y = 1;
}
}
}
// this function check if the player collided with the wall (the coordinate of player and the wall will be same)
bool check_collide() {
if (game[0][0] && player_y == 0) {
return true;
} else if (game[1][0] && player_y == 1) {
return true;
} else {
return false;
}
}
// this function make the game harder every few period
void change_difficulty(int score) {
if (score % 30 == 0 && difficulty > 4) {
difficulty -= 1;
}
if (score % 20 == 0 && mob_speed > 300) {
mob_speed -= 100;
}
}
// this function renders the game (game array and player_y variable) onto the screen
void render_game() {
Serial.println("Game:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 16; j++) {
Serial.print(game[i][j]);
}
Serial.println();
}
lcd.clear();
//first line
lcd.setCursor(0, 0);
for (int i = 0; i < 16; i++) {
if (game[0][i] == 1) {
lcd.write(B11111111);
} else if (game[0][i] == 0) {
lcd.print(" ");
}
}
//second line
lcd.setCursor(0, 1);
for (int i = 0; i < 16; i++) {
if (game[1][i] == 1) {
lcd.write(B11111111);
} else if (game[1][i] == 0) {
lcd.print(" ");
}
}
//player
if (player_y == 0) {
lcd.setCursor(0, 0);
lcd.print(">");
} else {
lcd.setCursor(0, 1);
lcd.print(">");
}
return;
}
// arduino function, run once
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(7, INPUT);
pinMode(8, OUTPUT);
randomSeed(analogRead(A0));
if (!huskylens.begin(mySerial)) {
Serial.println(F("AI Camera Begin failed!"));
} else {
Serial.println(F("AI Camera Begin Success!"));
}
huskylens.writeAlgorithm(ALGORITHM_FACE_RECOGNITION);
}
// arduino function, its a loop
void loop() {
lcd.clear();
unsigned long lcd_line_1_time = 0, lcd_line_2_time = 0;
printlcd(1, "Please press the");
printlcd(2, "button to play!");
while (true) {
if (digitalRead(7)) break;
}
// count down + MUSIC!!
lcd.clear();
printlcd(1, " 3!");
tone(8, 1000);
delay(500);
noTone(8);
delay(500);
lcd.clear();
printlcd(1, " 2!");
tone(8, 1000);
delay(500);
noTone(8);
delay(500);
lcd.clear();
printlcd(1, " 1!");
tone(8, 1000);
delay(500);
noTone(8);
delay(500);
lcd.clear();
printlcd(1, " GO!");
tone(8, 1400);
delay(500);
noTone(8);
delay(500);
lcd.clear();
//in to the game! now, each loop is each fps
gameinit();
int score = 0;
bool is_lose = false;
while (true) {
// some debug msg
Serial.println("-----");
Serial.println(String()+F("Score:")+score);
Serial.println(String()+F("Speed:")+mob_speed);
Serial.println(String()+F("Difficulty:")+difficulty);
// move mob
move_mob();
// spawn mob;
if (score % difficulty == 0) {
spawn_mob();
}
// move player
move_player();
// check lose
is_lose = check_collide();
if (is_lose) {
printlcd(1, " GAME OVER!");
printlcd(2, "U bumped in wall");
//death music qwq
tone(8, 1000);
delay(500);
noTone(8);
tone(8, 600);
delay(500);
noTone(8);
tone(8, 300);
delay(500);
noTone(8);
delay(350);
lcd.clear();
printlcd(1, " GAME OVER!");
printlcd(2, "Score:");
lcd.setCursor(6, 1);
lcd.print(score);
delay(5000);
break;
}
// add score, this also control the game difficulty
score += 1;
// render the game to lcd screen
render_game();
// change difficulty
change_difficulty(score);
// we need to wait a short period of time, do we?
delay(mob_speed);
}
}
Arduino UNO Project: Move Ur Head — Arduino C Program
最后更新于 2024-06-06 574 次阅读
Comments NOTHING