查看完整版本: 火柴人移動小程式
頁: [1]

franky051173 發表於 2011-9-3 10:27 PM

火柴人移動小程式

小弟近來迷上遊戲的製做,由於是第一次動手做,先嘗試做個火柴人移動的程式,完成時那真是一個感動阿~~~盡管還是稱不上遊戲,但還是想與大家分享一下 程式碼如下注意要使用allegro遊戲引擎才能順利編譯喔 //mark 1 主要新增對時鐘的類別 #define ALLEGRO_NO_MAGIC_MAIN
#include <allegro.h>
#include <iostream>
int ticks ;
int cursor_x=0,cursor_y=0;
int rcursor_x=0,rcursor_y=0;
int click=0 ;
int rclick=0 ;
int a=0 ;void timer() {
++ticks ;
}
END_OF_FUNCTION(timer);//**************************************************************
void getMouseInfo(){     if(mouse_b & 1){                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
                  //BITMAP* bmp3 = load_bitmap("x2.bmp",NULL);
                  click = 1;
     }
      else
     click =0;
     if(mouse_b & 2){                  rcursor_x = mouse_x;
                  rcursor_y = mouse_y;
                  rclick =1 ;
                  
                  
                  //BITMAP* bmp3 = load_bitmap("x2.bmp",NULL);     }
     else
     rclick =0;
     //act2 =0 ;
}
//************************************************************
class otimer //定義物件
{
  private :
      
  public :
        
                     
  int myset(int newtick) ;        // 設定ticks
  void timer() ;
  
};   
int otimer::myset(int newtick)
{
   ticks = newtick ;  
}  
//************************************************************
class obj
{
public :
  int v ;
  
  int manx ;
  int many ;
  int d ;
  int no  ;
  int t ;
  int bmpx,bmpy;
  void set(int newx,int newy,int newv);
  void setcon(int newd, int newno, int newt);
  BITMAP* bmp ;
  void move() ;
  void load(int sizex, int sizey, int p1 ,int p2, int p3) ;
        };void obj::set(int newx,int newy,int newv)
{
manx = newx ;
many = newy ;
v = newv ;
}     
void obj::setcon(int newd,int newno,int newt)
{
d = newd ;
no = newno ;
t = newt ;
}     
void obj::move()
{
  if(cursor_y==many && cursor_x==manx)
{
d=0 ;
}
if(cursor_x > manx)
{
   d =1 ;//1表示向右 ,2表示向左. 0表示不動           
if(manx + v > cursor_x)
manx = cursor_x ;
else                  
manx+=v;

}
  if(cursor_x < manx)
{
if(manx - v < cursor_x)
manx = cursor_x ;
else                  
manx-=v;
d =2 ;//1表示向右 ,2表示向左. 0表示不動
}
if(cursor_x==manx)
  manx +=0  ;

if(cursor_y > many)
{
if(many + v > cursor_y)
many = cursor_y ;
else                  
many+=v;
//d =1 ;//1表示向右 ,2表示向左. 0表示不動
}
  if(cursor_y < many)
{
if(many - v < cursor_y)
many = cursor_y ;
else                  
many-=v;
//d =2 ;//1表示向右 ,2表示向左. 0表示不動
}
if(cursor_y==many)
  many +=0  ;

}
void obj::load(int sizex, int sizey, int p1 ,int p2, int p3)
{
   
if(d==0)
{
   bmpy=sizey*0 ;
   bmpx=sizex*t ;
   ++t ;
   if(t>(p1-1))
   t=0;
}
if(d==1)
{
   bmpy=sizey*2 ;
   bmpx=sizex*t ;
   ++t ;
   if(t>(p2-1))
   t=0;     
}
if(d==2)
{
  bmpy=sizey*1 ;
  bmpx=sizex*t ;
   ++t ;
   if(t>(p3-1))
   t=0;      
}}        //*************************************************************************int main() {
//初始化還境   
allegro_init();
install_mouse();otimer t1 ;
t1.myset(0) ;
obj man;
obj man2 ;
man.set(0,0,10) ;
man.setcon(0,2,0);
man2.set(100,200,5) ;
man2.setcon(0,2,0);
install_timer();
LOCK_VARIABLE(ticks);
LOCK_FUNCTION(timer);
install_int_ex( timer,BPS_TO_TIMER(1000));set_color_depth(32);
set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800,600, 0,0 );install_keyboard();
int secs = 0;
BITMAP* map = load_bitmap("map.bmp",NULL);
//if(mouse_b & 1)
BITMAP* arrow = load_bitmap("x.bmp",NULL);
BITMAP* arrow2 = load_bitmap("x2.bmp",NULL);
BITMAP* mouse ;man.bmp = load_bitmap("woman.bmp",NULL);
man2.bmp = load_bitmap("man2.bmp",NULL);
BITMAP* buffer ;
buffer = create_bitmap( 800,600); //************************************************************
while(!key)
{
  if(ticks>=100) //等待0.1秒種後進行處理
  {
                 
    getMouseInfo() ;   
    if(click==1)
     {
      mouse = arrow2 ;
      }
     else
     mouse = arrow ;         
    man.move();
    man.load(32,50,1,4,4)  ;
    man2.move();
    man2.load(50,50,1,2,2)  ;
    std::cout<<"x:"<<cursor_x<<std::endl;
std::cout<<"y:"<<cursor_y<<std::endl;
std::cout<<"manx:"<<man.manx<<std::endl;
std::cout<<"many:"<<man.many<<std::endl;
std::cout<<"direction:"<<man.d<<std::endl;   
    ticks=0;
  }
//畫面顯示
acquire_screen(); masked_blit(map,buffer, 0,0 ,0,0,800 ,600);
masked_blit(man.bmp,buffer, man.bmpx,man.bmpy ,man.manx,man.many,32 ,50);
masked_blit(man2.bmp,buffer, man2.bmpx,man2.bmpy ,man2.manx,man2.many,50 ,50);
masked_blit(mouse,buffer, 0,0 ,mouse_x,mouse_y,50 ,50);
blit(buffer,screen, 0,0 ,0,0,800 ,600);release_screen();
}
return 0;
}...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

lbj82321 發表於 2011-9-9 12:21 PM

呵呵呵  謝謝大大的提供。快來下載玩玩看囉。

sakuxz 發表於 2011-9-25 08:44 PM

您是看神麼書   或是去哪裡學的
c++寫遊戲好難
頁: [1]