Ya está lista la ALPHA de mi juego. Aquí el código:
#include <allegro.h>
#include <list>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
const int XTAM = 60, YTAM = 40;
const int SZ = 12;
int vx = 1, vy = 0;
int al, score = 0;
BITMAP *cuadrado1;
BITMAP *cuadradobmp;
BITMAP *buffer;
BITMAP *fruta;
BITMAP *manzanabmp;
BITMAP *fresabmp;
BITMAP *bombabmp;
void draw_score()
{
rectfill(screen, 0,0,200,14,0);
textprintf(screen, font, 0, 0, -1, "SCORE: %d", score);
}
void pantalla(){
blit(buffer, screen, 0,0,0,0, XTAM*SZ, YTAM*SZ);
}
struct Punto{
int x,y;
};
void cuadrado(const Punto& p){
blit(cuadradobmp, cuadrado1, 0, 0, 0, 0, 12, 12);
draw_sprite(buffer, cuadrado1, p.x*SZ, p.y*SZ);
}
void pinta_comida(const Punto& p){
srand(time(0));
if (al==0) blit(manzanabmp, fruta, 0, 0, 0, 0, 12, 12);
if (al==1) blit(fresabmp, fruta, 0, 0, 0, 0, 12, 12);
draw_sprite(buffer, fruta, p.x*SZ, p.y*SZ);
}
Punto al_azar(){
srand(time(0));
Punto p = {rand() % XTAM, rand() % YTAM};
al = rand() % 2;
return p;
}
void teclas(){
if (key[KEY_UP] && vy != 1){
vx = 0, vy = -1;
}else if (key[KEY_DOWN] && vy != -1){
vx = 0; vy = 1;
}else if (key[KEY_LEFT] && vx != 1){
vx = -1, vy = 0;
}else if (key[KEY_RIGHT] && vx != -1){
vx = 1, vy = 0;
}
}
bool hay_choque(const Punto& cabeza, const list<Punto>& cola){
if (cabeza.x >= XTAM || cabeza.x < 0) return true;
if (cabeza.y >= YTAM || cabeza.y < 0) return true;
list <Punto>::const_iterator it = cola.begin();
for (; it != cola.end(); it++){
if (cabeza.x == it->x && cabeza.y == it->y){
return true;
}
}
return false;
}
int main(){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, XTAM * SZ, YTAM * SZ, 0, 0);
buffer = create_bitmap(XTAM * SZ, YTAM * SZ);
cuadrado1 = create_bitmap(12,12);
cuadradobmp = load_bitmap("imagenes/cabeza.bmp", NULL);
fruta = create_bitmap(12,12);
fresabmp = load_bitmap("imagenes/fresa.bmp", NULL);
manzanabmp = load_bitmap("imagenes/manzana.bmp", NULL);
bombabmp = load_bitmap("imagenes/bomba.bmp", NULL); //Ya desvelo lo que incluire proximamente ;D
list<Punto> cola;
int engorda = 0;
int retraso = 0;
bool choque = false;
Punto cara = {30, 30};
Punto comida = al_azar();
while(!key[KEY_ESC] && !choque ){
pantalla();
retraso++;
teclas();
draw_score();
if (retraso==8){
cola.push_front(cara);
if (engorda > 0){
engorda--;
}else {
cola.pop_back();
}
cara.x += vx;
cara.y += vy;
if (hay_choque(cara, cola)){
choque = true;
} else if (cara.x == comida.x && cara.y == comida.y){
engorda = 5;
if (al==0) score += 5;
else if (al==1) score +=10;
comida = al_azar();
while (hay_choque(comida, cola) ||
comida.x == cara.x && comida.y == cara.y ||
comida.y < 5){
comida = al_azar();
}
}
retraso = 0;
}
if (!choque){
clear(buffer);
list<Punto>::const_iterator it;
for (it = cola.begin(); it != cola.end(); it++){
cuadrado(*it);
}
cuadrado(cara);
pinta_comida(comida);
}
rest(10);
}
rectfill(screen, 0,0,200,14,0);
textprintf(screen, font, 200, 200, -1, "Tu puntuacion ha sido de: %d", score);
rest(2000);
return 0;
}
END_OF_MAIN();
He usado Allegro, una lista para crear a mi serpiente y estructuras en lugar de clases. El juego de momento solo tiene una fruta cada vez, aunque hay varios tipos de fruta (cada una da diferentes puntos).
El juego funciona perfectamente, aunque aún me queda incluirle un menú, más frutas y algún tipo de jefe o algo así
PD: Cuando pueda subiré los archivos compilados en linux y windows