Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <FastLED.h>
  2. #define LED_PIN 8
  3. #define NUM_LEDS 80
  4. int button_move_R = 7;
  5. #define BRIGHTNESS 150
  6. int button_move_L = 9;
  7. int button_attack = 5;
  8. int i = 0;
  9. int j=0;
  10. int l=0;
  11. int lastmovement;// -1 left, 1 right
  12. int direct;
  13. CRGB leds[NUM_LEDS];
  14. int Enemies=0;
  15. int startTime;
  16. int SpawnLocation;
  17. int mobLocations;
  18. CRGB mob[NUM_LEDS];
  19. void setup() {
  20. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  21. pinMode(button_move_R, INPUT);
  22. pinMode(button_move_L, INPUT);
  23. pinMode(button_attack, INPUT);
  24. Serial.begin(9600);
  25. randomSeed(analogRead(0));
  26. }
  27. int attack(int x){
  28. Serial.print("ATTACK");
  29. leds[x+1]=CRGB(255,255,0);
  30. leds[x-1]=CRGB(255,255,0);
  31. FastLED.show();
  32. if(leds[x+1]==CRGB(255,0,0)||leds[x-1]==CRGB(255,0,0)){//
  33. leds[x+1]=CRGB(0,0,0); //
  34. leds[x-1]=CRGB(0,0,0); // CHECKS FOR AN ENEMY AND
  35. leds[x-1]=CRGB(255,255,0); // REMOVES IT.
  36. leds[x+1]=CRGB(255,255,0);
  37. FastLED.show();
  38. }
  39. delay(300);
  40. leds[x+1]=CRGB(0,0,0);
  41. leds[x-1]=CRGB(0,0,0);
  42. FastLED.show();
  43. return x;
  44. }
  45. int endGame(int red,int green, int blue){
  46. for(int i = 0; i < 80; i++) {
  47. leds[i] = CRGB(red, green, blue);
  48. FastLED.show();
  49. //i++;
  50. }
  51. while(digitalRead(button_move_R) == LOW&&digitalRead(button_move_L)==LOW&&digitalRead(button_attack)==LOW);
  52. for(int i = 0; i < 80; i++) {
  53. leds[i] = CRGB(0, 0, 0);
  54. FastLED.show();
  55. //i++;
  56. }
  57. }
  58. int spawnMob(int x){
  59. if(Enemies<6&&millis()>Enemies*4000){
  60. Serial.print("Spawn!");
  61. SpawnLocation=random(x+15,80);
  62. Serial.print(SpawnLocation);
  63. Enemies++;
  64. if(SpawnLocation==mobLocations){
  65. SpawnLocation=random(x+15,80);
  66. }
  67. leds[SpawnLocation]=CRGB(255,0,0);
  68. FastLED.show();
  69. mobLocations=SpawnLocation;
  70. }
  71.  
  72. }
  73. /*void spawnLava(){
  74. while(i<3){
  75. for(j=20+l;j<31+l;j++){
  76. leds[j] = CRGB(255,255,255);
  77. FastLED.show();
  78. }
  79. l = l+20;
  80. i++;
  81. }
  82. }
  83. */
  84. void loop() {
  85. Serial.println(startTime);
  86. // startTime = millis();
  87. // spawnLava();
  88. while(i<80) {
  89. while(digitalRead(button_move_R)== LOW && digitalRead(button_move_L) == LOW&&digitalRead(button_attack)==LOW){
  90. spawnMob(i-1);
  91. }
  92. spawnMob(i-1);
  93. if(digitalRead(button_move_R)==HIGH){
  94. if(leds[i]==CRGB(255,0,0)){
  95. endGame(255,0,0);
  96. i=0;
  97. lastmovement=1;
  98. continue;
  99. }
  100. leds[i-1] = CRGB(0, 0, 0);
  101. leds[i] = CRGB(0, 255, 0);
  102. FastLED.show();
  103. i++;
  104. if(i > lastmovement) {
  105. direct = 1;
  106. }
  107. lastmovement=i;
  108. delay(100);
  109. }
  110. else if (digitalRead(button_move_L)==HIGH){
  111. if(i == 0){
  112. i = 1;
  113. }
  114. if(leds[i]==CRGB(0,255,0)){
  115. leds[i] = CRGB(0, 0, 0);
  116. }
  117. leds[i-1] = CRGB(0, 255, 0);
  118. i--;
  119. if(i < lastmovement) {
  120. direct = 0;
  121. }
  122. lastmovement=i;
  123. FastLED.show();
  124. delay(100);
  125. }
  126. if(digitalRead(button_attack)==HIGH){
  127. Serial.println("attack is pressed");
  128. if(direct == 1) {
  129. attack(i-1);
  130. }
  131. if(direct == 0) {
  132. attack(i);
  133. }
  134. }
  135. }
  136. i=0;
  137. endGame(0,255,255);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement