Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <SoftwareSerial.h>
- SoftwareSerial mySer(8, 9);
- #define LED_PIN 8 // Data pin for led tape
- #define NUM_LEDS 80
- int button_move_R = 7;
- int button_move_L = 9;
- int button_attack = 5;
- int i = 0;
- int lastmovement;// 0 left, 1 right
- int direct;
- long previousTime = 0;
- CRGB leds[NUM_LEDS];
- //int enemyPos[80]; // has the enemy poition
- int playerPos[80];
- bool isAlive=true;
- struct enemy {
- bool live = true;
- int pos;
- };
- struct enemy enemies[7];
- int attack(int x, int y) {
- if (x+2 == y){
- enemies.pos[y] = 0;
- leds[x - 2] = CRGB(0,0,255);
- leds[x - 1] = CRGB(0,0,255);
- leds[x + 1] = CRGB(0,0,255);
- leds[x + 2] = CRGB(0,0,255);
- FastLED.show();
- }
- if(y-x <2){
- if(enemies.pos[y] == 0) {
- leds[y] = CRGB(0,0,0);
- isAlive = false;
- //Serial.println("HIT HT HIT");
- FastLED.show();
- }
- }
- leds[x - 2] = CRGB(0,0,255);
- leds[x - 1] = CRGB(0,0,255);
- leds[x + 1] = CRGB(0,0,255);
- leds[x + 2] = CRGB(0,0,255);
- leds[x] = CRGB(0,255,0);
- FastLED.show();
- leds[x - 2] = CRGB(0,0,0);
- leds[x - 1] = CRGB(0,0,0);
- leds[x + 1] = CRGB(0,0,0);
- leds[x + 2] = CRGB(0,0,0);
- FastLED.show();
- }
- void setup() {
- FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
- pinMode(button_move_R, INPUT);
- pinMode(button_move_L, INPUT);
- pinMode(button_attack, INPUT);
- Serial.begin(9600);
- mySer.begin(9600);
- }
- //int x = 80;
- int c = 0;
- void loop() {
- // while(mySer.available())
- // {
- // Serial.print(mySer.read());
- // }
- if(mySer.read() == 1){
- //Serial.println("move right");
- if(i >= 79){
- for(int i = 0; i < 80; i++) {
- leds[i] = CRGB(0, 170, 255);
- FastLED.show();
- //i++;
- }
- while(mySer.read() == 1 && mySer.read() == 2 && mySer.read() == 3);
- for(int i = 0; i < 80; i++) {
- leds[i] = CRGB(0, 0, 0);
- FastLED.show();
- //i++;
- }
- i = 0;
- //x = 79;
- }
- if(leds[i] == CRGB(255,0,0)) {
- for(int i = 0; i < 80; i++) {
- leds[i] = CRGB(255, 0, 0);
- FastLED.show();
- //i++;
- }
- for(int i = 0; i < 80; i++){
- for(int i = 0; i < 80; i++) {
- leds[i] = CRGB(0, 0, 0);
- FastLED.show();
- //i++;
- }
- }
- i = 0;
- //x = 79;
- }
- playerPos[i] = 1;
- playerPos[i-1] = 0;
- if(playerPos[i] == 1 && playerPos[i - 1] == 0) {
- leds[i-1] = CRGB(0, 0, 0);
- leds[i] = CRGB(0, 255, 0);
- }
- i++;
- if(i > lastmovement) {
- direct = 1;
- }
- lastmovement=i;
- FastLED.show();
- delay(50);
- }
- else if (mySer.read() == 2){
- // Serial.println("move left");
- if(i == 0){
- i = 1;
- }
- playerPos[i]=0;
- playerPos[i-1]=1;
- if(playerPos[i] == 0 && playerPos[i - 1] == 1) {
- leds[i] = CRGB(0, 0, 0);
- leds[i-1] = CRGB(0, 255, 0);
- }
- i--;
- if(i < lastmovement) {
- direct = 0;
- }
- lastmovement=i;
- FastLED.show();
- delay(50);
- }
- if(Interval(millis(), 200, &previousTime))
- {
- if(isAlive == false){
- enemies.pos[c] = -1;
- enemies.alive[c] = true;
- }
- enemies.pos[c]--;
- if (x == -1) {
- enemies.pos[c] = 79;
- }
- /*second = x + 2;
- third = x + 3;
- forth = x + 4;
- fifth = x + 5;*/
- //if(enemyPos[x] != playerPos[i])
- //Serial.println("1: ");
- //Serial.println(x);
- enemies.pos[c] = 1;
- enemies[c + 1].pos = 0;
- leds[c] = CRGB(255,0,0);
- leds[c + 1] = CRGB(0,0,0);
- // Serial.println("2: ");
- // Serial.println(x);
- FastLED.show();
- // Serial.println("3: ");
- // Serial.println(x);
- }
- if(mySer.read() == 3){
- if(direct == 1) {
- attack(i-1, x-1);
- Serial.println("right i-1");
- Serial.println(i-1);
- Serial.println("right x-1");
- Serial.println(x-1);
- }
- if(direct == 0) {
- attack(i, x);
- Serial.println("left i");
- Serial.println(i);
- Serial.println("left x");
- Serial.println(x);
- }
- }
- }
- bool Interval(unsigned long currTime, int interval, long *previousTime)
- {
- if(currTime - *previousTime >= interval)
- {
- *previousTime = currTime;
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement