欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

反弹球消砖块C语言重构函数封装

程序员文章站 2024-03-18 17:15:22
...

代码重构

这个游戏是这篇博客的进阶版弹力球小程序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;
}

显示移动挡板

反弹球消砖块C语言重构函数封装
反弹球消砖块C语言重构函数封装
反弹球消砖块C语言重构函数封装

#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语言重构函数封装

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!