2.2 用函数实现反弹球消砖块
作者:互联网
2.2.1 代码重构
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
//全局变量
int high, width;
int ball_x, ball_y;
int ball_vx, ball_vy;
//数据初始化
void startup() {
high = 15;
width = 20;
ball_x = 0;
ball_y = width / 2;
ball_vx = 1;
ball_vy = 1;
}
//清屏函数
void gotoxy(int x, int y) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
//消除光标
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//显示函数
void show() {
gotoxy(0, 0);
int i, j;
for (int i = 0; i < high; i++) {
for (int j = 0; j < width; j++) {
if ((i == ball_x) && (j == ball_y))
printf("O");
else
printf(" ");
}
printf("\n");
}
}
//与用户输入无关的更新
void updateWithoutInput() {
ball_x += ball_vx;
ball_y += ball_vy;
if ((ball_x == 0) || (ball_x == high - 1))
ball_vx *= -1;
if ((ball_y == 0) || (ball_y == width - 1))
ball_vy *= -1;
Sleep(50);
}
//与用户输入有关的更新
void updateWithInput() {
}
//主函数
int main() {
HideCursor();
startup();
while (true) {
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
标签:ball,int,void,pos,high,width,砖块,2.2,反弹球 来源: https://blog.csdn.net/weixin_51671868/article/details/120338598