Guest User

Untitled

a guest
Oct 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. #include "IRremote.h"
  2. int receiver = 7;
  3. IRrecv irrecv(receiver);
  4. decode_results results;
  5.  
  6. #define DS_pin 4
  7. #define STCP_pin 5
  8. #define SHCP_pin 6
  9.  
  10. #define ship 0
  11. #define enemy 1
  12. #define killed 2
  13.  
  14. int posX=0;
  15. int posY=0;
  16. int posZ=0;
  17.  
  18. int distanciaX;
  19. int distanciaY;
  20. int distanciaZ;
  21.  
  22. class Enemigo{
  23. public:
  24. Enemigo(int x, int y, int z);
  25. int getZ();
  26. int getX();
  27. int getY();
  28. void setZ(int z);
  29. void setY(int y);
  30. void setX(int x);
  31. void perseguirJugador();
  32. private:
  33. int _x;
  34. int _y;
  35. int _z;
  36. };
  37.  
  38. Enemigo::Enemigo(int x, int y, int z){
  39. _x=x;
  40. _y=y;
  41. _z=z;
  42. }
  43.  
  44. int Enemigo::getZ(){
  45. return _z;
  46. }
  47.  
  48. int Enemigo::getY(){
  49. return _y;
  50. }
  51.  
  52. int Enemigo::getX(){
  53. return _x;
  54. }
  55.  
  56. void Enemigo::setZ(int z){
  57. _z = z;
  58. }
  59.  
  60. void Enemigo::setY(int y){
  61. _y = y;
  62. }
  63.  
  64. void Enemigo::setX(int x){
  65. _x = x;
  66. }
  67.  
  68. void Enemigo::perseguirJugador(){
  69. distanciaX=posX - _x;
  70. distanciaY=posY - _y;
  71. distanciaZ=posZ - _z;
  72.  
  73. if(_y+1==posY){
  74. _y++;
  75. distanciaY=0;
  76. }else if(_y-1 ==posY){
  77. _y--;
  78. distanciaY=0;
  79. }
  80. _y = constrain(_y, 0,3);
  81. if(distanciaX>0){
  82. _x++;
  83. }else if(distanciaX<0){
  84. _x--;
  85. }
  86. _x = constrain(_x, 0,3);
  87. if(distanciaY>0){
  88. _y++;
  89. _y = constrain(_y, 0,3);
  90. return;
  91. }else if(distanciaY<0){
  92. _y--;
  93. _y = constrain(_y, 0,3);
  94. return;
  95. }
  96. if(distanciaZ>0){
  97. _z++;
  98. }else if(distanciaZ<0){
  99. _z--;
  100. }
  101. _z = constrain(_z, 0,3);
  102. return;
  103. }
  104.  
  105.  
  106. int barrido=0;
  107.  
  108. int contMov=0;
  109. int respuesta=0;
  110. int Arreglo[16];
  111.  
  112. int contador=0;
  113. int SnakeHead=0;
  114. int randomCounter=0;
  115.  
  116. int posiciones[6]={0,0,0,0,0,0};
  117. int decorado[6]={0,0,0,0,0,0};
  118.  
  119. int Matrix[3][4][4][4]={
  120. {
  121. {
  122. {0,0,0,0},
  123. {0,0,0,0},
  124. {0,0,0,0},
  125. {0,0,0,0}
  126. },
  127. {
  128. {0,0,0,0},
  129. {0,0,0,0},
  130. {0,0,0,0},
  131. {0,0,0,0}
  132. },
  133. {
  134. {0,0,0,0},
  135. {0,0,0,0},
  136. {0,0,0,0},
  137. {0,0,0,0}
  138. },
  139. {
  140. {0,0,0,0},
  141. {0,0,0,0},
  142. {0,0,0,0},
  143. {0,0,0,0}
  144. }
  145. },
  146. {
  147. {
  148. {0,0,0,0},
  149. {0,0,0,0},
  150. {0,0,0,0},
  151. {0,0,0,0}
  152. },
  153. {
  154. {0,0,0,0},
  155. {0,0,0,0},
  156. {0,0,0,0},
  157. {0,0,0,0}
  158. },
  159. {
  160. {0,0,0,0},
  161. {0,0,0,0},
  162. {0,0,0,0},
  163. {0,0,0,0}
  164. },
  165. {
  166. {0,0,0,0},
  167. {0,0,0,0},
  168. {0,0,0,0},
  169. {0,0,0,0}
  170. }
  171. },
  172. {
  173. {
  174. {0,0,0,0},
  175. {0,0,0,0},
  176. {0,0,0,0},
  177. {0,0,0,0}
  178. },
  179. {
  180. {0,0,0,0},
  181. {0,0,0,0},
  182. {0,0,0,0},
  183. {0,0,0,0}
  184. },
  185. {
  186. {0,0,0,0},
  187. {0,0,0,0},
  188. {0,0,0,0},
  189. {0,0,0,0}
  190. },
  191. {
  192. {0,0,0,0},
  193. {0,0,0,0},
  194. {0,0,0,0},
  195. {0,0,0,0}
  196. }
  197. }
  198. };
  199.  
  200. Enemigo e1(3,3,3);
  201.  
  202. void setup()
  203. {
  204. for(int i=0; i<7;i++){
  205. pinMode(i, OUTPUT);
  206. }
  207. irrecv.enableIRIn();
  208. }
  209.  
  210. void ShiftRegister(){
  211. digitalWrite(STCP_pin, LOW);
  212. for(int i=15; i>=0; i--){
  213. digitalWrite(SHCP_pin, LOW);
  214. digitalWrite(DS_pin, Arreglo[i]);
  215. digitalWrite(SHCP_pin, HIGH);
  216. }
  217. digitalWrite(STCP_pin, HIGH);
  218. }
  219.  
  220. void translateIR(){
  221. switch(results.value){
  222. case 0xFF30CF:
  223. SnakeHead=1;
  224. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  225. case 0xFF18E7:
  226. SnakeHead=2;
  227. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  228. case 0xFF10EF:
  229. SnakeHead=3;
  230. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  231. case 0xFF5AA5:
  232. SnakeHead=4;
  233. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  234. case 0xFF42BD:
  235. SnakeHead=5;
  236. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  237. case 0xFF4AB5:
  238. SnakeHead=6;
  239. posiciones[SnakeHead-1]=!posiciones[SnakeHead-1]; break;
  240. }
  241.  
  242. contMov=0;
  243. for(int i=0; i<6; i++)
  244. if(i!=SnakeHead-1)
  245. posiciones[i]=0;
  246. }
  247.  
  248. void Hunt(int i){
  249. switch(i){
  250. case 1: posZ++; break;
  251. case 2: posY++; break;
  252. case 3: posX--; break;
  253. case 4: posX++; break;
  254. case 5: posZ--; break;
  255. case 6: posY--; break;
  256. }
  257. }
  258.  
  259. void Mover(){
  260. if(irrecv.decode(&results)){
  261. translateIR();
  262. irrecv.resume();
  263. }
  264.  
  265. if(posiciones[SnakeHead-1]==1)
  266. Hunt(SnakeHead);
  267. contMov=0;
  268.  
  269. posX = constrain(posX,0,3);
  270. posY = constrain(posY,0,3);
  271. posZ = constrain(posZ,0,3);
  272. }
  273.  
  274.  
  275. void Dead(){
  276.  
  277. Matrix[killed][posZ][posY][posX]=1;
  278.  
  279. decorado[0]=posX-1;
  280. decorado[1]=posY-1;
  281. decorado[2]=posZ-1;
  282. decorado[3]=posX+1;
  283. decorado[4]=posY+1;
  284. decorado[5]=posZ+1;
  285.  
  286. for(int i=0; i<6; i++)
  287. decorado[i]=constrain(decorado[i],0,3);
  288.  
  289. for(int k=decorado[2]; k<=decorado[5]; k++){
  290. for(int i=decorado[1]; i<=decorado[4]; i++){
  291. for(int j=decorado[0]; j<=decorado[3]; j++){
  292. Matrix[killed][k][i][j]=1;
  293. }
  294. }
  295. }
  296.  
  297. for(int scan=0; scan<5; scan++){
  298. barrido=0;
  299. do{
  300. barrido++;
  301. for(int Nivel=0; Nivel<4; Nivel++){
  302. for(int i=0; i<4; i++)
  303. for(int j=0; j<4; j++){
  304. Arreglo[contador]=Matrix[killed][Nivel][i][j];
  305. contador++;
  306. }
  307. ShiftRegister();
  308. digitalWrite(Nivel, LOW);
  309. limpiar(Nivel);
  310. }
  311. ShiftRegister();
  312. }while(barrido<300);
  313. }
  314.  
  315. for(int Nivel=0; Nivel<4; Nivel++)
  316. for(int i=0; i<4; i++)
  317. for(int j=0; j<4; j++)
  318. Matrix[killed][Nivel][i][j]=0;
  319. }
  320.  
  321. void reiniciar3D(){
  322. e1.setZ(3);
  323. e1.setY(3);
  324. e1.setX(3);
  325. posX=posY=posZ=0;
  326. for(int i=0; i<6; i++)
  327. posiciones[i]=0;
  328. limpiar3D();
  329. Matrix[ship][posZ][posY][posX]=1;
  330. Matrix[enemy][e1.getZ()][e1.getY()][e1.getX()]=1;
  331.  
  332. }
  333.  
  334. void limpiar(int Nivel){
  335. contador=0;
  336. for(int x=0; x<16;x++){
  337. Arreglo[x]=0;
  338. }
  339. ShiftRegister();
  340. digitalWrite(Nivel, HIGH);
  341. }
  342.  
  343. void limpiar3D(){
  344. for(int z=0; z<4; z++)
  345. for(int x=0; x<4; x++)
  346. for(int y=0; y<4; y++){
  347. Matrix[ship][z][y][x]=0;
  348. Matrix[enemy][z][y][x]=0;
  349. }
  350. }
  351.  
  352. void loop()
  353. {
  354. for(int scan=0; scan<200; scan++){
  355. for(int Nivel=0; Nivel<4; Nivel++){
  356. for(int i=0; i<4; i++)
  357. for(int j=0; j<4; j++){
  358.  
  359. if(Matrix[enemy][Nivel][i][j]==1 && Matrix[ship][Nivel][i][j]==1){
  360. Dead();
  361. reiniciar3D();
  362. }
  363.  
  364. if(Matrix[enemy][Nivel][i][j]==1){
  365. Arreglo[contador]=1;
  366. }
  367. else if(Matrix[ship][Nivel][i][j]==1){
  368. Arreglo[contador]=1;
  369. }else{
  370. Arreglo[contador]=0;
  371. }
  372.  
  373. contador++;
  374.  
  375. }
  376. ShiftRegister();
  377. digitalWrite(Nivel, LOW);
  378. limpiar(Nivel);
  379. }
  380. }
  381. Mover();
  382. e1.perseguirJugador();
  383. limpiar3D();
  384.  
  385. Matrix[ship][posZ][posY][posX]=1;
  386. Matrix[enemy][e1.getZ()][e1.getY()][e1.getX()]=1;
  387.  
  388. }
Add Comment
Please, Sign In to add comment