代码重构
这个游戏是这篇博客的进阶版弹力球小程序C语言实现,如果有哪些地方不是很理解可以翻回我的那篇博客看看
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high, width;
int x, y;
int vx, vy;
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};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
high = 15;
width = 20;
x = 0;
y = width / 2;
vx = 1;
vy = 1;
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i < high; i++)
{
for(j = 0; j < width; j++)
{
if((i == x) && (j == y))
printf("o");
else
printf(" ");
}
printf("\n");
}
}
void updateWithoutInput()
{
x = x + vx;
y = y + vy;
if((x == 0) || (x == high - 1))
vx = -vx;
if((y == 0) || (y == width - 1))
vy = -vy;
// sleep(1);
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
显示边框
跳动的小球游戏
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high, width;
int x, y;
int vx, vy;
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};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
high = 15;
width = 20;
x = 0;
y = width / 2;
vx = 1;
vy = 1;
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= high; i++)
{
for(j = 0; j <= width; j++)
{
if((i == x) && (j == y))
printf("o");
else if(j == width)
printf("|");
else if(i == high)
printf("_");
else
printf(" ");
}
printf("\n");
}
}
void updateWithoutInput()
{
x = x + vx;
y = y + vy;
if((x == 0) || (x == high - 1))
vx = -vx;
if((y == 0) || (y == width - 1))
vy = -vy;
// sleep(1);
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
显示移动挡板
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high, width;
int x, y;
int vx, vy;
int px, py;
int ridus;
int left, right;
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};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
high = 15;
width = 20;
x = 0;
y = width / 2;
vx = 1;
vy = 1;
ridus = 5;
px = high;
py = width / 2;
left = py - ridus;
right = py + ridus;
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= high + 1; i++)
{
for(j = 0; j <= width; j++)
{
if((i == x) && (j == y))
printf("o");
else if(j == width)
printf("|");
else if(i == high + 1)
printf("_");
else if((i == high) && (j >= left) && (j <= right))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void updateWithoutInput()
{
x = x + vx;
y = y + vy;
if((x == 0) || (x == high - 1))
vx = -vx;
if((y == 0) || (y == width - 1))
vy = -vy;
// sleep(1);
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a')
{
py--;
left = py - ridus;
right = py + ridus;
}
if(input == 'd')
{
py++;
left = py - ridus;
right = py + ridus;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
消砖块
弹力球消砖块
大体粗略Demo:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high, width;
int x, y;
int vx, vy;
int px, py;
int ridus;
int left, right;
int num; //反弹小球个数
int bx, by;
int score;
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};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
high = 20;
width = 20;
x = 0;
y = width / 2;
vx = 1;
vy = 1;
ridus = 5;
px = high;
py = width / 2;
left = py - ridus;
right = py + ridus;
num = 0;
bx = 0;
by = width / 2 - 1;
score = 0;
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= high + 1; i++)
{
for(j = 0; j <= width; j++)
{
if((i == x) && (j == y))
printf("o");
else if(j == width)
printf("|");
else if(i == high + 1)
printf("_");
else if((i == high) && (j >= left) && (j <= right))
printf("*");
else if((i == bx) && (j == by))
{
printf("###");
j += 2;
}
else
printf(" ");
}
printf("\n");
}
printf("反弹小球数:%d\n", num);
printf("消除的砖块数:%d\n", score);
}
void updateWithoutInput()
{
if(x == high - 1)
{
if((y >= left) && (y <= right))
{
num++;
printf("\a");
if(num % 2)
y = y + rand() * 10 % 6 - 5;
else
{
if(y + rand() * 10 % 6 + 3 < width - 1)
y = y + rand() * 10 % 6 + 3;
}
}
else
{
printf("Lose!!!\n");
system("pause");
exit(0);
}
}
if((x == bx) && (y > by && y <= by + 3))
{
score++;
by = rand() * 10 % width;
}
x = x + vx;
y = y + vy;
if((x == 0) || (x == high - 1))
vx = -vx;
if((y == 0) || (y == width - 1))
vy = -vy;
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a')
{
py--;
left = py - ridus;
right = py + ridus;
}
if(input == 'd')
{
py++;
left = py - ridus;
right = py + ridus;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
因为速度太快了,有点小难,这是我的辣鸡战绩:
终极版弹力球消砖块
添加了挡板上下移动功能
C语言自制竞速消砖块游戏
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int life = 3;
int high, width; //游戏画面大小
int x, y; //小球坐标
int vx, vy; //小球速度
int px, py; //挡板中心坐标
int ridus; //挡板半径大小
int left, right;
int h;
int num; //反弹小球个数
int bx, by; //砖块位置
int score; //得分
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 HideCursor() //治光标闪烁问题
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() //数据初始化
{
high = 20;
width = 20;
h = high;
x = 0;
y = width / 2;
vx = 1;
vy = 1;
ridus = 5;
px = high;
py = width / 2;
left = py - ridus;
right = py + ridus;
num = 0;
bx = 0;
by = width / 2 - 1;
score = 0;
}
void show() //显示画面
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= high + 1; i++)
{
for(j = 0; j <= width; j++)
{
if((i == x) && (j == y))
printf("o");
else if(j == width)
printf("|");
else if(i == high + 1)
printf("_");
else if((i == h) && (j >= left) && (j <= right))
printf("*");
else if((i == bx) && (j == by))
{
printf("###");
j += 2;
}
else
printf(" ");
}
printf("\n");
}
printf("反弹小球数Num:%d\n", num);
printf("消除的砖块数Score:%d\n", score);
printf("你的生命值: %d", life);
if(y == width - 1 && x == h - 1)
y -= 1;
}
void updateWithoutInput()
{
int flag = 0;
if(x == h - 1)
{
if(((y >= left) || (left < 0 && y >= 0)) && ((y <= right) || (right > width && y < width)))
{
if(vx > 0)
{
flag = 1;
num++;
printf("\a");
// int t = 0;
if(num % 2)
{
do{
y = y + rand() * 7 % 3 - 1;
if(y >= width)
y = width - 1;
if(y > right)
y -= 1;
if(y < left)
y += 1;
// if(t >= 1)
// y += 1;
// y -= 1;
// t += 1;
}while(y < left || y > right);
}
else
{
do{
y = y + rand() * 7 % 3 + 1;
if(y >= width)
y = width - 1;
if(y > right)
y -= 1;
if(y < left)
y += 1;
// y += 1;
}while((y < left || y > right));
}
}
}
else if(life == 0)
{
printf("Lose!!!\n");
system("pause");
exit(0);
}
else
{
life--;
}
}
if(x > h + 1)
{
printf("Lose!!!\n");
system("pause");
exit(0);
}
if((x == bx) && (y >= by && y <= by + 3))
{
score++;
by = rand() * 10 % width;
}
x = x + vx;
y = y + vy;
if((x == 0) || (x == h) || (x == high - 1))
vx = -vx;
if(flag)
x--;
if((y == 0) || (y == width))
{
vy = -vy;
}
if(y == 0)
y += 1;
if(y == width)
{
if(x == h - 1)
x -= 1;
y -= 1;
}
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a')
{
py--;
left = py - ridus;
right = py + ridus;
}
if(input == 'd')
{
py++;
left = py - ridus;
right = py + ridus;
}
if(input == 'w')
{
h--;
}
if(input == 's')
{
h++;
if(h == high + 1)
h--;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
// sleep(1);
}
return 0;
}