Final Proyect

Hello,

Here you will find my final proyect which took me all semester to decide what to do, at the end i worked side by side with a really good friend Martha, the blog that i shared with you some posts ago,

The game is based on Snake from years ago and it took us a while to finish it but at the end it worth it, we share with you the code and its really important to mention that to make it work we download CodeBlocks which helps us to write the code and compile in the same program:

#include <windows.h>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <stdio.h>

#define Arriba 72 //Define numeros asociados a las flechas
#define Izquierda 75
#define Derecha 77
#define Abajo 80
#define Esc 27
int cuerpo[200][2];
int n=1, tam=4, x=10, y=12, dir=3, xc=30, yc=15, velocidad=100, score=0, h=1;
char tecla;

void gotoxy(int x, int y) //funcion posicion de “x” y “y”
{
HANDLE hCon;
COORD dwPos;

dwPos.X =x;
dwPos.Y =y;
hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hCon,dwPos);

}
void pintar(){
//lineas
for(int i = 2 ; i < 78 ; i++){
gotoxy(i,3);
printf(“%c”,205);
gotoxy(i,23);
printf(“%c”, 205);

}
//lineas verticales
for(int i = 4 ; i < 23 ; i++){
gotoxy(2,i);
printf(“%c”,186);
gotoxy(77,i);
printf(“%c”, 186);

}
//esquinas
gotoxy(2,3);
printf(“%c”,201);
gotoxy(2,23);
printf(“%c”, 200);
gotoxy(77,3);
printf(“%c”, 187);
gotoxy(77,23);
printf(“%c”, 188);

}
void guardar_posicion(){

cuerpo[n][0]=x;
cuerpo[n][1]=y;
n++; //sigue incrementando
if(n==tam) n=1;

}

void pintar_cuerpo(){

for(int i = 1 ; i < tam ; i++){
gotoxy(cuerpo[i][0], cuerpo[i][1]) ; printf(“*”);

}

}
void borrar_cuerpo(){
gotoxy(cuerpo[n][0], cuerpo[n][1]) ; printf(” “);

}
void teclear(){
if(kbhit()){
tecla=getch();
switch(tecla){
case Arriba:
if(dir!=2)
dir=1;
break;
case Abajo:
if(dir!=1)
dir=2;
break;
case Derecha:
if(dir!=4)
dir=3;
break;
case Izquierda:
if(dir!=3)
dir=4;
break;
}
}

}
void cambiar_velocidad(){
if(score==h*20){
velocidad-=10;
h++;
}
}
void

2016-05-05_0028


if(x==xc && y==yc){
xc=(rand()%73)+4;
yc=(rand()%19)+4;
tam++;
score +=10;
gotoxy(xc,yc);printf(“%c”, 4);

cambiar_velocidad();
}

}
bool game_over(){
if(y==3 || y==23 || x==2 || x==77) return false;//falta signo de O
for( int j = tam – 1 ; j > 0 ; j–){
if(cuerpo[j][0]== x && cuerpo[j][1]== y)
return false;
}
return true;
}
void puntos(){
gotoxy(3,1); printf(“Score %d”, score);
}
int main(){

pintar();
gotoxy(xc,yc);printf(“%c”, 4);

while(tecla != Esc && game_over())
{
borrar_cuerpo();
guardar_posicion();
pintar_cuerpo();

comida();

puntos();
teclear();
teclear();
teclear();
if(dir==1) y–;
if(dir==2) y++;
if(dir==3) x++;
if(dir==4) x–;

Sleep(velocidad);

}
pintar();
system(“pause>null”);
return 0;

}

 

The program works!!!

2016-05-05_0028

It was a hard proyect but at the end we both agreed the results where satisfactory, Martha will share with you the outcome of the other idea we had…

 

Thanks for reading,

 

 

 

 

 

-Free Money-

CC BY-SA 4.0 Final Proyect by edithsantoscom is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.