3D Game ProgrammingLab2-2D game example3D Game ProgrammingLab2-2D game example
GoalGoal
Familiar with GLUT game processLoad imagesI/O controlAnimation
Image examplesImage examples
stand walk jump sun1 sun2
flyup flydown cat1 cat2
Read ImageRead Image
RGBApixmap pic;pic.readBMPFile(“filename.bmp");
Draw imageDraw image
In display()
glRasterPos2i(x, y);pic.blend();
(x,y)(0,0)
(w,h)
Change StateChange State
Stand Walk
Jump
RGBApixmap pic[3];
Pic[0].readBMPFile(“stand.bmp"); Pic[1].readBMPFile(“walk.bmp");
Pic[2].readBMPFile(“walk.bmp");
Change StateChange Statevoid SpecialKeys(int key, int x, int y){
switch(key) {case GLUT_KEY_LEFT:
picX -= 5;if (whichPic==0)
whichPic=1;else
whichPic=0;
DirectState=1;break;
case GLUT_KEY_RIGHT:…
}}
* glutSpecialFunc Description
void display() {
// 改變圖片位置glRasterPos2i(PicX, PicY);
// 藉由縮放讓圖片改變方向if(DirectState==0) {
glPixelZoom(-1.0, 1.0);
// 切換不同狀態pic[whichPic].blend();
}
設定透明色去除圖片背景設定透明色去除圖片背景cout<<"Reading sprite";
// 匯入角色圖片pic[0].readBMPFile("pokemon/stand.bmp"); cout<<'.'; pic[1].readBMPFile("pokemon/walk.bmp"); cout<<'.';pic[2].readBMPFile("pokemon/jump.bmp"); cout<<'.'<<endl;
// 選取要去掉的背景顏色 (232, 248, 248)for (int i=0; i<3; i++) pic[i].setChromaKey(232, 248, 248);
// 顏色 (232, 248, 248) 為左圖 箭頭指向的淡藍色背景
去被圖製作範例去被圖製作範例首先找一張將背景透明的圖片 ( 檔案格式為 .gif
或 .png ) ,接著用 Photoshop 等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色 (232, 248, 248) 舉例:
Time FunctionTime Functionvoid glutTimerFunc(unsigned int msecs, void (*func)(int value), int value);
時間間隔 (milliseconds)
要呼叫的函式名稱
要傳入函式的變數
Time FunctionTime Functionvoid jump(int i){ whichPic=2;
if (i<15) picY+=14; else picY-=14; if(i<30) {
i++;glutTimerFunc( 50, jump, i);
}else {whichPic=0;jumpState=0;picY=150;
} glutPostRedisplay();}
glutTimerFunc( A, B, C );(1)A = 時間間隔 (milliseconds)
(2)B = 要呼叫的Function(3)C = 要傳入的變數GLUT 每隔 A msecs 就會呼叫一次 B(C) 函式
以 jump function 為例:glutTimerFunc(50,jump,i); 表示每隔 50 單位時間,呼叫一次 jump (i)
PracticePractice
Back and Forth
n steps
ChallengeChallenge
Fly
W
H y = H – c(x - W)2
Super ChallengeSuper Challenge
Fly Randomly
sin(x) or cos(x)
+ rand() % maxChange
Top Related