Why Does My Program Crash?
I finally got working on my snake game again. Right now, i've only gotten it to work to the point where you can move one piece of the snake around. Now i'm trying to get it to seem like a whole snake is moving. The way I'm doing it is that I'm deleting the back end of the snake and adding another one at the head. This probably isn't the best way to do it, so if you could tell me what would be a better way, then that would be good. But right now, the game is crashing right as it starts. Here's the code:
Code:
//Includes
#include <allegro.h>
#include<iostream>
#include<string>
#include <stdlib.h>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <complex>
#include <math.h>
#include <cmath>
using namespace std;
//Graphics
BITMAP *snakeimg = NULL;
BITMAP *buffer = NULL;
//Sounds
//Variables
float x,y;
float dir = 45;
float dx = sin(dir);
float dy = cos(dir);
#define PI 3.14159265
int maxsize = 3;
//Structs
struct snake {
int x;
int y;
};
struct fruit {
int x;
int y;
};
//Vectors
vector<snake> snakes(0);
//Functions
void UpdateSnake();
void TestKeys();
int main(int argc, char argv[1])
{
allegro_init();
install_keyboard();
set_color_depth(24);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,400,0,0);
//BITMAPS
snakeimg = load_bitmap("Snake.bmp",NULL);
buffer = create_bitmap(400,400);
while(!key[KEY_ESC])
{
clear_bitmap(buffer);
TestKeys();
UpdateSnake();
draw_sprite(screen,buffer,0,0);
}
allegro_exit();
return 0;
//Cleanup
for(int i = 0;i < snakes.size(); i++);
{
snakes.erase(snakes.begin()+i);
}
destroy_bitmap(snakeimg);
destroy_bitmap(buffer);
}
END_OF_MAIN();
void UpdateSnake()
{
dx = sin(dir*PI/180);
dy = cos(dir*PI/180);
x = x + dx/5;
y = y + dy/5;
snake w;
w.x = x;
w.y = y;
snakes.push_back(w);
if(snakes.size() > maxsize)
{
snakes.erase(snakes.begin()+snakes.size());
}
for(int i = 0; i < snakes.size(); i++)
{
draw_sprite(buffer,snakeimg,snakes[i].x,snakes[i].y);
}
}
void TestKeys()
{
if(key[KEY_LEFT])
{
dir = dir + .4;
}
else if(key[KEY_RIGHT])
{
dir = dir - .4;
}
}