Alyson Castiblanco
/
JUEGO_DEF
Codigo con interfaz gráfica. Falta juntar los dos programas. Ya accede a ambos juegos
main.cpp@0:aa4d235c4151, 2018-11-18 (annotated)
- Committer:
- acastiblancoc
- Date:
- Sun Nov 18 01:43:14 2018 +0000
- Revision:
- 0:aa4d235c4151
Codigo con interfaz gr?fica. Falta juntar los dos programas. Ya accede a ambos juegos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acastiblancoc | 0:aa4d235c4151 | 1 | #include "mbed.h" |
acastiblancoc | 0:aa4d235c4151 | 2 | #include "MPU6050.h" |
acastiblancoc | 0:aa4d235c4151 | 3 | #include "figs.h" |
acastiblancoc | 0:aa4d235c4151 | 4 | Ticker bajar; |
acastiblancoc | 0:aa4d235c4151 | 5 | Serial command(USBTX,USBRX); |
acastiblancoc | 0:aa4d235c4151 | 6 | AnalogIn joy(A0); |
acastiblancoc | 0:aa4d235c4151 | 7 | AnalogIn joy1(A1); |
acastiblancoc | 0:aa4d235c4151 | 8 | float vx=0; |
acastiblancoc | 0:aa4d235c4151 | 9 | float vy=0; |
acastiblancoc | 0:aa4d235c4151 | 10 | DigitalIn cambios(PB_8); |
acastiblancoc | 0:aa4d235c4151 | 11 | int cc=0, punto=1; |
acastiblancoc | 0:aa4d235c4151 | 12 | uint8_t movimiento=0; |
acastiblancoc | 0:aa4d235c4151 | 13 | float VEL= 500; |
acastiblancoc | 0:aa4d235c4151 | 14 | uint8_t matrix_act [11]={0,0,0,0,0,0,0,0,0,0,0}; |
acastiblancoc | 0:aa4d235c4151 | 15 | uint8_t matrix_temp [8]={0,0,0,0,0,0,0,0}; |
acastiblancoc | 0:aa4d235c4151 | 16 | uint8_t matrix_temp1[8]={0,0,0,0,0,0,0,0}; |
acastiblancoc | 0:aa4d235c4151 | 17 | uint8_t fila=1,cont=0,columna=1,rotacion=1,figura=0; |
acastiblancoc | 0:aa4d235c4151 | 18 | SPI deviceM(PB_15, PB_14, PB_13); |
acastiblancoc | 0:aa4d235c4151 | 19 | DigitalOut ssel (PB_12); |
acastiblancoc | 0:aa4d235c4151 | 20 | Serial a(USBTX,USBRX); |
acastiblancoc | 0:aa4d235c4151 | 21 | Ticker cambio; |
acastiblancoc | 0:aa4d235c4151 | 22 | MPU6050 Wire(PB_9,PB_8); |
acastiblancoc | 0:aa4d235c4151 | 23 | InterruptIn iz(PA_14); |
acastiblancoc | 0:aa4d235c4151 | 24 | InterruptIn de(PA_13); |
acastiblancoc | 0:aa4d235c4151 | 25 | #define uint8_t unsigned char |
acastiblancoc | 0:aa4d235c4151 | 26 | float q[3]; |
acastiblancoc | 0:aa4d235c4151 | 27 | int pos; |
acastiblancoc | 0:aa4d235c4151 | 28 | float vel=200; |
acastiblancoc | 0:aa4d235c4151 | 29 | int b; |
acastiblancoc | 0:aa4d235c4151 | 30 | uint8_t cam[3]={0b01100000,0b00011000,0b00000110}; |
acastiblancoc | 0:aa4d235c4151 | 31 | uint8_t Matr[9]={0,0,0,0,0,0,0,0,0}; |
acastiblancoc | 0:aa4d235c4151 | 32 | uint8_t entorno={0b10000001}; |
acastiblancoc | 0:aa4d235c4151 | 33 | uint8_t carro={0b00011000}; |
acastiblancoc | 0:aa4d235c4151 | 34 | uint8_t obs=0b01100000; |
acastiblancoc | 0:aa4d235c4151 | 35 | void derecha(); |
acastiblancoc | 0:aa4d235c4151 | 36 | void izquierda(); |
acastiblancoc | 0:aa4d235c4151 | 37 | void colision(); |
acastiblancoc | 0:aa4d235c4151 | 38 | void juego(); |
acastiblancoc | 0:aa4d235c4151 | 39 | void accelerometro(); |
acastiblancoc | 0:aa4d235c4151 | 40 | void cambiof(); |
acastiblancoc | 0:aa4d235c4151 | 41 | int con; |
acastiblancoc | 0:aa4d235c4151 | 42 | int az; |
acastiblancoc | 0:aa4d235c4151 | 43 | |
acastiblancoc | 0:aa4d235c4151 | 44 | void sendSPI(uint8_t d1, uint8_t d2) |
acastiblancoc | 0:aa4d235c4151 | 45 | { |
acastiblancoc | 0:aa4d235c4151 | 46 | deviceM.unlock(); |
acastiblancoc | 0:aa4d235c4151 | 47 | ssel=0; |
acastiblancoc | 0:aa4d235c4151 | 48 | deviceM.write(d1); |
acastiblancoc | 0:aa4d235c4151 | 49 | deviceM.write(d2); |
acastiblancoc | 0:aa4d235c4151 | 50 | ssel=1; |
acastiblancoc | 0:aa4d235c4151 | 51 | deviceM.lock(); |
acastiblancoc | 0:aa4d235c4151 | 52 | } |
acastiblancoc | 0:aa4d235c4151 | 53 | |
acastiblancoc | 0:aa4d235c4151 | 54 | void borrar() |
acastiblancoc | 0:aa4d235c4151 | 55 | { |
acastiblancoc | 0:aa4d235c4151 | 56 | for(int i;i<=8;i++) { |
acastiblancoc | 0:aa4d235c4151 | 57 | sendSPI(i,0); |
acastiblancoc | 0:aa4d235c4151 | 58 | } |
acastiblancoc | 0:aa4d235c4151 | 59 | } |
acastiblancoc | 0:aa4d235c4151 | 60 | |
acastiblancoc | 0:aa4d235c4151 | 61 | void inicializar(){ |
acastiblancoc | 0:aa4d235c4151 | 62 | |
acastiblancoc | 0:aa4d235c4151 | 63 | a.baud(9600); |
acastiblancoc | 0:aa4d235c4151 | 64 | sendSPI(0x0c,1); |
acastiblancoc | 0:aa4d235c4151 | 65 | sendSPI(0x09,0); |
acastiblancoc | 0:aa4d235c4151 | 66 | sendSPI(0x0A,0x01); |
acastiblancoc | 0:aa4d235c4151 | 67 | sendSPI(0x0F,1); |
acastiblancoc | 0:aa4d235c4151 | 68 | wait (0.5); |
acastiblancoc | 0:aa4d235c4151 | 69 | sendSPI(0x0F,0); |
acastiblancoc | 0:aa4d235c4151 | 70 | wait(0.5); |
acastiblancoc | 0:aa4d235c4151 | 71 | sendSPI(0xB,0x7); |
acastiblancoc | 0:aa4d235c4151 | 72 | borrar(); |
acastiblancoc | 0:aa4d235c4151 | 73 | vel=200; |
acastiblancoc | 0:aa4d235c4151 | 74 | carro=0b00011000; |
acastiblancoc | 0:aa4d235c4151 | 75 | } |
acastiblancoc | 0:aa4d235c4151 | 76 | |
acastiblancoc | 0:aa4d235c4151 | 77 | void imprimir() |
acastiblancoc | 0:aa4d235c4151 | 78 | { |
acastiblancoc | 0:aa4d235c4151 | 79 | |
acastiblancoc | 0:aa4d235c4151 | 80 | |
acastiblancoc | 0:aa4d235c4151 | 81 | int c_d; //carro, obstaculo y entorno |
acastiblancoc | 0:aa4d235c4151 | 82 | int c_e; //carro y entorno |
acastiblancoc | 0:aa4d235c4151 | 83 | int din; |
acastiblancoc | 0:aa4d235c4151 | 84 | for(int i=1;i<=7;i++){ |
acastiblancoc | 0:aa4d235c4151 | 85 | sendSPI(i,entorno); |
acastiblancoc | 0:aa4d235c4151 | 86 | } |
acastiblancoc | 0:aa4d235c4151 | 87 | for(int j=1;j<=10;j++){ |
acastiblancoc | 0:aa4d235c4151 | 88 | din=(obs>>b) | entorno ; |
acastiblancoc | 0:aa4d235c4151 | 89 | c_d= carro | din; |
acastiblancoc | 0:aa4d235c4151 | 90 | c_e=carro | entorno; |
acastiblancoc | 0:aa4d235c4151 | 91 | sendSPI(8,c_e); |
acastiblancoc | 0:aa4d235c4151 | 92 | if(j<=7){ |
acastiblancoc | 0:aa4d235c4151 | 93 | sendSPI(j,din); |
acastiblancoc | 0:aa4d235c4151 | 94 | } |
acastiblancoc | 0:aa4d235c4151 | 95 | |
acastiblancoc | 0:aa4d235c4151 | 96 | if(j>1 && j<=8) |
acastiblancoc | 0:aa4d235c4151 | 97 | sendSPI(j-1,din); |
acastiblancoc | 0:aa4d235c4151 | 98 | |
acastiblancoc | 0:aa4d235c4151 | 99 | if(j>2 && j<=9){ |
acastiblancoc | 0:aa4d235c4151 | 100 | sendSPI(j-2,entorno); |
acastiblancoc | 0:aa4d235c4151 | 101 | } |
acastiblancoc | 0:aa4d235c4151 | 102 | if(j>=8&&j<=9){ |
acastiblancoc | 0:aa4d235c4151 | 103 | sendSPI(8,c_d); |
acastiblancoc | 0:aa4d235c4151 | 104 | } |
acastiblancoc | 0:aa4d235c4151 | 105 | if(j>10){ |
acastiblancoc | 0:aa4d235c4151 | 106 | sendSPI(8,c_e); |
acastiblancoc | 0:aa4d235c4151 | 107 | } |
acastiblancoc | 0:aa4d235c4151 | 108 | |
acastiblancoc | 0:aa4d235c4151 | 109 | if( (obs>>b)==carro && j==8 ){ |
acastiblancoc | 0:aa4d235c4151 | 110 | colision(); |
acastiblancoc | 0:aa4d235c4151 | 111 | } |
acastiblancoc | 0:aa4d235c4151 | 112 | |
acastiblancoc | 0:aa4d235c4151 | 113 | wait_ms(vel); |
acastiblancoc | 0:aa4d235c4151 | 114 | con++; |
acastiblancoc | 0:aa4d235c4151 | 115 | if(con==10){ |
acastiblancoc | 0:aa4d235c4151 | 116 | vel=vel-10;} |
acastiblancoc | 0:aa4d235c4151 | 117 | } |
acastiblancoc | 0:aa4d235c4151 | 118 | |
acastiblancoc | 0:aa4d235c4151 | 119 | } |
acastiblancoc | 0:aa4d235c4151 | 120 | |
acastiblancoc | 0:aa4d235c4151 | 121 | void carro_juego () |
acastiblancoc | 0:aa4d235c4151 | 122 | { |
acastiblancoc | 0:aa4d235c4151 | 123 | cambio.attach(&cambiof,0.2); |
acastiblancoc | 0:aa4d235c4151 | 124 | while(1){ |
acastiblancoc | 0:aa4d235c4151 | 125 | accelerometro(); |
acastiblancoc | 0:aa4d235c4151 | 126 | //juego(); |
acastiblancoc | 0:aa4d235c4151 | 127 | } |
acastiblancoc | 0:aa4d235c4151 | 128 | } |
acastiblancoc | 0:aa4d235c4151 | 129 | |
acastiblancoc | 0:aa4d235c4151 | 130 | void juego(){ |
acastiblancoc | 0:aa4d235c4151 | 131 | inicializar(); |
acastiblancoc | 0:aa4d235c4151 | 132 | de.fall(&derecha); |
acastiblancoc | 0:aa4d235c4151 | 133 | iz.fall(&izquierda); |
acastiblancoc | 0:aa4d235c4151 | 134 | a.printf("inicio\n"); |
acastiblancoc | 0:aa4d235c4151 | 135 | while(1){ |
acastiblancoc | 0:aa4d235c4151 | 136 | con=0; |
acastiblancoc | 0:aa4d235c4151 | 137 | b=2*(rand()%3); |
acastiblancoc | 0:aa4d235c4151 | 138 | a.printf("numero %d\n",b); |
acastiblancoc | 0:aa4d235c4151 | 139 | imprimir(); |
acastiblancoc | 0:aa4d235c4151 | 140 | } |
acastiblancoc | 0:aa4d235c4151 | 141 | } |
acastiblancoc | 0:aa4d235c4151 | 142 | |
acastiblancoc | 0:aa4d235c4151 | 143 | void derecha() |
acastiblancoc | 0:aa4d235c4151 | 144 | { |
acastiblancoc | 0:aa4d235c4151 | 145 | if(carro==0b00011000 || carro==0b01100000) |
acastiblancoc | 0:aa4d235c4151 | 146 | carro=carro>>2; |
acastiblancoc | 0:aa4d235c4151 | 147 | a.printf("entra der \n"); |
acastiblancoc | 0:aa4d235c4151 | 148 | return; |
acastiblancoc | 0:aa4d235c4151 | 149 | } |
acastiblancoc | 0:aa4d235c4151 | 150 | void izquierda() |
acastiblancoc | 0:aa4d235c4151 | 151 | { |
acastiblancoc | 0:aa4d235c4151 | 152 | if( carro==0b00000110 || carro==0b00011000 ) |
acastiblancoc | 0:aa4d235c4151 | 153 | carro=carro<<2; |
acastiblancoc | 0:aa4d235c4151 | 154 | a.printf("entra izq \n"); |
acastiblancoc | 0:aa4d235c4151 | 155 | return; |
acastiblancoc | 0:aa4d235c4151 | 156 | } |
acastiblancoc | 0:aa4d235c4151 | 157 | |
acastiblancoc | 0:aa4d235c4151 | 158 | void colision () |
acastiblancoc | 0:aa4d235c4151 | 159 | { |
acastiblancoc | 0:aa4d235c4151 | 160 | borrar(); |
acastiblancoc | 0:aa4d235c4151 | 161 | sendSPI(0x0F,0); |
acastiblancoc | 0:aa4d235c4151 | 162 | sendSPI(3,0b01111110); |
acastiblancoc | 0:aa4d235c4151 | 163 | sendSPI(4,0b01111110); |
acastiblancoc | 0:aa4d235c4151 | 164 | sendSPI(5,0b01111110); |
acastiblancoc | 0:aa4d235c4151 | 165 | sendSPI(6,0b01111110); |
acastiblancoc | 0:aa4d235c4151 | 166 | wait(2); |
acastiblancoc | 0:aa4d235c4151 | 167 | juego(); |
acastiblancoc | 0:aa4d235c4151 | 168 | } |
acastiblancoc | 0:aa4d235c4151 | 169 | |
acastiblancoc | 0:aa4d235c4151 | 170 | void accelerometro(){ |
acastiblancoc | 0:aa4d235c4151 | 171 | |
acastiblancoc | 0:aa4d235c4151 | 172 | Wire.getAccelero(q); |
acastiblancoc | 0:aa4d235c4151 | 173 | a.printf("accelero: \t x= %f,\t y= %f, \t z= %f \n",q[0],q[1],q[2]); |
acastiblancoc | 0:aa4d235c4151 | 174 | int accelero[3]; |
acastiblancoc | 0:aa4d235c4151 | 175 | Wire.getAcceleroRaw(accelero); |
acastiblancoc | 0:aa4d235c4151 | 176 | int valor=accelero[1]; |
acastiblancoc | 0:aa4d235c4151 | 177 | if(valor<=(2000) && valor>=(1000)){ |
acastiblancoc | 0:aa4d235c4151 | 178 | az=1;} |
acastiblancoc | 0:aa4d235c4151 | 179 | if(valor<(1000) && valor>=(-1000)){ |
acastiblancoc | 0:aa4d235c4151 | 180 | az=2;} |
acastiblancoc | 0:aa4d235c4151 | 181 | if(valor<(-1000) && valor>=(-2000)){ |
acastiblancoc | 0:aa4d235c4151 | 182 | az=3;} |
acastiblancoc | 0:aa4d235c4151 | 183 | a.printf("acel : \t %d \n",az); |
acastiblancoc | 0:aa4d235c4151 | 184 | wait(0.8); |
acastiblancoc | 0:aa4d235c4151 | 185 | } |
acastiblancoc | 0:aa4d235c4151 | 186 | |
acastiblancoc | 0:aa4d235c4151 | 187 | void cambiof(){ |
acastiblancoc | 0:aa4d235c4151 | 188 | |
acastiblancoc | 0:aa4d235c4151 | 189 | switch (az){ |
acastiblancoc | 0:aa4d235c4151 | 190 | case 1: carro=cam[1]; |
acastiblancoc | 0:aa4d235c4151 | 191 | break; |
acastiblancoc | 0:aa4d235c4151 | 192 | case 2: carro=cam[2]; |
acastiblancoc | 0:aa4d235c4151 | 193 | break; |
acastiblancoc | 0:aa4d235c4151 | 194 | case 3: carro=cam[3]; |
acastiblancoc | 0:aa4d235c4151 | 195 | break; |
acastiblancoc | 0:aa4d235c4151 | 196 | } |
acastiblancoc | 0:aa4d235c4151 | 197 | |
acastiblancoc | 0:aa4d235c4151 | 198 | |
acastiblancoc | 0:aa4d235c4151 | 199 | } |
acastiblancoc | 0:aa4d235c4151 | 200 | |
acastiblancoc | 0:aa4d235c4151 | 201 | ///////////////////////////////////////////////////////////////////////////////////// |
acastiblancoc | 0:aa4d235c4151 | 202 | |
acastiblancoc | 0:aa4d235c4151 | 203 | void init_display() |
acastiblancoc | 0:aa4d235c4151 | 204 | { |
acastiblancoc | 0:aa4d235c4151 | 205 | sendSPI(0x0c,1); |
acastiblancoc | 0:aa4d235c4151 | 206 | sendSPI(0x0b,7); |
acastiblancoc | 0:aa4d235c4151 | 207 | sendSPI(0x09,0); |
acastiblancoc | 0:aa4d235c4151 | 208 | sendSPI(0x0A,0x0E); |
acastiblancoc | 0:aa4d235c4151 | 209 | int j; |
acastiblancoc | 0:aa4d235c4151 | 210 | for (j=0;j<4;j++){ |
acastiblancoc | 0:aa4d235c4151 | 211 | sendSPI(0x0F,1); |
acastiblancoc | 0:aa4d235c4151 | 212 | wait (0.2); |
acastiblancoc | 0:aa4d235c4151 | 213 | sendSPI(0x0f,0); |
acastiblancoc | 0:aa4d235c4151 | 214 | wait (0.2); |
acastiblancoc | 0:aa4d235c4151 | 215 | } |
acastiblancoc | 0:aa4d235c4151 | 216 | } |
acastiblancoc | 0:aa4d235c4151 | 217 | |
acastiblancoc | 0:aa4d235c4151 | 218 | void copy_matrix(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) |
acastiblancoc | 0:aa4d235c4151 | 219 | { |
acastiblancoc | 0:aa4d235c4151 | 220 | if(((matrix_act[cont-2]& *(fig+c)>>columna-1)==0)&&((matrix_act[cont-1]& *(fig+b)>>columna-1)==0)&&((matrix_act[cont]& *(fig+a)>>columna-1)==0)){ |
acastiblancoc | 0:aa4d235c4151 | 221 | matrix_temp[0]= *(fig+5)>>columna-1; |
acastiblancoc | 0:aa4d235c4151 | 222 | matrix_temp[1]= *(fig+a)>>columna-1; |
acastiblancoc | 0:aa4d235c4151 | 223 | matrix_temp[2]= *(fig+b)>>columna-1; |
acastiblancoc | 0:aa4d235c4151 | 224 | matrix_temp[3]= *(fig+c)>>columna-1; |
acastiblancoc | 0:aa4d235c4151 | 225 | } |
acastiblancoc | 0:aa4d235c4151 | 226 | } |
acastiblancoc | 0:aa4d235c4151 | 227 | |
acastiblancoc | 0:aa4d235c4151 | 228 | void borraraa() |
acastiblancoc | 0:aa4d235c4151 | 229 | { |
acastiblancoc | 0:aa4d235c4151 | 230 | int i; |
acastiblancoc | 0:aa4d235c4151 | 231 | for(i=0;i<=8;i++) |
acastiblancoc | 0:aa4d235c4151 | 232 | { |
acastiblancoc | 0:aa4d235c4151 | 233 | sendSPI(i,0); |
acastiblancoc | 0:aa4d235c4151 | 234 | matrix_act[i]=0; |
acastiblancoc | 0:aa4d235c4151 | 235 | } |
acastiblancoc | 0:aa4d235c4151 | 236 | }; |
acastiblancoc | 0:aa4d235c4151 | 237 | |
acastiblancoc | 0:aa4d235c4151 | 238 | void buscar_fila(){ |
acastiblancoc | 0:aa4d235c4151 | 239 | for (uint8_t i=0;i<9;i++){ |
acastiblancoc | 0:aa4d235c4151 | 240 | if((matrix_act[i]& matrix_temp[3])==0){ |
acastiblancoc | 0:aa4d235c4151 | 241 | fila=i; |
acastiblancoc | 0:aa4d235c4151 | 242 | } |
acastiblancoc | 0:aa4d235c4151 | 243 | if(((matrix_act[i]& matrix_temp[3])!=0)&& (i!=9)){ |
acastiblancoc | 0:aa4d235c4151 | 244 | fila=i-1; |
acastiblancoc | 0:aa4d235c4151 | 245 | command.printf("Fila pos 1: %d \n", fila); |
acastiblancoc | 0:aa4d235c4151 | 246 | i=9; |
acastiblancoc | 0:aa4d235c4151 | 247 | } |
acastiblancoc | 0:aa4d235c4151 | 248 | if(((matrix_act[i]& matrix_temp[2])!=0)&& (i!=9)){ |
acastiblancoc | 0:aa4d235c4151 | 249 | fila=i; |
acastiblancoc | 0:aa4d235c4151 | 250 | i=9; |
acastiblancoc | 0:aa4d235c4151 | 251 | command.printf("Fila pos 2: %d \n", fila); |
acastiblancoc | 0:aa4d235c4151 | 252 | } |
acastiblancoc | 0:aa4d235c4151 | 253 | if(((matrix_act[i]& matrix_temp[1])!=0)&& (i!=9)){ |
acastiblancoc | 0:aa4d235c4151 | 254 | fila=i+1; |
acastiblancoc | 0:aa4d235c4151 | 255 | i=9; |
acastiblancoc | 0:aa4d235c4151 | 256 | command.printf("Fila pos 3: %d \n", fila); |
acastiblancoc | 0:aa4d235c4151 | 257 | } |
acastiblancoc | 0:aa4d235c4151 | 258 | } |
acastiblancoc | 0:aa4d235c4151 | 259 | if(fila>8) |
acastiblancoc | 0:aa4d235c4151 | 260 | fila=8; |
acastiblancoc | 0:aa4d235c4151 | 261 | if(fila<=1){ |
acastiblancoc | 0:aa4d235c4151 | 262 | punto=1; |
acastiblancoc | 0:aa4d235c4151 | 263 | VEL=500; |
acastiblancoc | 0:aa4d235c4151 | 264 | wait_ms(VEL); |
acastiblancoc | 0:aa4d235c4151 | 265 | borrar(); |
acastiblancoc | 0:aa4d235c4151 | 266 | fila=8; |
acastiblancoc | 0:aa4d235c4151 | 267 | } |
acastiblancoc | 0:aa4d235c4151 | 268 | }; |
acastiblancoc | 0:aa4d235c4151 | 269 | |
acastiblancoc | 0:aa4d235c4151 | 270 | void guardar(){ |
acastiblancoc | 0:aa4d235c4151 | 271 | matrix_act[fila-3]=matrix_temp1[0]; |
acastiblancoc | 0:aa4d235c4151 | 272 | matrix_act[fila-2]=matrix_temp1[1]; |
acastiblancoc | 0:aa4d235c4151 | 273 | matrix_act[fila-1]=matrix_temp1[2]; |
acastiblancoc | 0:aa4d235c4151 | 274 | matrix_act[fila ]=matrix_temp1[3]; |
acastiblancoc | 0:aa4d235c4151 | 275 | }; |
acastiblancoc | 0:aa4d235c4151 | 276 | |
acastiblancoc | 0:aa4d235c4151 | 277 | void reada() |
acastiblancoc | 0:aa4d235c4151 | 278 | { vx=0; |
acastiblancoc | 0:aa4d235c4151 | 279 | vy=0; |
acastiblancoc | 0:aa4d235c4151 | 280 | vx=joy.read(); |
acastiblancoc | 0:aa4d235c4151 | 281 | vy=joy1.read(); |
acastiblancoc | 0:aa4d235c4151 | 282 | cc=cambios.read(); |
acastiblancoc | 0:aa4d235c4151 | 283 | } |
acastiblancoc | 0:aa4d235c4151 | 284 | |
acastiblancoc | 0:aa4d235c4151 | 285 | void bajaf() |
acastiblancoc | 0:aa4d235c4151 | 286 | { |
acastiblancoc | 0:aa4d235c4151 | 287 | movimiento=4;} |
acastiblancoc | 0:aa4d235c4151 | 288 | void movimiento_fig(){ |
acastiblancoc | 0:aa4d235c4151 | 289 | reada(); |
acastiblancoc | 0:aa4d235c4151 | 290 | command.printf("vy = %f \n", vy); |
acastiblancoc | 0:aa4d235c4151 | 291 | command.printf("vx = %f \n", vx); |
acastiblancoc | 0:aa4d235c4151 | 292 | command.printf("cc = %f \n", cambios.read()); |
acastiblancoc | 0:aa4d235c4151 | 293 | if(vy<0.2) |
acastiblancoc | 0:aa4d235c4151 | 294 | movimiento=1; |
acastiblancoc | 0:aa4d235c4151 | 295 | if(vy>0.8) |
acastiblancoc | 0:aa4d235c4151 | 296 | movimiento=4; |
acastiblancoc | 0:aa4d235c4151 | 297 | if(vx<0.2) |
acastiblancoc | 0:aa4d235c4151 | 298 | movimiento=3; |
acastiblancoc | 0:aa4d235c4151 | 299 | if(vx>0.80) |
acastiblancoc | 0:aa4d235c4151 | 300 | movimiento=2; |
acastiblancoc | 0:aa4d235c4151 | 301 | command.printf("movimiento = %d \n", movimiento); |
acastiblancoc | 0:aa4d235c4151 | 302 | switch (movimiento){ |
acastiblancoc | 0:aa4d235c4151 | 303 | |
acastiblancoc | 0:aa4d235c4151 | 304 | case 4: |
acastiblancoc | 0:aa4d235c4151 | 305 | cont++; |
acastiblancoc | 0:aa4d235c4151 | 306 | //printf("%f", joy.read()); |
acastiblancoc | 0:aa4d235c4151 | 307 | if(cont>8)cont=8; |
acastiblancoc | 0:aa4d235c4151 | 308 | break; |
acastiblancoc | 0:aa4d235c4151 | 309 | |
acastiblancoc | 0:aa4d235c4151 | 310 | case 1: |
acastiblancoc | 0:aa4d235c4151 | 311 | if( (matrix_temp[1]==2) || (matrix_temp[1]==1)|| (matrix_temp[1]==3)) |
acastiblancoc | 0:aa4d235c4151 | 312 | columna--; |
acastiblancoc | 0:aa4d235c4151 | 313 | rotacion++; |
acastiblancoc | 0:aa4d235c4151 | 314 | if(rotacion>4) |
acastiblancoc | 0:aa4d235c4151 | 315 | rotacion=1; |
acastiblancoc | 0:aa4d235c4151 | 316 | break; |
acastiblancoc | 0:aa4d235c4151 | 317 | |
acastiblancoc | 0:aa4d235c4151 | 318 | case 2: |
acastiblancoc | 0:aa4d235c4151 | 319 | if((matrix_temp[2]==7 || matrix_temp[3]==7)||(matrix_temp[2]==3 && matrix_temp[3]==6)) { |
acastiblancoc | 0:aa4d235c4151 | 320 | command.printf("a"); |
acastiblancoc | 0:aa4d235c4151 | 321 | columna=6; |
acastiblancoc | 0:aa4d235c4151 | 322 | } |
acastiblancoc | 0:aa4d235c4151 | 323 | else if((matrix_temp[1]==2 && matrix_temp[2]==2 && matrix_temp[3]==2)){ |
acastiblancoc | 0:aa4d235c4151 | 324 | command.printf("b"); |
acastiblancoc | 0:aa4d235c4151 | 325 | columna=8; |
acastiblancoc | 0:aa4d235c4151 | 326 | } |
acastiblancoc | 0:aa4d235c4151 | 327 | else if (columna<7){ |
acastiblancoc | 0:aa4d235c4151 | 328 | command.printf("c"); |
acastiblancoc | 0:aa4d235c4151 | 329 | columna++; |
acastiblancoc | 0:aa4d235c4151 | 330 | } |
acastiblancoc | 0:aa4d235c4151 | 331 | //printf("%f", joy1.read()); |
acastiblancoc | 0:aa4d235c4151 | 332 | break; |
acastiblancoc | 0:aa4d235c4151 | 333 | |
acastiblancoc | 0:aa4d235c4151 | 334 | case 3: |
acastiblancoc | 0:aa4d235c4151 | 335 | columna--; |
acastiblancoc | 0:aa4d235c4151 | 336 | if(columna<1) |
acastiblancoc | 0:aa4d235c4151 | 337 | columna=1; |
acastiblancoc | 0:aa4d235c4151 | 338 | //printf("%f", joy.read()); |
acastiblancoc | 0:aa4d235c4151 | 339 | break; |
acastiblancoc | 0:aa4d235c4151 | 340 | |
acastiblancoc | 0:aa4d235c4151 | 341 | } |
acastiblancoc | 0:aa4d235c4151 | 342 | } |
acastiblancoc | 0:aa4d235c4151 | 343 | |
acastiblancoc | 0:aa4d235c4151 | 344 | |
acastiblancoc | 0:aa4d235c4151 | 345 | void print_matrix(uint8_t *temp){ |
acastiblancoc | 0:aa4d235c4151 | 346 | matrix_temp1[0]=(matrix_temp[0]+ matrix_act[cont-3]); |
acastiblancoc | 0:aa4d235c4151 | 347 | matrix_temp1[1]=(matrix_temp[1]+ matrix_act[cont-2]); |
acastiblancoc | 0:aa4d235c4151 | 348 | matrix_temp1[2]=(matrix_temp[2]+ matrix_act[cont-1]); |
acastiblancoc | 0:aa4d235c4151 | 349 | matrix_temp1[3]=(matrix_temp[3]+ matrix_act[cont]); |
acastiblancoc | 0:aa4d235c4151 | 350 | sendSPI(cont-3,matrix_temp1[0]); |
acastiblancoc | 0:aa4d235c4151 | 351 | sendSPI(cont-2,matrix_temp1[1]); |
acastiblancoc | 0:aa4d235c4151 | 352 | sendSPI(cont-1,matrix_temp1[2]); |
acastiblancoc | 0:aa4d235c4151 | 353 | sendSPI(cont ,matrix_temp1[3]); //pasa de i+2 a i+1 |
acastiblancoc | 0:aa4d235c4151 | 354 | if(punto%3==0) |
acastiblancoc | 0:aa4d235c4151 | 355 | { |
acastiblancoc | 0:aa4d235c4151 | 356 | VEL=VEL/3; |
acastiblancoc | 0:aa4d235c4151 | 357 | if(VEL<=50) |
acastiblancoc | 0:aa4d235c4151 | 358 | VEL=50; |
acastiblancoc | 0:aa4d235c4151 | 359 | } |
acastiblancoc | 0:aa4d235c4151 | 360 | wait_ms(VEL); |
acastiblancoc | 0:aa4d235c4151 | 361 | } |
acastiblancoc | 0:aa4d235c4151 | 362 | |
acastiblancoc | 0:aa4d235c4151 | 363 | void crear_fig(char type_fig,char rotacion,char columna) |
acastiblancoc | 0:aa4d235c4151 | 364 | { |
acastiblancoc | 0:aa4d235c4151 | 365 | switch (type_fig){ |
acastiblancoc | 0:aa4d235c4151 | 366 | case 1: if(columna>7) |
acastiblancoc | 0:aa4d235c4151 | 367 | columna=7; |
acastiblancoc | 0:aa4d235c4151 | 368 | copy_matrix(5,1,1,PLANT,columna); break; |
acastiblancoc | 0:aa4d235c4151 | 369 | |
acastiblancoc | 0:aa4d235c4151 | 370 | case 2: if(rotacion==1 || rotacion==3) |
acastiblancoc | 0:aa4d235c4151 | 371 | copy_matrix(0,0,0,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 372 | if(rotacion==2 || rotacion==4) |
acastiblancoc | 0:aa4d235c4151 | 373 | copy_matrix(5,5,2,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 374 | break; |
acastiblancoc | 0:aa4d235c4151 | 375 | |
acastiblancoc | 0:aa4d235c4151 | 376 | case 3: if(rotacion==1) |
acastiblancoc | 0:aa4d235c4151 | 377 | copy_matrix(0,0,1,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 378 | if(rotacion==2) |
acastiblancoc | 0:aa4d235c4151 | 379 | copy_matrix(5,2,0,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 380 | if(rotacion==3) |
acastiblancoc | 0:aa4d235c4151 | 381 | copy_matrix(1,3,3,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 382 | if(rotacion==4) |
acastiblancoc | 0:aa4d235c4151 | 383 | copy_matrix(5,6,2,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 384 | break; |
acastiblancoc | 0:aa4d235c4151 | 385 | |
acastiblancoc | 0:aa4d235c4151 | 386 | case 4: if(rotacion==1) |
acastiblancoc | 0:aa4d235c4151 | 387 | copy_matrix(5,3,2,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 388 | if(rotacion==2) |
acastiblancoc | 0:aa4d235c4151 | 389 | copy_matrix(0,1,0,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 390 | if(rotacion==3) |
acastiblancoc | 0:aa4d235c4151 | 391 | copy_matrix(5,2,3,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 392 | if(rotacion==4) |
acastiblancoc | 0:aa4d235c4151 | 393 | copy_matrix(3,1,3,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 394 | break; |
acastiblancoc | 0:aa4d235c4151 | 395 | |
acastiblancoc | 0:aa4d235c4151 | 396 | case 5: if(rotacion==1 || rotacion==3) |
acastiblancoc | 0:aa4d235c4151 | 397 | copy_matrix(5,4,1,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 398 | if(rotacion==2 || rotacion==4) |
acastiblancoc | 0:aa4d235c4151 | 399 | copy_matrix(0,1,3,PLANT,columna); |
acastiblancoc | 0:aa4d235c4151 | 400 | break; |
acastiblancoc | 0:aa4d235c4151 | 401 | } |
acastiblancoc | 0:aa4d235c4151 | 402 | } |
acastiblancoc | 0:aa4d235c4151 | 403 | |
acastiblancoc | 0:aa4d235c4151 | 404 | void figuras(){ |
acastiblancoc | 0:aa4d235c4151 | 405 | figura= rand() % 5+1; |
acastiblancoc | 0:aa4d235c4151 | 406 | rotacion= rand() % 4+1; |
acastiblancoc | 0:aa4d235c4151 | 407 | columna= 4; |
acastiblancoc | 0:aa4d235c4151 | 408 | } |
acastiblancoc | 0:aa4d235c4151 | 409 | |
acastiblancoc | 0:aa4d235c4151 | 410 | void elimina_linea(){ |
acastiblancoc | 0:aa4d235c4151 | 411 | for(uint8_t i=0;i<9;i++){ |
acastiblancoc | 0:aa4d235c4151 | 412 | if(matrix_act[i]==255){ |
acastiblancoc | 0:aa4d235c4151 | 413 | for(uint8_t a=i;a>=1;a--){ |
acastiblancoc | 0:aa4d235c4151 | 414 | sendSPI(a,matrix_act[a-1]); |
acastiblancoc | 0:aa4d235c4151 | 415 | matrix_act[a]=matrix_act[a-1]; |
acastiblancoc | 0:aa4d235c4151 | 416 | } |
acastiblancoc | 0:aa4d235c4151 | 417 | punto++; |
acastiblancoc | 0:aa4d235c4151 | 418 | printf("%f", punto); |
acastiblancoc | 0:aa4d235c4151 | 419 | } |
acastiblancoc | 0:aa4d235c4151 | 420 | } |
acastiblancoc | 0:aa4d235c4151 | 421 | } |
acastiblancoc | 0:aa4d235c4151 | 422 | |
acastiblancoc | 0:aa4d235c4151 | 423 | void tetris() { |
acastiblancoc | 0:aa4d235c4151 | 424 | bajar.attach(&bajaf, 0.1); |
acastiblancoc | 0:aa4d235c4151 | 425 | init_display(); |
acastiblancoc | 0:aa4d235c4151 | 426 | borrar(); |
acastiblancoc | 0:aa4d235c4151 | 427 | while(1){ |
acastiblancoc | 0:aa4d235c4151 | 428 | figuras(); |
acastiblancoc | 0:aa4d235c4151 | 429 | buscar_fila(); |
acastiblancoc | 0:aa4d235c4151 | 430 | cont=0; |
acastiblancoc | 0:aa4d235c4151 | 431 | elimina_linea(); |
acastiblancoc | 0:aa4d235c4151 | 432 | while (cont<fila){ |
acastiblancoc | 0:aa4d235c4151 | 433 | movimiento_fig(); |
acastiblancoc | 0:aa4d235c4151 | 434 | crear_fig(figura,rotacion,columna); |
acastiblancoc | 0:aa4d235c4151 | 435 | buscar_fila(); |
acastiblancoc | 0:aa4d235c4151 | 436 | print_matrix(matrix_temp); |
acastiblancoc | 0:aa4d235c4151 | 437 | } |
acastiblancoc | 0:aa4d235c4151 | 438 | guardar(); |
acastiblancoc | 0:aa4d235c4151 | 439 | } |
acastiblancoc | 0:aa4d235c4151 | 440 | } |
acastiblancoc | 0:aa4d235c4151 | 441 | ////////////////////////////////////////////////////////////////////////////////////////////// |
acastiblancoc | 0:aa4d235c4151 | 442 | |
acastiblancoc | 0:aa4d235c4151 | 443 | void hacer_carita() |
acastiblancoc | 0:aa4d235c4151 | 444 | { |
acastiblancoc | 0:aa4d235c4151 | 445 | int k; |
acastiblancoc | 0:aa4d235c4151 | 446 | uint8_t carita[8]={0b00000000, 0, 0b00100100,0b00100100,0,0, 0b01000010,0b00111100}; |
acastiblancoc | 0:aa4d235c4151 | 447 | for(k=1;k<=8;k++) |
acastiblancoc | 0:aa4d235c4151 | 448 | { |
acastiblancoc | 0:aa4d235c4151 | 449 | sendSPI(k ,carita[k]);} |
acastiblancoc | 0:aa4d235c4151 | 450 | } |
acastiblancoc | 0:aa4d235c4151 | 451 | void inicializar_menu(){ |
acastiblancoc | 0:aa4d235c4151 | 452 | sendSPI(0x0c,1); |
acastiblancoc | 0:aa4d235c4151 | 453 | sendSPI(0x0b,7); |
acastiblancoc | 0:aa4d235c4151 | 454 | sendSPI(0x09,0); |
acastiblancoc | 0:aa4d235c4151 | 455 | sendSPI(0x0A,0x0E); |
acastiblancoc | 0:aa4d235c4151 | 456 | int j=0; |
acastiblancoc | 0:aa4d235c4151 | 457 | for (j=0;j<4;j++){ |
acastiblancoc | 0:aa4d235c4151 | 458 | hacer_carita(); |
acastiblancoc | 0:aa4d235c4151 | 459 | wait (0.2); |
acastiblancoc | 0:aa4d235c4151 | 460 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 461 | wait (0.2); |
acastiblancoc | 0:aa4d235c4151 | 462 | } |
acastiblancoc | 0:aa4d235c4151 | 463 | } |
acastiblancoc | 0:aa4d235c4151 | 464 | |
acastiblancoc | 0:aa4d235c4151 | 465 | void eleccion_tetris() |
acastiblancoc | 0:aa4d235c4151 | 466 | { |
acastiblancoc | 0:aa4d235c4151 | 467 | int k; |
acastiblancoc | 0:aa4d235c4151 | 468 | uint8_t elec[8]={0b00000000, 0,0, 0b00000000,0b11010011,0b11011010,0b00001010, 0}; |
acastiblancoc | 0:aa4d235c4151 | 469 | for(k=1;k<=8;k++) |
acastiblancoc | 0:aa4d235c4151 | 470 | { |
acastiblancoc | 0:aa4d235c4151 | 471 | sendSPI(k ,elec[k]);} |
acastiblancoc | 0:aa4d235c4151 | 472 | /* reada(); |
acastiblancoc | 0:aa4d235c4151 | 473 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 474 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 475 | carro_juego();} |
acastiblancoc | 0:aa4d235c4151 | 476 | }*/ |
acastiblancoc | 0:aa4d235c4151 | 477 | } |
acastiblancoc | 0:aa4d235c4151 | 478 | |
acastiblancoc | 0:aa4d235c4151 | 479 | void eleccion_carrito() |
acastiblancoc | 0:aa4d235c4151 | 480 | { |
acastiblancoc | 0:aa4d235c4151 | 481 | int k; |
acastiblancoc | 0:aa4d235c4151 | 482 | uint8_t elec1[8]={0b00000000, 0,0,0b00111000,0b01111100,0b00101000, 0, 0b11111111}; |
acastiblancoc | 0:aa4d235c4151 | 483 | for(k=1;k<=8;k++) |
acastiblancoc | 0:aa4d235c4151 | 484 | { |
acastiblancoc | 0:aa4d235c4151 | 485 | sendSPI(k ,elec1[k]);} |
acastiblancoc | 0:aa4d235c4151 | 486 | /*reada(); |
acastiblancoc | 0:aa4d235c4151 | 487 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 488 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 489 | tetris();}*/ |
acastiblancoc | 0:aa4d235c4151 | 490 | } |
acastiblancoc | 0:aa4d235c4151 | 491 | |
acastiblancoc | 0:aa4d235c4151 | 492 | int main() |
acastiblancoc | 0:aa4d235c4151 | 493 | { int game=0; |
acastiblancoc | 0:aa4d235c4151 | 494 | int pant=1; |
acastiblancoc | 0:aa4d235c4151 | 495 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 496 | inicializar_menu(); |
acastiblancoc | 0:aa4d235c4151 | 497 | //eleccion_tetris(); |
acastiblancoc | 0:aa4d235c4151 | 498 | while(1) |
acastiblancoc | 0:aa4d235c4151 | 499 | { |
acastiblancoc | 0:aa4d235c4151 | 500 | game=0; |
acastiblancoc | 0:aa4d235c4151 | 501 | reada(); |
acastiblancoc | 0:aa4d235c4151 | 502 | if(vy<0.48) |
acastiblancoc | 0:aa4d235c4151 | 503 | game=1; |
acastiblancoc | 0:aa4d235c4151 | 504 | if(vy>0.58) |
acastiblancoc | 0:aa4d235c4151 | 505 | game=2; |
acastiblancoc | 0:aa4d235c4151 | 506 | |
acastiblancoc | 0:aa4d235c4151 | 507 | switch(game) |
acastiblancoc | 0:aa4d235c4151 | 508 | { |
acastiblancoc | 0:aa4d235c4151 | 509 | case 1: |
acastiblancoc | 0:aa4d235c4151 | 510 | pant++; |
acastiblancoc | 0:aa4d235c4151 | 511 | if(pant%2!=0) |
acastiblancoc | 0:aa4d235c4151 | 512 | {borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 513 | eleccion_tetris(); |
acastiblancoc | 0:aa4d235c4151 | 514 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 515 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 516 | carro_juego();} |
acastiblancoc | 0:aa4d235c4151 | 517 | } |
acastiblancoc | 0:aa4d235c4151 | 518 | if(pant%2==0) |
acastiblancoc | 0:aa4d235c4151 | 519 | {borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 520 | eleccion_carrito(); |
acastiblancoc | 0:aa4d235c4151 | 521 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 522 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 523 | tetris();} |
acastiblancoc | 0:aa4d235c4151 | 524 | } |
acastiblancoc | 0:aa4d235c4151 | 525 | break; |
acastiblancoc | 0:aa4d235c4151 | 526 | |
acastiblancoc | 0:aa4d235c4151 | 527 | case 2: |
acastiblancoc | 0:aa4d235c4151 | 528 | pant++; |
acastiblancoc | 0:aa4d235c4151 | 529 | if(pant%2!=0) |
acastiblancoc | 0:aa4d235c4151 | 530 | {borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 531 | eleccion_tetris(); |
acastiblancoc | 0:aa4d235c4151 | 532 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 533 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 534 | carro_juego();} |
acastiblancoc | 0:aa4d235c4151 | 535 | } |
acastiblancoc | 0:aa4d235c4151 | 536 | if(pant%2==0) |
acastiblancoc | 0:aa4d235c4151 | 537 | { borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 538 | eleccion_carrito(); |
acastiblancoc | 0:aa4d235c4151 | 539 | if(vx>0.6){ |
acastiblancoc | 0:aa4d235c4151 | 540 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 541 | tetris();} |
acastiblancoc | 0:aa4d235c4151 | 542 | } |
acastiblancoc | 0:aa4d235c4151 | 543 | break; |
acastiblancoc | 0:aa4d235c4151 | 544 | } |
acastiblancoc | 0:aa4d235c4151 | 545 | //printf(" pantalla %d \n", pant); |
acastiblancoc | 0:aa4d235c4151 | 546 | wait(0.5); |
acastiblancoc | 0:aa4d235c4151 | 547 | //printf("entra a %d \n", game); |
acastiblancoc | 0:aa4d235c4151 | 548 | //wait(1); |
acastiblancoc | 0:aa4d235c4151 | 549 | //printf(" vx %f \n", vx); |
acastiblancoc | 0:aa4d235c4151 | 550 | |
acastiblancoc | 0:aa4d235c4151 | 551 | } |
acastiblancoc | 0:aa4d235c4151 | 552 | } |
acastiblancoc | 0:aa4d235c4151 | 553 | |
acastiblancoc | 0:aa4d235c4151 | 554 | |
acastiblancoc | 0:aa4d235c4151 | 555 | /* |
acastiblancoc | 0:aa4d235c4151 | 556 | wait(1); |
acastiblancoc | 0:aa4d235c4151 | 557 | borraraa(); |
acastiblancoc | 0:aa4d235c4151 | 558 | eleccion_carrito(); |
acastiblancoc | 0:aa4d235c4151 | 559 | wait(1);*/ |
acastiblancoc | 0:aa4d235c4151 | 560 |