C语言——反弹球游戏(第一阶段
#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #define High 15 //游戏画面尺寸 #define Width 20 //全局变量 int ball_x,ball_y; //小球的坐标 int ball_vx,ball_vy; //小球的速度 int canvas[High][Width] = {0}; //二维数组存储游戏画布中的对应元素,0为空格,1为小球 void gotoxy(int x, int y) //将光标移到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup() //数据的初始化 { ball_x = 0; ball_y = 0; ball_vx = 1; ball_vy = 1; canvas[ball_x][ball_y] = 1; } 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"); } printf("|\n"); } for(j = 0; j < Width; j++) printf("-"); } void updateWithoutInput() //与用户输入无关的更新 { 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() //与用户输入有关的更新 { } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; }