game

#include <cstdio> #include <cstdlib> #define GLUT_DISABLE_ATEXIT_HACK #include <GL\glut.h> #include <windows.h> float ball_x, ball_y; float ball_vx, ball_vy; float bar_x, bar_y; float bar_vx, bar_vy; float bar_width, bar_height; float block_x[100], block_y[100]; float block_width, block_height; int block_cnt; bool trigger_left_key, trigger_right_key; int game_state; const int GAME_STATE_PLAY = 0; const int GAME_STATE_END = 1; /* 判斷是否是在某個長方形裡面 * 參數: * - x :測試x座標 * - y :測試y座標 * - w :長方形寬度 * - h :長方形高度 * - cx:長方形中心x座標 * - cy:長方形中心y座標 */ bool IsInBox(float x, float y, float w, float h, float cx, float cy){ if((cx-h/2 <= x) && (x <= cx+h/2) && (cy-w/2 <= y) && (y <= cy+w/2)){ return true; } else{ return false; } } /* 判斷座標是否為空 * 參數: * - x:測試x座標 * - y:測試y座標 */ bool IsLegalPoint(float x, float y){ if(x>1 || x<-1){ return false; } if(y>1){ return false; } // if(IsInBox(x, y, bar_width, bar_height, bar_x, bar_y)==true){ // return false; // } for(int n=0;n<block_cnt;n++){ if(IsInBox(x, y, block_width, block_height, block_x[n], block_y[n])==true){ return false; } } return true ; } /* 消除被撞到的磚塊 * 參數: * - x:x座標 * - y:y座標 */ void BlockDelete(float x, float y){ for(int n=0;n<block_cnt;n++){ if(IsInBox(x, y, block_width, block_height, block_x[n], block_y[n])==true){ block_x[n]=30 ; block_y[n]=30 ; // block_cnt-- ; } } } void SystemTimer(int value){ if(game_state == GAME_STATE_PLAY){ ball_x += ball_vx; ball_y += ball_vy; if(IsLegalPoint(ball_x + ball_vx, ball_y) == false){ BlockDelete(ball_x + ball_vx, ball_y); ball_vx = -ball_vx; } if(IsLegalPoint(ball_x, ball_y + ball_vy) == false){ BlockDelete(ball_x, ball_y + ball_vy); ball_vy = -ball_vy; } // if(IsInBox(ball_x, ball_y, bar_width, bar_height, bar_x, bar_y)==true){ // ball_vx = -ball_vx; // } if(IsInBox(ball_x, ball_y, bar_width, bar_height, bar_x, bar_y)==true){ ball_vy = -ball_vy; ball_vx = -ball_vx; } if(trigger_left_key) bar_x -= bar_vx; if(trigger_right_key) bar_x += bar_vx; if(ball_y < -1) game_state = GAME_STATE_END; }else if(game_state == GAME_STATE_END){ //doing nothing } glutPostRedisplay(); glutTimerFunc(25, SystemTimer, 1); return; } void DrawRectangle(float midx, float midy, float width, float height, float r, float g, float b){ glBegin(GL_QUADS); glColor3f(r, g, b); glVertex3f(midx - width/2, midy - height/2, 0.0); glVertex3f(midx + width/2, midy - height/2, 0.0); glVertex3f(midx + width/2, midy + height/2, 0.0); glVertex3f(midx - width/2, midy + height/2, 0.0); glEnd(); return; } void Display(){ glClear(GL_COLOR_BUFFER_BIT); if(game_state == GAME_STATE_PLAY){ float wid = 0.05; DrawRectangle(ball_x, ball_y, wid, wid, 0.5, 0.5, 0.5); DrawRectangle(bar_x, bar_y, bar_width, bar_height, 0.5, 0.7, 0.7); for(int lx = 0;lx < block_cnt;lx++) DrawRectangle(block_x[lx], block_y[lx], block_width, block_height, 0.5, 0.3, 0.3); }else if(game_state == GAME_STATE_END){ // print GAME_END glColor3f(1,1,1); float x = -0.2, y = 0; char str[] = "GAME OVER"; glRasterPos2f((GLfloat)x,(GLfloat)y); for(int c = 0; str[c] != 0; c++) glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[c]); } glFlush(); return; } void Init(){ bar_x = 0, bar_y = -0.5; bar_vx = 0.05, bar_vy = 0; bar_width = 0.3, bar_height = 0.015; ball_vx = 0.01, ball_vy = 0.012; ball_x = 0, ball_y = 0; trigger_left_key = false, trigger_right_key = false; block_width = 0.18, block_height = 0.09; block_cnt = 24; for(int lx = 0;lx < 4;lx++) for(int ly = 0;ly < 6;ly++) block_x[lx*6 + ly] = -0.5 + ly*0.2 , block_y[lx*6 + ly] = 0.5 - lx*0.1; game_state = GAME_STATE_PLAY; return; } void SpecialKeyDown(int key, int x, int y){ if(key == GLUT_KEY_LEFT) trigger_left_key = true; if(key == GLUT_KEY_RIGHT) trigger_right_key = true; glutPostRedisplay(); return; } void SpecialKeyUp(int key, int x, int y){ if(key == GLUT_KEY_LEFT) trigger_left_key = false; if(key == GLUT_KEY_RIGHT) trigger_right_key = false; glutPostRedisplay(); return; } void NormalKeyDown(unsigned char c, int x, int y){ glutPostRedisplay(); return; } void NormalKeyUp(unsigned char c, int x, int y){ glutPostRedisplay(); return; } int main(int argc, char* argv[]){ Init(); glutInit(&argc, argv); glutInitWindowSize(600, 600); glutCreateWindow("Blockhit"); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA); glutDisplayFunc(Display); glutTimerFunc(25, SystemTimer, 1); glutKeyboardFunc(NormalKeyDown); glutKeyboardUpFunc(NormalKeyUp); glutSpecialFunc(SpecialKeyDown); glutSpecialUpFunc(SpecialKeyUp); glEnable(GL_BLEND); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glutMainLoop(); return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.