C语言——反弹球游戏(第二阶段
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C语言——反弹球游戏(第二阶段
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            #include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>#define High 15         //游戲畫面尺寸
#define Width 20//全局變量
int ball_x,ball_y;      //小球的坐標(biāo)
int ball_vx,ball_vy;    //小球的速度
int canvas[High][Width] = {0};  //二維數(shù)組存儲(chǔ)游戲畫布中的對(duì)應(yīng)元素,0為空格,1為小球
int position_x,position_y;
int ridus;
int left,right;void gotoxy(int x, int y)       //將光標(biāo)移到(x,y)位置
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos);
}void startup()          //數(shù)據(jù)的初始化
{ball_x = 0;ball_y = Width/2;ball_vx = 1;ball_vy = 1;canvas[ball_x][ball_y] = 1;ridus = 5;position_x = High-1;position_y = Width/2;left = position_y-ridus;right = position_y+ridus;int k;for(k = left; k <= right; k++)canvas[position_x][k] = 2;
}void show()         //顯示畫面
{gotoxy(0,0);int i,j;for(i = 0; i < High; i++){for(j = 0; j < Width; j++){if(canvas[i][j] == 0)printf(" ");else if(canvas[i][j] == 1)printf("0");else if(canvas[i][j] == 2)printf("*");}printf("|\n");}for(j = 0; j < Width; j++)printf("-");
}void updateWithoutInput()       //與用戶輸入無關(guān)的更新
{if(ball_x == High-2){if((ball_y >= left) && (ball_y <= right))printf("\a");else{printf("游戲失敗\n");system("pause");exit(0);}}canvas[ball_x][ball_y] = 0;ball_x = ball_x+ball_vx;ball_y = ball_y+ball_vy;if((ball_x == 0) || (ball_x == High-1))ball_vx = -ball_vx;if((ball_y == 0) || (ball_y == Width-1))ball_vy = -ball_vy;canvas[ball_x][ball_y] = 1;Sleep(50);
}void updateWithInput()      //與用戶輸入有關(guān)的更新
{char input;if(kbhit()){input = getch();if(input == 'a' && left > 0){canvas[position_x][right] = 0;position_y--;left = position_y-ridus;right = position_y+ridus;canvas[position_x][left] = 2;}if(input == 'd' && right < Width-1){canvas[position_x][left] = 0;position_y++;left = position_y-ridus;right = position_y+ridus;canvas[position_x][right] = 2;}}
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();}return 0;
}  
                        
                        
                        ?
轉(zhuǎn)載于:https://www.cnblogs.com/JAYPARK/p/10285381.html
總結(jié)
以上是生活随笔為你收集整理的C语言——反弹球游戏(第二阶段的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 配置guzz_app.propertie
 - 下一篇: Java String:重要到别人只能当