c++乒乓球遊戲問題 |
尚未結案
|
x00054
一般會員 發表:2 回覆:0 積分:0 註冊:2009-01-11 發送簡訊給我 |
這是我做的乒乓球遊戲,要如何把左右左右移動的棒子改成上下移動的棒子呢?還有球的移動方向從左右改成上下呢?,謝謝
#include #include #define DOWN_RIGHT 0 #define UP_RIGHT 1 #define DOWN_LEFT 2 #define UP_LEFT 3 void moveBall(void); void respondToKeyboard(void); void reverseVerticalDirection(void); void reverseHorizontalDirection(void); int ball_x; int ball_y; int barL_y; int barR_y; int scoreL; int scoreR; int W,H; int direction; BITMAP *ball; BITMAP *bar; BITMAP *buffer; FONT *pongFont; void delay (int a) { while (a > 0 ) {a = a -1; } /* printf( "a= %d", a ); */ } int main(void) { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, 640, 480, 0 ,0 ); ball_x=SCREEN_W/2; ball_y=SCREEN_H/2; barL_y=SCREEN_H/2; barR_y=SCREEN_H/2; ball=load_bitmap("ball.bmp", NULL); bar= load_bitmap("bar.bmp",NULL); buffer = create_bitmap( SCREEN_W,SCREEN_H ); pongFont = load_font("pongfont.pcx", NULL, NULL); srand(time(NULL)); direction = rand() % 4; while(!key[KEY_ESC]) { moveBall(); respondToKeyboard(); clear_to_color(buffer, makecol(255,255,255)); blit(bar,buffer, 0, 0, 20,barL_y, bar->w, bar->h); blit(bar,buffer, 0, 0, 600,barR_y, bar->w, bar->h); // readkey(); textprintf_ex(buffer, pongFont, 75, 20, makecol( 0, 0, 0),-1, "Left Player Score: %d",scoreL ); textprintf_ex(buffer, pongFont, 400, 20, makecol( 0, 0, 0),-1, "Right Player Score: %d",scoreR ); blit(ball,buffer, 0, 0, ball_x, ball_y, ball->w, ball->h); blit(buffer,screen,0,0,0,0,buffer->w,buffer->h); clear_bitmap(buffer); W=ball->w; H=ball->h; delay(1000000); // readkey(); } destroy_bitmap(ball); destroy_bitmap(bar); destroy_bitmap(buffer); destroy_font(pongFont); return 0; } END_OF_MAIN(); void moveBall() { if (ball_y <=0 || ball_y >480-H ) reverseVerticalDirection(); if(ball_x <= 0 || ball_x >= 640-W) reverseHorizontalDirection(); switch (direction){ case DOWN_RIGHT: ball_x; ball_y; break; case UP_RIGHT: ball_x; --ball_y; break; --ball_y; case DOWN_LEFT: --ball_x; ball_y; break; case UP_LEFT: --ball_x; --ball_y; break; } if(ball_x <40 && (direction == DOWN_LEFT || direction == UP_LEFT)) { if(ball_y >(barL_y - 39)&& ball_y <(barL_y 99)) {reverseHorizontalDirection(); scoreL;} else if (ball_x <= 10){ scoreR; ball_x = SCREEN_W / 2; ball_y = SCREEN_H / 2; direction = rand() % 4; } } if(ball_x >560 && (direction == DOWN_RIGHT || direction == UP_RIGHT)) { if(ball_y >(barR_y - 39)&& ball_y <(barR_y 99)) {reverseHorizontalDirection(); scoreR;} else if (ball_x >= 600){ scoreL; ball_x = SCREEN_W / 2; ball_y = SCREEN_H / 2; direction = rand() % 4; } } } void respondToKeyboard() { if (key [KEY_A]) barL_y -= 3; if (key [KEY_Z]) barL_y = 3; if (key [KEY_UP]) barR_y -= 3; if (key [KEY_DOWN]) barR_y = 3; if (barL_y < 30) barL_y = 30; else if (barL_y > 380) barL_y = 380; if (barR_y < 30) barR_y = 30; if (barR_y > 380) barR_y = 380; } void reverseVerticalDirection() { if((direction % 2) == 0) { direction; printf("direction=%d,",direction);} else --direction; } void reverseHorizontalDirection() {direction =(direction 2)% 4; } 編輯記錄
taishyang 重新編輯於 2009-01-12 10:09:53, 註解 移除[急需]兩字‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |