Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if _DEBUG
- #pragma comment(lib, "alld.lib")
- #else
- #pragma comment(lib, "alleg.lib")
- #endif
- #define MAXMETEOS 32
- #include<allegro.h>
- #include<stdio.h>
- class GameObject
- {
- public: //pozniej ma byc protected
- int _x;
- int _y;
- int dmg;
- char* _image;
- int _dir_x;
- int _dir_y;
- virtual ~GameObject()
- {
- }
- virtual void refresh()=0;
- };
- class meteoryt : public GameObject
- {
- int r_x;
- int r_y;
- int last_tick_time;
- int spddelay;
- int *ig_time;
- public:
- BITMAP *_bmp;
- bool isAlive;
- bool move_tick;
- meteoryt(int x, int y, char* img,int* time_var)
- {
- spddelay=5;
- last_tick_time=0;
- move_tick=true;
- ig_time=time_var;
- isAlive=1;
- dmg=100;
- _x=x;
- _y=y;
- _image=img;
- _bmp=load_bmp(_image,default_palette); //bitmapa meteor dla meteo
- _dir_x=1;
- _dir_y=1;
- r_x=this->_x+400;
- r_y=this->_y+400;
- }
- ~meteoryt()
- {
- destroy_bitmap(_bmp);
- }
- void setTimeVar(int *var_ms)
- {
- ig_time=var_ms;
- }
- void move()
- {
- //ograniczenie ekranu
- if(this->_y>600) this->_y=-120;
- if(this->_y<-120) this->_y=580;
- if(this->_x>800) this->_x=-100;
- if(this->_x<-100) this->_x=800;
- //meteor trajektoria X
- if(move_tick)
- {
- if((this->_x>=100 || this->_x<=r_x) && this->_dir_x==1) ++this->_x;
- if((this->_x>=100 || this->_x<=r_x) && this->_dir_x==-1) --this->_x;
- }
- if(this->_x==100 || this->_x==r_x)
- {
- if(this->_dir_x==1)
- this->_dir_x=-1;
- else
- this->_dir_x=1;
- }
- //meteor trajektoria Y
- if(move_tick)
- {
- if((this->_y>=100 || this->_y<=r_y) && this->_dir_y==1) ++this->_y;
- if((this->_y>=100 || this->_y<=r_y) && this->_dir_y==-1) --this->_y;
- }
- if(this->_y==100 || this->_y==r_y)
- {
- if(this->_dir_y==1)
- this->_dir_y=-1;
- else
- this->_dir_y=1;
- }
- }
- //kolizja z nabojem
- void gotHit(int x, int y, int size_x, int size_y, int &ptr)
- {
- if(x>=this->_x && x<=this->_x+size_x && y>=this->_y && y<=this->_y+size_y)
- {
- this->isAlive=0;
- ptr+=100;
- }
- }
- void refresh()
- {
- move_tick = false;
- if(ig_time==NULL) move_tick=true;
- else if(*ig_time - last_tick_time > spddelay)
- {
- last_tick_time= *ig_time;
- move_tick= true;
- }
- }
- void set_spddelay(int ms)
- {
- spddelay=ms;
- }
- };
- class GameEngine
- {
- private:
- long speed;
- volatile static int GameEngine::msec;
- static bool GameEngine::isPaused;
- public:
- BITMAP* background;
- BITMAP* bufor;
- int g_time;
- void refresh();
- static void errMsg(char *err)
- {
- if (screen != NULL)
- set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
- allegro_message(err);
- }
- class window
- {
- public:
- unsigned int width;
- unsigned int height;
- bool quit;
- bool pause;
- bool menu;
- char* backGround;
- window(unsigned int w, unsigned int h, char* bg)
- {
- width=w;
- height=h;
- bool menu=true;
- bool quit=false;
- bool pause=false;
- backGround=bg;
- }
- }*mainWindow;
- class Time
- {
- static void timer_handler()
- {
- if(!GameEngine::isPaused)
- ++GameEngine::msec;
- }
- END_OF_FUNCTION(timer_handler)
- void init()
- {
- LOCK_VARIABLE(GameEngine::msec);
- LOCK_FUNCTION(Time::timer_handler);
- install_int(Time::timer_handler,1);
- GameEngine::msec=0;
- }
- void deinit()
- {
- remove_int(Time::timer_handler);
- }
- public:
- void pause(){ GameEngine::isPaused=true; }
- void unpause(){ GameEngine::isPaused=false; }
- void reset() { msec=0; }
- int ms() { return msec; }
- double sec() { return ( msec/1000.0f ); }
- double min() { return ( msec/60000.0f ); }
- friend GameEngine;
- }*time;/* time passed since game's started */
- class Sound
- {
- private:
- SAMPLE *sample[33];
- int time_last_play[33];
- int slen[33];
- void init()
- {
- for(int i=0;i<33;++i)
- {
- time_last_play[i]=0;
- sample[i]=NULL;
- }
- }
- void deinit()
- {
- for(int i=0;i<33;++i)
- destroy_sample(sample[i]);
- }
- public:
- void setVolume(unsigned char vol)
- {
- set_volume(vol,vol);
- }
- SAMPLE* load_sound(char *path, int id=32)
- {
- destroy_sample(sample[id]);
- try
- {
- sample[id]=load_sample(path);
- if(sample[id]==NULL)
- {
- char err[128];
- sprintf(err,"nie udalo sie zaladowac dzwieku z pliku: %s",path);
- throw(err);
- }
- }
- catch(char *str)
- {
- errMsg(str);
- }
- slen[id]=1000*(double((sample[id]->len * 8) / sample[id]->bits) / double(sample[id]->freq));
- return sample[id];
- }
- void play_sound(int id,int block_delay_ms=-1,int vol=255,int freq=1000,int pan=127,int loop=0)
- {
- int stime=0;
- if(block_delay_ms>=0)
- stime=(slen[id]+block_delay_ms);
- if(GameEngine::msec-time_last_play[id]>=stime)
- {
- time_last_play[id]=GameEngine::msec;
- play_sample(sample[id],vol,pan,freq,loop);
- }
- }
- void play_sound(char *path,int block_delay_ms=-1,int vol=255,int freq=1000,int pan=127,int loop=0)
- {
- int stime=0;
- if(block_delay_ms>=0)
- if(GameEngine::msec-time_last_play[32]>=stime)
- {
- time_last_play[32]=GameEngine::msec;
- play_sample(load_sound(path),vol,pan,freq,loop);
- }
- }
- friend GameEngine;
- }*sound;
- class Meteos
- {
- private:
- int count;
- meteoryt *meteo[MAXMETEOS];
- BITMAP *bufor;
- GameEngine *ge;
- public:
- Meteos()
- {
- count=0;
- for(int i=0;i<MAXMETEOS;++i)
- meteo[i]=NULL;
- }
- void addMeteo(int x,int y,char *bmp,int *time_var=NULL)
- {
- if(time_var==NULL)
- meteo[count]=new meteoryt(x,y,bmp,&ge->g_time);
- else
- meteo[count]=new meteoryt(x,y,bmp,time_var);
- masked_blit(meteo[count]->_bmp,screen,0,0,meteo[count]->_x,meteo[count]->_y,meteo[count]->_bmp->w,meteo[count]->_bmp->h);
- blit(meteo[count]->_bmp,bufor,0,0,0,0,meteo[count]->_bmp->w,meteo[count]->_bmp->h);
- ++count;
- }
- int getCount()
- {
- return this->count;
- }
- void delMeteo(int id)
- {
- if(count>0)
- {
- id%=count;
- delete meteo[id];
- for(int i=id;i<MAXMETEOS-1;++i)
- {
- meteo[i]=meteo[i+1];
- }
- meteo[MAXMETEOS-1]=NULL;
- --count;
- }
- }
- ~Meteos()
- {
- for(int i=0;i<count;++i)
- delete meteo[i];
- }
- meteoryt* operator[](int i)
- {
- return meteo[i];
- }
- void refreshAll()
- {
- for(int i=0;i<count;++i)
- meteo[i]->refresh();
- }
- friend GameEngine;
- }meteo;
- GameEngine(int w, int h, char *bg) : g_time(0)
- {
- allegro_init();
- install_timer();
- time= new Time;
- time->init();
- sound= new Sound;
- sound->init();
- speed=0;
- install_keyboard();
- install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,"");
- sound->setVolume(255);
- set_color_depth(16);
- mainWindow = new window(800,600,bg);
- set_gfx_mode(GFX_AUTODETECT_WINDOWED, mainWindow->width, mainWindow->height, 0, 0);
- bufor = create_bitmap(800,600);
- meteo.bufor=bufor;
- meteo.ge=this;
- try
- {
- background=load_bmp(mainWindow->backGround, default_palette);
- if(background==NULL)
- throw("nie udalo sie zaladowac tla");
- }
- catch(char *str)
- {
- errMsg(str);
- }
- }
- ~GameEngine()
- {
- destroy_bitmap(bufor);
- destroy_bitmap(background);
- sound->deinit();
- time->deinit();
- delete mainWindow;
- delete time;
- allegro_exit();
- }
- void decSpeed(){--speed;}
- void incSpeed(){++speed;}
- int getSpeed(){return speed;}
- };
- volatile int GameEngine::msec=0;
- bool GameEngine::isPaused = false;
- class ship : public GameObject
- {
- public:
- bool _ammo;
- bool _underAttack;
- ship(int x, int y, char* img)
- {
- _x=x;
- _y=y;
- _image=img;
- _underAttack=0;
- dmg=0;
- _ammo=0;
- _dir_x=0;
- _dir_y=0;
- }
- ~ship()
- {}
- void refresh() // musi byc bo bys mial abstracta skoro gameobject to ma jako abstract ()=0 ;P
- {}
- };
- class bullet
- {
- public:
- int _x;
- int _y;
- char* _img;
- bullet(char* img)
- {
- _x=0;
- _y=0;
- _img=img;
- }
- ~bullet(){}
- void move(int x, int y)
- {
- this->_x=x+32;
- this->_y=y;
- }
- } bullet("images/ship/fire.bmp");
- void GameEngine::refresh()
- {
- g_time= time->ms();
- meteo.refreshAll();
- }
- int main()
- {
- GameEngine *ge= new GameEngine(800,600,"images/menu2.bmp");
- //glowne okno
- /* ladowanie dzwiekow */
- ge->sound->load_sound("sounds/ale_urwal.wav",0);
- ge->sound->load_sound("sounds/ale_to_bylo_dobre.wav",1);
- /* menu */
- int klawisz=0;
- BITMAP* play=load_bmp("images/menu/1.bmp", default_palette);
- BITMAP* play2=load_bmp("images/menu/1b.bmp", default_palette);
- BITMAP* options=load_bmp("images/menu/2.bmp", default_palette);
- BITMAP* options2=load_bmp("images/menu/2b.bmp", default_palette);
- BITMAP* load_game=load_bmp("images/menu/3.bmp", default_palette);
- BITMAP* load_game2=load_bmp("images/menu/3b.bmp", default_palette);
- BITMAP* save_game=load_bmp("images/menu/4.bmp", default_palette);
- BITMAP* save_game2=load_bmp("images/menu/4b.bmp", default_palette);
- BITMAP* read=load_bmp("images/menu/5.bmp", default_palette);
- BITMAP* read2=load_bmp("images/menu/5b.bmp", default_palette);
- BITMAP* quit=load_bmp("images/menu/6.bmp", default_palette);
- BITMAP* quit2=load_bmp("images/menu/6b.bmp", default_palette);
- clear_to_color(screen, makecol(17,16,16));
- //kursor
- install_mouse();
- enable_hardware_cursor();
- select_mouse_cursor(MOUSE_CURSOR_ALLEGRO);
- show_mouse(screen);
- while(ge->mainWindow->menu)
- {
- if(klawisz<2 && key[KEY_UP])
- {
- ++klawisz;
- }
- if(klawisz>0 && key[KEY_DOWN])
- {
- --klawisz;
- }
- if(mouse_y>84 && mouse_y<129)
- {
- if(mouse_b==1)
- {
- ge->mainWindow->menu=0;
- ge->mainWindow->quit=0;
- break;
- }
- blit(play,screen,0,0,250,84,play->w,play->h);
- }
- else
- blit(play2,screen,0,0,250,84,play2->w,play2->h);
- if(mouse_y>145 && mouse_y<190)
- blit(options,screen,0,0,250,145,options->w,options->h);
- else
- blit(options2,screen,0,0,250,145,options2->w,options2->h);
- if(mouse_y>206 && mouse_y<251)
- blit(load_game,screen,0,0,250,206,load_game->w,load_game->h);
- else
- blit(load_game2,screen,0,0,250,206,load_game2->w,load_game2->h);
- if(mouse_y>267 && mouse_y<312)
- blit(save_game,screen,0,0,250,267,save_game->w,save_game->h);
- else
- blit(save_game2,screen,0,0,250,267,save_game2->w,save_game2->h);
- if(mouse_y>328 && mouse_y<373)
- blit(read,screen,0,0,250,328,read->w,read->h);
- else
- blit(read2,screen,0,0,250,328,read2->w,read2->h);
- //wyjscie z gry
- if(mouse_y>389 && mouse_y<441)
- {
- blit(quit,screen,0,0,250,389,quit->w,quit->h);
- if(mouse_b==1)
- {
- ge->mainWindow->menu=0;
- ge->mainWindow->quit=1;
- }
- }
- else
- blit(quit2,screen,0,0,250,389,quit2->w,quit2->h);
- if(key[KEY_ESC]) break;
- }
- show_mouse(NULL);
- /* koniec menu */
- if(!ge->mainWindow->menu)
- {
- //czas
- //float const &time = ge->time->sec(); <- bez sensu nie bedzie zwracac wartosci ;p
- ge->mainWindow->backGround="images/background.bmp";
- ge->background=load_bmp(ge->mainWindow->backGround, default_palette);
- masked_blit(ge->background,screen,0,0,0,0,ge->background->w,ge->background->h);
- //statek
- ship SpaceShip(320,220,"images/ship.bmp");
- BITMAP* ship=load_bmp(SpaceShip._image,default_palette);
- masked_blit(ship,screen,0,0,SpaceShip._x,SpaceShip._y,ship->w,ship->h);
- //ogien
- BITMAP* fire=load_bmp("images/ship/0.bmp",default_palette);
- //crash
- BITMAP* crash=load_bmp("images/ship/crash.bmp",default_palette);
- //bufor
- blit(ge->background,ge->bufor,0,0,0,0,ge->background->w,ge->background->h);
- blit(ship,ge->bufor,0,0,0,0,ship->w,ship->h);
- ge->meteo.addMeteo(100,140,"images/meteo.bmp"); //meteo[0]
- ge->meteo.addMeteo(50,240,"images/meteo.bmp"); //meteo[1]
- ge->meteo.addMeteo(150,440,"images/meteo.bmp"); //meteo[2]
- ge->meteo.addMeteo(250,340,"images/meteo.bmp"); //meteo[3]
- ge->meteo.addMeteo(400,400,"images/meteo.bmp"); //meteo[4]
- //predkosci meteo
- ge->meteo[0]->set_spddelay(5);
- bool move_tick=false;
- int spddelay = 5; // 5ms tick
- int gtime,prevgtime;
- gtime=prevgtime=ge->time->ms();
- float time_a=ge->time->sec();
- while(!ge->mainWindow->quit)
- {
- if(key[KEY_TAB])
- {
- if(ge->mainWindow->pause==0)
- ge->mainWindow->pause=1;
- else
- ge->mainWindow->pause=0;
- }
- if(ge->mainWindow->pause==1)
- {
- readkey();
- ge->mainWindow->pause=0;
- }
- //koniec gry
- if(!ge->meteo[0]->isAlive && !ge->meteo[1]->isAlive && !ge->meteo[2]->isAlive && !ge->meteo[3]->isAlive && !ge->meteo[4]->isAlive)
- {
- readkey();
- }
- ge->refresh(); //odswieza timery, pozycje
- move_tick = false;
- if(ge->g_time-prevgtime> spddelay)
- {
- prevgtime= ge->g_time;
- move_tick= true;
- }
- if(key[KEY_RIGHT] || SpaceShip._dir_x==1)
- {
- if(move_tick) ++SpaceShip._x;
- SpaceShip._dir_x=1;
- }
- if(key[KEY_LEFT] || SpaceShip._dir_x==-1)
- {
- if(move_tick) --SpaceShip._x;
- SpaceShip._dir_x=-1;
- }
- if(key[KEY_DOWN] || SpaceShip._dir_y==-1)
- {
- if(move_tick) ++SpaceShip._y;
- if(key[KEY_DOWN] && SpaceShip._dir_y==-1)
- {
- SpaceShip._dir_y=-1;
- SpaceShip._dir_x=0;
- }
- else
- SpaceShip._dir_y=-1;
- }
- if(key[KEY_UP] || SpaceShip._dir_y==1)
- {
- if(move_tick) --SpaceShip._y;
- if(key[KEY_UP] && SpaceShip._dir_y==1)
- {
- SpaceShip._dir_y=1;
- SpaceShip._dir_x=0;
- }
- else
- SpaceShip._dir_y=1;
- }
- //ograniczenia ekranu
- if(SpaceShip._y>600) SpaceShip._y=-120;
- if(SpaceShip._y<-120) SpaceShip._y=580;
- if(SpaceShip._x>800) SpaceShip._x=-100;
- if(SpaceShip._x<-100) SpaceShip._x=800;
- if(!ge->mainWindow->menu)
- {
- ge->meteo[0]->move();
- ge->meteo[1]->move();
- ge->meteo[2]->move();
- ge->meteo[3]->move();
- ge->meteo[4]->move();
- }
- blit(ge->background, ge->bufor,0,0,0,0,ge->background->w,ge->background->h);
- if(ge->meteo[0]->isAlive==1)
- {
- masked_blit(ge->meteo[0]->_bmp,ge->bufor,0,0,ge->meteo[0]->_x,ge->meteo[0]->_y,ge->meteo[0]->_bmp->w,ge->meteo[0]->_bmp->h);
- }
- if(ge->meteo[1]->isAlive==1)
- {
- masked_blit(ge->meteo[1]->_bmp,ge->bufor,0,0,ge->meteo[1]->_x,ge->meteo[1]->_y,ge->meteo[1]->_bmp->w,ge->meteo[1]->_bmp->h);
- }
- if(ge->meteo[2]->isAlive==1)
- {
- masked_blit(ge->meteo[2]->_bmp,ge->bufor,0,0,ge->meteo[2]->_x,ge->meteo[2]->_y,ge->meteo[2]->_bmp->w,ge->meteo[2]->_bmp->h);
- }
- if(ge->meteo[3]->isAlive==1)
- {
- masked_blit(ge->meteo[3]->_bmp,ge->bufor,0,0,ge->meteo[3]->_x,ge->meteo[3]->_y,ge->meteo[3]->_bmp->w,ge->meteo[3]->_bmp->h);
- }
- if(ge->meteo[4]->isAlive==1)
- {
- masked_blit(ge->meteo[4]->_bmp,ge->bufor,0,0,ge->meteo[4]->_x,ge->meteo[4]->_y,ge->meteo[4]->_bmp->w,ge->meteo[4]->_bmp->h);
- }
- if(key[KEY_UP])
- masked_blit(fire, ge->bufor,0,0,SpaceShip._x,SpaceShip._y,ship->w,ship->h);
- else
- masked_blit(ship, ge->bufor,0,0,SpaceShip._x,SpaceShip._y,ship->w,ship->h);
- BITMAP* b=load_bmp(bullet._img,default_palette);
- //strzal
- if(SpaceShip._ammo==1)
- {
- masked_blit(b, ge->bufor,0,0,bullet._x,bullet._y,b->w,b->h);
- --bullet._y;
- }
- if(bullet._y<0)
- {
- SpaceShip._ammo=0;
- bullet._x=0;
- bullet._y=0;
- }
- //detekcja kolizji z nabojem
- if(SpaceShip._ammo==1)
- {
- ge->meteo[0]->gotHit(bullet._x,bullet._y,77,72,SpaceShip.dmg);
- ge->meteo[1]->gotHit(bullet._x,bullet._y,77,72,SpaceShip.dmg);
- ge->meteo[2]->gotHit(bullet._x,bullet._y,77,72,SpaceShip.dmg);
- ge->meteo[3]->gotHit(bullet._x,bullet._y,77,72,SpaceShip.dmg);
- ge->meteo[4]->gotHit(bullet._x,bullet._y,77,72,SpaceShip.dmg);
- }
- if(key[KEY_SPACE] && SpaceShip._ammo==0)
- {
- SpaceShip._ammo=1;
- bullet.move(SpaceShip._x+32,SpaceShip._y);
- }
- //zderzenie
- for(int i=0; i<ge->meteo.getCount(); ++i)
- {
- if(ge->time->sec()-time_a>2.0)
- SpaceShip._underAttack=0;
- if(ge->meteo[i]->isAlive==1 && ge->meteo[i]->_y>=SpaceShip._y && ge->meteo[i]->_y<=SpaceShip._y+80 && ge->meteo[i]->_x>=SpaceShip._x && ge->meteo[i]->_x<=SpaceShip._x+76
- || ge->meteo[i]->isAlive==1 && SpaceShip._y>=ge->meteo[i]->_y && SpaceShip._y<=ge->meteo[i]->_y+72 && SpaceShip._x>=ge->meteo[i]->_x && SpaceShip._x<=ge->meteo[i]->_x+77)
- {
- ge->sound->play_sound(0,500);
- if(SpaceShip._underAttack==0)
- {
- SpaceShip.dmg-=100;
- SpaceShip._underAttack=1;
- time_a=ge->time->sec();
- }
- masked_blit(crash, ge->bufor,0,0,SpaceShip._x,SpaceShip._y,ship->w,ship->h);
- SpaceShip._dir_x=ge->meteo[i]->_dir_x;
- SpaceShip._dir_y=ge->meteo[i]->_dir_y;
- }
- else
- {
- masked_blit(ship, ge->bufor,0,0,SpaceShip._x,SpaceShip._y,ship->w,ship->h);
- }
- }
- textprintf(ge->bufor,font,20,20,makecol(255,255,128),"Punkty zycia : %d",SpaceShip.dmg);
- blit(ge->bufor, screen, 0,0,0,0, 800,600);
- //wyjscie z gry
- if(key[KEY_ESC])
- {
- ge->mainWindow->quit=true;
- break;
- }
- }
- destroy_bitmap(ship);
- }
- return 0;
- }
- END_OF_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement