[ create a new paste ] login | about

Link: http://codepad.org/mRtRpEle    [ raw code | fork ]

C++, pasted on Jun 4:
#include <ctime>
#include <allegro.h>

BITMAP *bmapple = NULL;
BITMAP *bmsnake = NULL;
    
class pos { 
      public:
             int x;
             int y;
             pos () {x = 0; y = 0;} 
};

class snake {
      public:
             pos main[2500];
             int length;
             void eat(pos p) { 
                  length++;
                  for(int i=(length-1); i>0; i--) main[i] = main[i-1];
                  main[0]=p;
             }
             void move(pos p) {
                  for(int i=(length-1); i>0; i--) main[i] = main[i-1];
                  main[0] = p;
             }
             bool isHere(pos p) {
                  for(int i=0; i<length; i++) {
                          if(main[i].x==p.x && main[i].y==p.y) return true;
                  }
                  return false;
             }   
};

class board {
      public:
             char main[25][100];
             void make(snake s, pos apple) {
                  for(int i=0; i<25; i++) {
                             for(int j=0; j<100; j++) main[i][j] = 0;
                  }
                  for(int i=0; i<s.length; i++) main[s.main[i].x][s.main[i].y] = 2;
                  main[apple.x][apple.y] = 1;
             }
};

pos randomApplePosition(snake s) {
    pos p;
    while(true) {
                srand(static_cast<int>(time(NULL)));
                p.x = rand()%25;
                p.y = rand()%100;
                if(!s.isHere(p)) return p;
    }
}

void draw(board b) {
     clear_to_color(screen, makecol(112,112,112));
     for(int i=0; i<25; i++) {
             for(int j=0; j<100; j++) {
                     if(b.main[i][j]==2) blit(bmsnake, screen, 0, 0, j*11, i*11, bmsnake->w, bmsnake->h);
                     else if(b.main[i][j]==1) blit(bmapple, screen, 0, 0, j*11, i*11, bmapple->w, bmapple->h);
             }
     }
}

int main()
{
    allegro_init();
    install_keyboard();
    set_color_depth(8);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1099, 274,0,0);
    bmapple = create_bitmap(10, 10);
    bmsnake = create_bitmap(10, 10);
    clear_to_color(bmapple, makecol(255, 0, 0));
    clear_to_color(bmsnake, makecol(0, 0, 0));
    board b;
    for(int i=0; i<38; i++) {
            for(int j=0; j<78; j++) b.main[i][j] = 0; //czyścimy naszą początkową planszę
    }
    snake s;
    s.length = 1;
    pos head;
    pos apple = randomApplePosition(s);
    b.main[apple.x][apple.y] = 1;
    int dir = 0;
    int score = 0;
    b.make(s, apple);
    draw(b);
    while(true) {
                if(key[KEY_UP]) {if(dir==KEY_DOWN) continue; dir=key[KEY_UP]; if(head.x!=0) head.x--;}
                if(key[KEY_LEFT]) {if(dir==KEY_RIGHT) continue; dir=key[KEY_LEFT]; if(head.y!=0) head.y--;}
                if(key[KEY_RIGHT]) {if(dir==KEY_LEFT) continue; dir=key[KEY_RIGHT]; if(head.y!=77) head.y++;}
                if(key[KEY_DOWN]) {if(dir==KEY_UP) continue; dir=key[KEY_DOWN]; if(head.x!=37) head.x++;}
                switch(b.main[head.x][head.y]) {
                                               case 0: s.move(head); b.make(s, apple); draw(b); break;
                                               case 1: score++; s.eat(head); apple = randomApplePosition(s); b.make(s, apple); draw(b); break;
                                               case 2: rest(3000); readkey(); return 0;
                }
    }
    allegro_exit();
    return 0;
}
END_OF_MAIN();


Create a new paste based on this one


Comments: