Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Alex Rodriguez Navas, David Marin Gutierrez, Carlos Sanson Martinez
- /* Main.c file generated by New Project wizard
- *
- * Created: weed dec 12 2019
- * Processor: PIC18F45K22
- * Compiler: MPLAB XC8
- */
- // Librerias
- #include <xc.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "config.h"
- #include "GLCD.h"
- // Config PIC
- #define _XTAL_FREQ 8000000
- #define A1 PORTAbits.RA1
- #define A2 PORTAbits.RA2
- #define A3 PORTAbits.RA3
- #define A4 PORTAbits.RA4
- // Sprites
- /*
- ESTRELLA: e
- CUADRADO: c
- TROFEO: t
- PLAYER ABAJO: s
- PLAYER ARRIBA: w
- PLAYER IZQUIERDA: a
- PLAYER DERECHA: d
- BARRA IZQUIERDA LLENA: g
- BARRA INTERIOR VACIA: h
- BARRA INTERIOR LLENA: i
- BARRA DERECHA VACIA: j
- BARRA DERECHA LLENA: k
- */
- // Estructuras
- struct pos {
- int x;
- int y;
- int active;
- };
- // Config Juego
- #define ROWS 7 // La primera linea es para el HUD
- #define COLS 25
- #define SUPERMODE_TRIGGER 83 // 'S' ascii
- #define GAME_TIME 180 // 180s --> 3min
- #define LEVEL1_STAR_MOV 4 // 2s
- #define LEVEL2_STAR_MOV 3 // 1.5s
- #define LEVEL3_STAR_MOV 2 // 1s
- #define LEVEL4_STAR_MOV 1 // 0.5s
- #define LEVEL1_STAR_CRE 40 // 20s
- #define LEVEL2_STAR_CRE 30 // 15s
- #define LEVEL3_STAR_CRE 20 // 10s
- #define LEVEL4_STAR_CRE 10 // 5s
- // Variables Juego
- int mapa[ROWS][COLS];
- // -1 <-> nada
- // num <-> estrella
- int dificultad;
- int puntuacion;
- int modo_super;
- int TimerCounter = 1;
- char direccion;
- struct pos jugador;
- struct pos estrellas[99];
- char* atoii(int num) {
- char buffer[4];
- sprintf(buffer, "%02d", num);
- return buffer;
- }
- void imprimir_texto(byte page, byte y, char* s) {
- for (int i=0; *s!='\n' && *s!='\0'; ++i) putchGLCD(page, y+i, *(s++));
- }
- void acabar_juego() {
- }
- char* renderizar_stopwatch() {
- char buffer[16];
- int mins = (GAME_TIME-(TimerCounter/2))/60;
- int segs = (GAME_TIME-(TimerCounter/2))%60;
- sprintf(buffer, "%02d:%02d", mins, segs);
- return buffer;
- }
- void renderizar_hud() {
- // Barra de dificultad
- if (dificultad == 1) imprimir_texto(0, 0, "ghhj");
- else if (dificultad == 2) imprimir_texto(0, 0, "gihj");
- else if (dificultad == 3) imprimir_texto(0, 0, "giij");
- else if (dificultad == 4) imprimir_texto(0, 0, "giik");
- // Indicador modo super
- if (modo_super) imprimir_texto(0, 4, "!");
- // Stopwatch
- imprimir_texto(0, 10, renderizar_stopwatch());
- // Puntuacion
- imprimir_texto(0, 22, "t");
- imprimir_texto(0, 23, atoii(puntuacion));
- // Despues de renderizar comprobamos si el juego ha acabado
- if((TimerCounter/2) == GAME_TIME) acabar_juego();
- }
- void renderizar_mapa() {
- for (int x = 1; x < ROWS; ++x) {
- for (int y = 0; y < COLS; ++y) {
- if (x == jugador.x && y == jugador.y) putchGLCD(jugador.x, jugador.y, direccion);
- else if (mapa[x][y] == -1) putchGLCD(x, y, '.');
- else if (modo_super) putchGLCD(x, y, 'c');
- else putchGLCD(x, y, 'e');
- }
- }
- }
- int pos_ok(struct pos p) {
- if (p.x > 0 && p.x < ROWS && p.y >= 0 && p.y < COLS) return 1;
- return 0;
- }
- int anadir_estrella(int x, int y) {
- struct pos newPos;
- newPos.x = x;
- newPos.y = y;
- newPos.active = 1;
- if (!pos_ok(newPos)) return 0;
- if (newPos.x == jugador.x && newPos.y == jugador.y) return 0;
- for (int i = 0; i < 99; ++i) {
- if (estrellas[i].active == 1 && estrellas[i].x == newPos.x && estrellas[i].y == newPos.y) return 0;
- }
- for (int i = 0; i < 99; ++i) {
- if (estrellas[i].active == 0) {
- estrellas[i] = newPos;
- mapa[newPos.x][newPos.y] = i;
- return 1;
- }
- }
- return 0;
- }
- void crear_estrella_random() {
- int rx, ry;
- do {
- rx = rand() % ROWS - 1;
- rx++;
- ry = rand() % COLS;
- } while (!anadir_estrella(rx,ry));
- }
- void mover_estrellas_random() {
- }
- void mover_jugador(struct pos newPos, char dir) {
- mapa[jugador.x][jugador.y] = -1;
- jugador = newPos;
- if (mapa[jugador.x][jugador.y] != -1) {
- estrellas[mapa[jugador.x][jugador.y]].active = 0;
- ++puntuacion;
- if (modo_super) ++puntuacion;
- crear_estrella_random();
- }
- direccion = dir;
- }
- void control_input(char dir) {
- struct pos newPos = jugador;
- if (dir == 'w') {
- newPos.x--;
- if (pos_ok(newPos)) mover_jugador(newPos,'w');
- }
- else if (dir == 'a') {
- newPos.y--;
- if (pos_ok(newPos)) mover_jugador(newPos,'a');
- }
- else if (dir == 's') {
- newPos.x++;
- if (pos_ok(newPos)) mover_jugador(newPos,'s');
- }
- else if (dir == 'd') {
- newPos.y++;
- if (pos_ok(newPos)) mover_jugador(newPos,'d');
- }
- }
- void setup(void) {
- // Configuracion
- // Pins Botones
- TRISA = 0xFF; // Todos los pins del Puerto A a Input
- ANSELA = 0x01; // Todos los pins del Puerto A a Digital menos A0 que esta en Analogico
- // Trimmer
- ADCON0 = 0x01; // AN0 / ENABLE ADC
- ADCON1 = 0x80; // REFERENCE (VSS, VD)
- ADCON2 = 0x11;
- // Linea Serie
- TXSTA1bits.BRGH = 1; // 115200 bauds
- BAUDCON1bits.BRG16 = 1;
- SPBRG1 = 16;
- TXSTA1bits.SYNC = 0; // Comunicacion Asincrona
- IPEN = 0; // Habilitar interrupts
- PEIE = 1;
- GIE = 1;
- RC1IF = 0; // Limpiar interrupt flag serie receiver
- RC1IE = 1; // Habilitar interrupt serie receiver
- ANSELC = 0x00; // PORTC a digital
- TRISCbits.RC7 = 1; // RC7 a input
- RCSTA1bits.RX9 = 0; // Comunicacion de 8 bits
- RCSTA1bits.CREN = 1; // Habilitar serie receiver
- RCSTA1bits.SPEN = 1; // Habilitar serial line
- // Timer 0
- T0CS = 0; // Internal clock
- T08BIT = 0; // 16 bit counter
- PSA = 0; // Prescaler
- T0PS2 = 1; // 64 prescaler
- T0PS1 = 0;
- T0PS0 = 1;
- TMR0H = 0xC2; // 0.5 seg
- TMR0L = 0xF6;
- // Inicializacion
- // Variables
- puntuacion = 0;
- dificultad = 1;
- modo_super = 0;
- // Inicializar Mapa
- for (int x=0; x<ROWS; ++x) for (int y=0; y<COLS; ++y) mapa[x][y] = -1;
- // GLCD
- GLCDinit(); // Inicizalizar GLCD
- clearGLCD(0,7,0,127); // Borrar toda la pantalla
- setStartLine(0); // Definir cursor inicial
- // Inicializar random seed
- ADCON0bits.GO = 1; // Petició de conversió
- while(ADCON0bits.GO) {} // Espera activa
- srand((ADRESH << 2) + (ADRESL >> 6));
- // Poner jugador en posicion random
- int rx = rand() % (ROWS - 1);
- rx++;
- int ry = rand() % COLS;
- direccion = 'w';
- jugador.x = rx;
- jugador.y = ry;
- jugador.active = 1;
- // Crear 3 estrellas random
- for (int i = 0; i < 3; ++i) crear_estrella_random();
- // Empezar Timer0
- TMR0IE = 1;
- TMR0IF = 0;
- TMR0ON = 1;
- }
- void interrupt rutina_AP(void) {
- if (RC1IF && RC1IE) { // Serie receiver
- // Leer lo recibido
- if (RCREG1 == SUPERMODE_TRIGGER) modo_super = 1;
- else modo_super = 0;
- RC1IF = 0; // Limpiar receiver interrupt flag
- }
- else if (TMR0IF && TMR0IE) { // Timer 0
- TimerCounter++;
- if (dificultad == 1) {
- if(TimerCounter == LEVEL1_STAR_MOV) mover_estrellas_random();
- if(TimerCounter == LEVEL1_STAR_CRE) crear_estrella_random();
- } else if (dificultad == 2) {
- if(TimerCounter == LEVEL2_STAR_MOV) mover_estrellas_random();
- if(TimerCounter == LEVEL2_STAR_CRE) crear_estrella_random();
- } else if (dificultad == 3) {
- if(TimerCounter == LEVEL3_STAR_MOV) mover_estrellas_random();
- if(TimerCounter == LEVEL3_STAR_CRE) crear_estrella_random();
- } else if (dificultad == 4) {
- if(TimerCounter == LEVEL4_STAR_MOV) mover_estrellas_random();
- if(TimerCounter == LEVEL4_STAR_CRE) crear_estrella_random();
- }
- TMR0H = 0xC2; // 0.5 seg
- TMR0L = 0xF6;
- TMR0IF = 0; // Limpar timer 0 interrupt flag
- }
- }
- // Tratamiento anti rebotes
- int A1P = 0;
- int A2P = 0;
- int A3P = 0;
- int A4P = 0;
- void loop(void) {
- ADCON0bits.GO = 1; // Petició de conversió
- while(ADCON0bits.GO) { // Espera activa
- // Renderizar graficos
- renderizar_hud();
- renderizar_mapa();
- // Controlar inputs
- if(A1 && !A1P) { // Boton 1
- __delay_ms(10);
- if(A1) {
- control_input('a');
- }
- }
- else if(A2 && !A2P) { // Boton 2
- __delay_ms(10);
- if(A2) {
- control_input('w');
- }
- }
- else if(A3 && !A3P) { // Boton 3
- __delay_ms(10);
- if(A3) {
- control_input('s');
- }
- }
- else if(A4 && !A4P) { // Boton 4
- __delay_ms(10);
- if(A4) {
- control_input('d');
- }
- }
- A1P = A1;
- A2P = A2;
- A3P = A3;
- A4P = A4;
- }
- int valor = (ADRESH << 2) + (ADRESL >> 6);
- if (valor < 256) dificultad = 1;
- else if (valor < 512) dificultad = 2;
- else if (valor < 768) dificultad = 3;
- else dificultad = 4;
- __delay_ms(5);
- }
- void main(void) {
- setup();
- while(1) loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement