Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. // Alex Rodriguez Navas, David Marin Gutierrez, Carlos Sanson Martinez
  2.  
  3. /* Main.c file generated by New Project wizard
  4. *
  5. * Created: weed dec 12 2019
  6. * Processor: PIC18F45K22
  7. * Compiler: MPLAB XC8
  8. */
  9.  
  10. // Librerias
  11. #include <xc.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "config.h"
  16. #include "GLCD.h"
  17.  
  18. // Config PIC
  19. #define _XTAL_FREQ 8000000
  20. #define A1 PORTAbits.RA1
  21. #define A2 PORTAbits.RA2
  22. #define A3 PORTAbits.RA3
  23. #define A4 PORTAbits.RA4
  24.  
  25. // Sprites
  26. /*
  27. ESTRELLA: e
  28. CUADRADO: c
  29. TROFEO: t
  30. PLAYER ABAJO: s
  31. PLAYER ARRIBA: w
  32. PLAYER IZQUIERDA: a
  33. PLAYER DERECHA: d
  34. BARRA IZQUIERDA LLENA: g
  35. BARRA INTERIOR VACIA: h
  36. BARRA INTERIOR LLENA: i
  37. BARRA DERECHA VACIA: j
  38. BARRA DERECHA LLENA: k
  39. */
  40.  
  41. // Estructuras
  42. struct pos {
  43. int x;
  44. int y;
  45. int active;
  46. };
  47.  
  48. // Config Juego
  49. #define ROWS 7 // La primera linea es para el HUD
  50. #define COLS 25
  51. #define SUPERMODE_TRIGGER 83 // 'S' ascii
  52. #define GAME_TIME 180 // 180s --> 3min
  53. #define LEVEL1_STAR_MOV 4 // 2s
  54. #define LEVEL2_STAR_MOV 3 // 1.5s
  55. #define LEVEL3_STAR_MOV 2 // 1s
  56. #define LEVEL4_STAR_MOV 1 // 0.5s
  57. #define LEVEL1_STAR_CRE 40 // 20s
  58. #define LEVEL2_STAR_CRE 30 // 15s
  59. #define LEVEL3_STAR_CRE 20 // 10s
  60. #define LEVEL4_STAR_CRE 10 // 5s
  61.  
  62. // Variables Juego
  63. int mapa[ROWS][COLS];
  64. // -1 <-> nada
  65. // num <-> estrella
  66.  
  67. int dificultad;
  68. int puntuacion;
  69. int modo_super;
  70. int TimerCounter = 1;
  71.  
  72. char direccion;
  73. struct pos jugador;
  74. struct pos estrellas[99];
  75.  
  76. char* atoii(int num) {
  77. char buffer[4];
  78. sprintf(buffer, "%02d", num);
  79. return buffer;
  80. }
  81.  
  82. void imprimir_texto(byte page, byte y, char* s) {
  83. for (int i=0; *s!='\n' && *s!='\0'; ++i) putchGLCD(page, y+i, *(s++));
  84. }
  85.  
  86. void acabar_juego() {
  87. }
  88.  
  89. char* renderizar_stopwatch() {
  90. char buffer[16];
  91. int mins = (GAME_TIME-(TimerCounter/2))/60;
  92. int segs = (GAME_TIME-(TimerCounter/2))%60;
  93. sprintf(buffer, "%02d:%02d", mins, segs);
  94. return buffer;
  95. }
  96.  
  97. void renderizar_hud() {
  98. // Barra de dificultad
  99. if (dificultad == 1) imprimir_texto(0, 0, "ghhj");
  100. else if (dificultad == 2) imprimir_texto(0, 0, "gihj");
  101. else if (dificultad == 3) imprimir_texto(0, 0, "giij");
  102. else if (dificultad == 4) imprimir_texto(0, 0, "giik");
  103.  
  104. // Indicador modo super
  105. if (modo_super) imprimir_texto(0, 4, "!");
  106.  
  107. // Stopwatch
  108. imprimir_texto(0, 10, renderizar_stopwatch());
  109.  
  110. // Puntuacion
  111. imprimir_texto(0, 22, "t");
  112. imprimir_texto(0, 23, atoii(puntuacion));
  113.  
  114. // Despues de renderizar comprobamos si el juego ha acabado
  115. if((TimerCounter/2) == GAME_TIME) acabar_juego();
  116. }
  117.  
  118. void renderizar_mapa() {
  119. for (int x = 1; x < ROWS; ++x) {
  120. for (int y = 0; y < COLS; ++y) {
  121. if (x == jugador.x && y == jugador.y) putchGLCD(jugador.x, jugador.y, direccion);
  122. else if (mapa[x][y] == -1) putchGLCD(x, y, '.');
  123. else if (modo_super) putchGLCD(x, y, 'c');
  124. else putchGLCD(x, y, 'e');
  125. }
  126. }
  127.  
  128. }
  129.  
  130. int pos_ok(struct pos p) {
  131. if (p.x > 0 && p.x < ROWS && p.y >= 0 && p.y < COLS) return 1;
  132. return 0;
  133. }
  134.  
  135. int anadir_estrella(int x, int y) {
  136. struct pos newPos;
  137. newPos.x = x;
  138. newPos.y = y;
  139. newPos.active = 1;
  140. if (!pos_ok(newPos)) return 0;
  141. if (newPos.x == jugador.x && newPos.y == jugador.y) return 0;
  142. for (int i = 0; i < 99; ++i) {
  143. if (estrellas[i].active == 1 && estrellas[i].x == newPos.x && estrellas[i].y == newPos.y) return 0;
  144. }
  145. for (int i = 0; i < 99; ++i) {
  146. if (estrellas[i].active == 0) {
  147. estrellas[i] = newPos;
  148. mapa[newPos.x][newPos.y] = i;
  149. return 1;
  150. }
  151. }
  152. return 0;
  153. }
  154.  
  155. void crear_estrella_random() {
  156. int rx, ry;
  157. do {
  158. rx = rand() % ROWS - 1;
  159. rx++;
  160. ry = rand() % COLS;
  161. } while (!anadir_estrella(rx,ry));
  162. }
  163.  
  164. void mover_estrellas_random() {
  165. }
  166.  
  167. void mover_jugador(struct pos newPos, char dir) {
  168. mapa[jugador.x][jugador.y] = -1;
  169. jugador = newPos;
  170.  
  171. if (mapa[jugador.x][jugador.y] != -1) {
  172. estrellas[mapa[jugador.x][jugador.y]].active = 0;
  173. ++puntuacion;
  174. if (modo_super) ++puntuacion;
  175. crear_estrella_random();
  176. }
  177. direccion = dir;
  178. }
  179.  
  180. void control_input(char dir) {
  181. struct pos newPos = jugador;
  182. if (dir == 'w') {
  183. newPos.x--;
  184. if (pos_ok(newPos)) mover_jugador(newPos,'w');
  185. }
  186. else if (dir == 'a') {
  187. newPos.y--;
  188. if (pos_ok(newPos)) mover_jugador(newPos,'a');
  189. }
  190. else if (dir == 's') {
  191. newPos.x++;
  192. if (pos_ok(newPos)) mover_jugador(newPos,'s');
  193. }
  194. else if (dir == 'd') {
  195. newPos.y++;
  196. if (pos_ok(newPos)) mover_jugador(newPos,'d');
  197. }
  198. }
  199.  
  200. void setup(void) {
  201.  
  202. // Configuracion
  203.  
  204. // Pins Botones
  205. TRISA = 0xFF; // Todos los pins del Puerto A a Input
  206. ANSELA = 0x01; // Todos los pins del Puerto A a Digital menos A0 que esta en Analogico
  207.  
  208. // Trimmer
  209. ADCON0 = 0x01; // AN0 / ENABLE ADC
  210. ADCON1 = 0x80; // REFERENCE (VSS, VD)
  211. ADCON2 = 0x11;
  212.  
  213. // Linea Serie
  214. TXSTA1bits.BRGH = 1; // 115200 bauds
  215. BAUDCON1bits.BRG16 = 1;
  216. SPBRG1 = 16;
  217.  
  218. TXSTA1bits.SYNC = 0; // Comunicacion Asincrona
  219.  
  220. IPEN = 0; // Habilitar interrupts
  221. PEIE = 1;
  222. GIE = 1;
  223. RC1IF = 0; // Limpiar interrupt flag serie receiver
  224. RC1IE = 1; // Habilitar interrupt serie receiver
  225.  
  226. ANSELC = 0x00; // PORTC a digital
  227. TRISCbits.RC7 = 1; // RC7 a input
  228.  
  229. RCSTA1bits.RX9 = 0; // Comunicacion de 8 bits
  230. RCSTA1bits.CREN = 1; // Habilitar serie receiver
  231. RCSTA1bits.SPEN = 1; // Habilitar serial line
  232.  
  233. // Timer 0
  234. T0CS = 0; // Internal clock
  235. T08BIT = 0; // 16 bit counter
  236. PSA = 0; // Prescaler
  237. T0PS2 = 1; // 64 prescaler
  238. T0PS1 = 0;
  239. T0PS0 = 1;
  240. TMR0H = 0xC2; // 0.5 seg
  241. TMR0L = 0xF6;
  242.  
  243. // Inicializacion
  244.  
  245. // Variables
  246. puntuacion = 0;
  247. dificultad = 1;
  248. modo_super = 0;
  249.  
  250. // Inicializar Mapa
  251. for (int x=0; x<ROWS; ++x) for (int y=0; y<COLS; ++y) mapa[x][y] = -1;
  252.  
  253. // GLCD
  254. GLCDinit(); // Inicizalizar GLCD
  255. clearGLCD(0,7,0,127); // Borrar toda la pantalla
  256. setStartLine(0); // Definir cursor inicial
  257.  
  258. // Inicializar random seed
  259. ADCON0bits.GO = 1; // Petició de conversió
  260. while(ADCON0bits.GO) {} // Espera activa
  261. srand((ADRESH << 2) + (ADRESL >> 6));
  262.  
  263. // Poner jugador en posicion random
  264. int rx = rand() % (ROWS - 1);
  265. rx++;
  266. int ry = rand() % COLS;
  267.  
  268. direccion = 'w';
  269. jugador.x = rx;
  270. jugador.y = ry;
  271. jugador.active = 1;
  272.  
  273. // Crear 3 estrellas random
  274. for (int i = 0; i < 3; ++i) crear_estrella_random();
  275.  
  276. // Empezar Timer0
  277. TMR0IE = 1;
  278. TMR0IF = 0;
  279. TMR0ON = 1;
  280. }
  281.  
  282. void interrupt rutina_AP(void) {
  283. if (RC1IF && RC1IE) { // Serie receiver
  284. // Leer lo recibido
  285. if (RCREG1 == SUPERMODE_TRIGGER) modo_super = 1;
  286. else modo_super = 0;
  287. RC1IF = 0; // Limpiar receiver interrupt flag
  288. }
  289. else if (TMR0IF && TMR0IE) { // Timer 0
  290. TimerCounter++;
  291.  
  292. if (dificultad == 1) {
  293. if(TimerCounter == LEVEL1_STAR_MOV) mover_estrellas_random();
  294. if(TimerCounter == LEVEL1_STAR_CRE) crear_estrella_random();
  295. } else if (dificultad == 2) {
  296. if(TimerCounter == LEVEL2_STAR_MOV) mover_estrellas_random();
  297. if(TimerCounter == LEVEL2_STAR_CRE) crear_estrella_random();
  298. } else if (dificultad == 3) {
  299. if(TimerCounter == LEVEL3_STAR_MOV) mover_estrellas_random();
  300. if(TimerCounter == LEVEL3_STAR_CRE) crear_estrella_random();
  301. } else if (dificultad == 4) {
  302. if(TimerCounter == LEVEL4_STAR_MOV) mover_estrellas_random();
  303. if(TimerCounter == LEVEL4_STAR_CRE) crear_estrella_random();
  304. }
  305.  
  306. TMR0H = 0xC2; // 0.5 seg
  307. TMR0L = 0xF6;
  308. TMR0IF = 0; // Limpar timer 0 interrupt flag
  309. }
  310. }
  311.  
  312. // Tratamiento anti rebotes
  313.  
  314. int A1P = 0;
  315. int A2P = 0;
  316. int A3P = 0;
  317. int A4P = 0;
  318.  
  319. void loop(void) {
  320. ADCON0bits.GO = 1; // Petició de conversió
  321. while(ADCON0bits.GO) { // Espera activa
  322. // Renderizar graficos
  323. renderizar_hud();
  324. renderizar_mapa();
  325.  
  326. // Controlar inputs
  327. if(A1 && !A1P) { // Boton 1
  328. __delay_ms(10);
  329. if(A1) {
  330. control_input('a');
  331. }
  332. }
  333. else if(A2 && !A2P) { // Boton 2
  334. __delay_ms(10);
  335. if(A2) {
  336. control_input('w');
  337. }
  338. }
  339. else if(A3 && !A3P) { // Boton 3
  340. __delay_ms(10);
  341. if(A3) {
  342. control_input('s');
  343. }
  344. }
  345. else if(A4 && !A4P) { // Boton 4
  346. __delay_ms(10);
  347. if(A4) {
  348. control_input('d');
  349. }
  350. }
  351.  
  352. A1P = A1;
  353. A2P = A2;
  354. A3P = A3;
  355. A4P = A4;
  356. }
  357. int valor = (ADRESH << 2) + (ADRESL >> 6);
  358. if (valor < 256) dificultad = 1;
  359. else if (valor < 512) dificultad = 2;
  360. else if (valor < 768) dificultad = 3;
  361. else dificultad = 4;
  362. __delay_ms(5);
  363. }
  364.  
  365. void main(void) {
  366. setup();
  367. while(1) loop();
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement