Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL/SDL.h>
- #include <SDL/SDL_gfxPrimitives.h>
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #define WIDTH 1280
- #define HEIGHT 1024
- #define BLOCK_SIZE 16
- #define BWIDTH (WIDTH/BLOCK_SIZE)
- #define BHEIGHT (HEIGHT/BLOCK_SIZE)
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- #define RMASK 0xff000000
- #define GMASK 0x00ff0000
- #define BMASK 0x0000ff00
- #define AMASK 0x000000ff
- #else
- #define RMASK 0x000000ff
- #define GMASK 0x0000ff00
- #define BMASK 0x00ff0000
- #define AMASK 0xff000000
- #endif
- SDL_Surface *screen, *background;
- short int foodx, foody;
- bool **walls;
- enum directions{left, right, up, down};
- struct block{
- short int x;
- short int y;
- block *next;
- };
- void draw_block(short int x, short int y, Uint32 color){
- boxColor(screen, x*BLOCK_SIZE, y*BLOCK_SIZE, (x+1)*BLOCK_SIZE, (y+1)*BLOCK_SIZE, color);
- }
- class Snake{
- private:
- block *last, *tmp;
- short int length, maxlength;
- directions direction, lastdir;
- void check_collisions(block* a){
- if(a->x >= BWIDTH || a->x < 0 || a->y >=BHEIGHT || a->y <0 || walls[a->x][a->y]) dead = true;
- for(tmp=last; tmp->next!=NULL; tmp=tmp->next)
- if(a->x == tmp->x && a->y == tmp->y){
- dead=true;
- break;
- }
- if(a->x == foodx && a->y ==foody){
- maxlength+=5;
- foodx = rand() % BWIDTH;
- foody = rand() % BHEIGHT;
- food_set:
- for(tmp=last; tmp!=NULL; tmp=tmp->next){
- if((foodx == tmp->x && foody == tmp->y) || walls[foodx][foody]){
- foodx = rand() % BWIDTH;
- foody = rand() % BHEIGHT;
- goto food_set;
- }
- }
- }
- }
- void push(){
- for(tmp=last; tmp->next; tmp = tmp->next);
- tmp->next = new block;
- switch(direction){
- case right:
- tmp->next->x=tmp->x+1;
- tmp->next->y=tmp->y;
- break;
- case left:
- tmp->next->x=tmp->x-1;
- tmp->next->y=tmp->y;
- break;
- case up:
- tmp->next->x=tmp->x;
- tmp->next->y=tmp->y-1;
- break;
- case down:
- tmp->next->x=tmp->x;
- tmp->next->y=tmp->y+1;
- break;
- }
- tmp->next->next=NULL;
- check_collisions(tmp->next);
- length++;
- }
- void pop(){
- tmp=last;
- last=last->next;
- delete tmp;
- length--;
- }
- public:
- bool dead;
- Snake(){
- dead = false;
- last = new block;
- last->x = BWIDTH/2;
- last->y = BHEIGHT/2;
- last->next = NULL;
- maxlength = 6;
- length = 1;
- lastdir = direction = right;
- }
- ~Snake(){
- while(last->next){
- tmp=last;
- last=last->next;
- delete tmp;
- }
- }
- void process_input(SDLKey sym){
- switch(sym){
- case SDLK_LEFT:
- if(lastdir!=right)direction = left;
- break;
- case SDLK_RIGHT:
- if(lastdir!=left)direction = right;
- break;
- case SDLK_UP:
- if(lastdir!=down)direction = up;
- break;
- case SDLK_DOWN:
- if(lastdir!=up)direction = down;
- break;
- default:
- break;
- }
- }
- void process(){
- if(length>maxlength) pop();
- push();
- lastdir = direction;
- }
- void print(){
- for(tmp=last; tmp!=NULL; tmp=tmp->next) draw_block(tmp->x, tmp->y, 0xFFFFFFFF);
- }
- };
- int init(){
- atexit(SDL_Quit);
- if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
- printf( "Unable to init SDL: %s\n", SDL_GetError() );
- return 1;
- }
- screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
- if ( !screen ){
- printf("Unable to set video mode: %s\n", SDL_GetError());
- return 1;
- }
- background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32, RMASK, GMASK, BMASK, AMASK);
- //code
- srand(time(NULL));
- foodx = rand() % BWIDTH;
- foody = rand() % BHEIGHT;
- return 0;
- }
- void init_walls(){
- walls = new bool* [BWIDTH];
- for(int i=0; i<BWIDTH; i++){
- walls[i] = new bool [BHEIGHT];
- for(int j=0; j<BHEIGHT; j++) walls[i][j]=false;
- }
- for(int i=2; i<BHEIGHT-3; i++)
- walls[4][i] = walls[5][i] = walls [BWIDTH-5][i] = walls [BWIDTH-6][i] = true;
- boxColor(background, 0, 0, WIDTH, HEIGHT, 0x000000FF);
- for(int i=0; i<BWIDTH; i++)
- for(int j=0; j<BHEIGHT; j++)
- if(walls[i][j]) boxColor(background, i*BLOCK_SIZE, j*BLOCK_SIZE, (i+1)*BLOCK_SIZE, (j+1)*BLOCK_SIZE, 0x0000AAFF);
- }
- int main ( int argc, char** argv ){
- long int frame=0;
- bool done;
- SDL_Event event;
- Snake snake;
- init();
- init_walls();
- while (!done){
- while(SDL_PollEvent(&event) )
- switch(event.type){
- case SDL_QUIT:
- done = true;
- break;
- case SDL_KEYDOWN:
- if(event.key.keysym.sym == 'q') done = true;
- snake.process_input(event.key.keysym.sym);
- default:
- break;
- }
- SDL_Delay(20);
- if(frame%2==0){
- snake.process();
- if(snake.dead) done = true;
- SDL_BlitSurface(background, NULL, screen, NULL);
- draw_block(foodx, foody, 0xFF0000FF);
- snake.print();
- SDL_Flip(screen);
- }
- frame++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment