/*
d2 button
d6 red led
d7 green led
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define red_led_pin 6
#define green_led_pin 7
#define button_pin 2
int status[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
long new_time = 0;
long jump_down_time = 0;
int periud = 100;
int jump_periud = 300;
int propery = 10;
int player_status = 0;
int scoce = 0;
int er_scose = 0;
void setup() {
// put your setup code here, to run once:
pinMode(red_led_pin, OUTPUT);
pinMode(green_led_pin, OUTPUT);
pinMode(button_pin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() > new_time) {
scoce+=1;
if (scoce*3 == 102) {
mwin();
}
move();
rendering();
new_time = millis() + periud;
}
if (digitalRead(button_pin) == 0 && player_status == 0) {
player_up();
rendering();
jump_down_time = millis() + jump_periud;
}
if (millis() > jump_down_time && player_status == 1){
player_down();
rendering();
}
if (player_status == 0 && status[0] == 'a'){
er_scose += 1;
scoce -= 1;
mfail();
if (er_scose == 3) {
er_scose = 0;
fail();
}
}
}
void player_up() {
player_status = 1;
}
void player_down() {
player_status = 0;
}
void rendering() {
if (player_status == 1){
lcd.setCursor(0,0);
lcd.print('b');
lcd.setCursor(0,1);
lcd.print(status[0]? 'a': '_');
}else{
lcd.setCursor(0,1);
lcd.print('b');
lcd.setCursor(0,0);
lcd.print(' ');
}
lcd.setCursor(1,1);
for (int i=1; i<16; i++) {
lcd.print(status[i]? 'a': '_');
}
lcd.setCursor(12,0);
lcd.print(scoce);
}
void move() {
for (int i = 0; i<15; i++){
status[i] = status[i+1];
}
status[15] = random(0,100) < propery? 1: 0;
}
void mfail(){
digitalWrite(red_led_pin, HIGH);
delay(500);
digitalWrite(red_led_pin, LOW);
}
void fail(){
digitalWrite(red_led_pin, HIGH);
digitalWrite(green_led_pin, LOW);
delay(500);
digitalWrite(green_led_pin, HIGH);
digitalWrite(red_led_pin, LOW);
delay(500);
digitalWrite(red_led_pin, HIGH);
digitalWrite(green_led_pin, LOW);
delay(500);
digitalWrite(green_led_pin, HIGH);
digitalWrite(red_led_pin, LOW);
delay(500);
digitalWrite(green_led_pin, LOW);
}
void mwin(){
digitalWrite(green_led_pin, HIGH);
delay(500);
digitalWrite(green_led_pin, LOW);
}