Advertisement
Jordan1711

1v1

Mar 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #include <FastLED.h>
  2. #define LED_PIN 8 // Data pin for led tape
  3. #define NUM_LEDS 80
  4. int button_move_R = 7;
  5. int button_move_L = 9;
  6. int button_attack = 5;
  7. int i = 0;
  8. int lastmovement;// 0 left, 1 right
  9. int direct;
  10. long previousTime = 0;
  11. CRGB leds[NUM_LEDS];
  12. int enemyPos[80]; // has the enemy poition
  13. int playerPos[80];
  14. bool isAlive=true;
  15.  
  16. int enemy[10];
  17. int attack(int x, int y) {
  18.  
  19. if (x+2 == y){
  20.  
  21.  
  22. enemyPos[y] = 0;
  23. leds[x - 2] = CRGB(0,0,255);
  24. leds[x - 1] = CRGB(0,0,255);
  25. leds[x + 1] = CRGB(0,0,255);
  26. leds[x + 2] = CRGB(0,0,255);
  27. FastLED.show();
  28.  
  29. }
  30. if(y-x <2){
  31. if(enemyPos[y] == 0) {
  32. leds[y] = CRGB(0,0,0);
  33. isAlive = false;
  34. //Serial.println("HIT HT HIT");
  35. FastLED.show();
  36. }
  37. }
  38. leds[x - 2] = CRGB(0,0,255);
  39. leds[x - 1] = CRGB(0,0,255);
  40. leds[x + 1] = CRGB(0,0,255);
  41. leds[x + 2] = CRGB(0,0,255);
  42. leds[x] = CRGB(0,255,0);
  43. FastLED.show();
  44. leds[x - 2] = CRGB(0,0,0);
  45. leds[x - 1] = CRGB(0,0,0);
  46. leds[x + 1] = CRGB(0,0,0);
  47. leds[x + 2] = CRGB(0,0,0);
  48. FastLED.show();
  49.  
  50. }
  51.  
  52. void setup() {
  53. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  54. pinMode(button_move_R, INPUT);
  55. pinMode(button_move_L, INPUT);
  56. pinMode(button_attack, INPUT);
  57. Serial.begin(9600);
  58. Serial.begin(9600);
  59. }
  60.  
  61. int second, third, forth, fifth;
  62. int x = 80;
  63.  
  64. void loop() {
  65. if(digitalRead(button_move_R)==HIGH){
  66. //Serial.println("move right");
  67. if(i >= 79){
  68. for(int i = 0; i < 80; i++) {
  69. leds[i] = CRGB(0, 170, 255);
  70. FastLED.show();
  71. //i++;
  72. }
  73. while(digitalRead(button_move_R) == LOW&&digitalRead(button_move_L)==LOW&&digitalRead(button_attack)==LOW);
  74. for(int i = 0; i < 80; i++) {
  75. leds[i] = CRGB(0, 0, 0);
  76. FastLED.show();
  77. //i++;
  78. }
  79. i = 0;
  80. x = 79;
  81. }
  82. if(leds[i] == CRGB(255,0,0)) {
  83. for(int i = 0; i < 80; i++) {
  84. leds[i] = CRGB(255, 0, 0);
  85. FastLED.show();
  86. //i++;
  87. }
  88. while(digitalRead(button_move_R) == LOW&&digitalRead(button_move_L)==LOW&&digitalRead(button_attack)==LOW);
  89. for(int i = 0; i < 80; i++) {
  90. leds[i] = CRGB(0, 0, 0);
  91. FastLED.show();
  92. //i++;
  93. }
  94. i = 0;
  95. x = 79;
  96. }
  97. playerPos[i] = 1;
  98. playerPos[i-1] = 0;
  99. if(playerPos[i] == 1 && playerPos[i - 1] == 0) {
  100. leds[i-1] = CRGB(0, 0, 0);
  101. leds[i] = CRGB(0, 255, 0);
  102. }
  103. i++;
  104. if(i > lastmovement) {
  105. direct = 1;
  106. }
  107. lastmovement=i;
  108. FastLED.show();
  109. delay(50);
  110. }
  111.  
  112. else if (digitalRead(button_move_L)==HIGH){
  113. // Serial.println("move left");
  114. if(i == 0){
  115. i = 1;
  116. }
  117. playerPos[i]=0;
  118. playerPos[i-1]=1;
  119. if(playerPos[i] == 0 && playerPos[i - 1] == 1) {
  120. leds[i] = CRGB(0, 0, 0);
  121. leds[i-1] = CRGB(0, 255, 0);
  122. }
  123. i--;
  124. if(i < lastmovement) {
  125. direct = 0;
  126. }
  127. lastmovement=i;
  128. FastLED.show();
  129. delay(50);
  130. }
  131.  
  132. if(Interval(millis(), 200, &previousTime))
  133. {
  134. int j=0;
  135. for(i=0;i<10;i++){
  136. if(isAlive == false){
  137. x = 80;
  138. }
  139.  
  140. //if(enemyPos[x] != playerPos[i])
  141. //Serial.println("1: ");
  142. //Serial.println(x);
  143. enemyPos[i]=1;
  144. enemyPos[i + 1] = 0;
  145. leds[enemyPos[j]] = CRGB(255,0,0);
  146. leds[enemyPos[j + 1]] = CRGB(0,0,0);
  147. enemy[j]++;
  148. }
  149. /*enemyPos[second] = 1;
  150. enemyPos[second + 1] = 0;
  151. leds[second] = CRGB(255,0,0);
  152. leds[second + 1] = CRGB(0,0,0);
  153. enemyPos[third] = 1;
  154. enemyPos[third + 1] = 0;
  155. leds[third] = CRGB(255,0,0);
  156. leds[third + 1] = CRGB(0,0,0);
  157. enemyPos[forth] = 1;
  158. enemyPos[forth + 1] = 0;
  159. leds[forth] = CRGB(255,0,0);
  160. leds[forth + 1] = CRGB(0,0,0);
  161. enemyPos[fifth] = 1;
  162. enemyPos[fifth + 1] = 0;
  163. leds[fifth] = CRGB(255,0,0);
  164. leds[fifth + 1] = CRGB(0,0,0);*/
  165. // Serial.println("2: ");
  166. // Serial.println(x);
  167. FastLED.show();
  168. // Serial.println("3: ");
  169. // Serial.println(x);
  170. }
  171.  
  172. if(digitalRead(button_attack)==HIGH){
  173. if(direct == 1) {
  174. attack(i-1, x-1);
  175. Serial.println("right i-1");
  176. Serial.println(i-1);
  177. Serial.println("right x-1");
  178. Serial.println(x-1);
  179.  
  180. }
  181.  
  182. if(direct == 0) {
  183. attack(i, x);
  184. Serial.println("left i");
  185. Serial.println(i);
  186. Serial.println("left x");
  187. Serial.println(x);
  188. }
  189. }
  190. }
  191.  
  192. bool Interval(unsigned long currTime, int interval, long *previousTime)
  193. {
  194. if(currTime - *previousTime >= interval)
  195. {
  196. *previousTime = currTime;
  197. return true;
  198. }
  199. return false;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement