C语言 打砖块3.0 黑窗口版本

C语言 打砖块3.0 黑窗口版本,第1张

C语言 打砖块3.0 黑窗口版本
#include
#include
#include
#include
//定义
#define WIDTH  40
#define HEIGHT 20

#define SPACE 0
#define VEDGE 1
#define HEDGE 2
#define BALL  3
#define BAFFLE 4
#define BRICK 5

#define RIGHT 1
#define DOWN  1
#define UP   -1
#define LEFT -1


int area_height;//游戏区域  0-n-1
int area_width;
int ball_col, ball_row;//小球位置
int ball_vv, ball_vh;//小球速度
int baffle_col, baffle_row;//挡板位置
int baffle_size;//挡板尺寸

int brick_col, brick_row;//
int canvas[HEIGHT+1][WIDTH+1] = { 0 };//画布,游戏区域
bool pause;

int score;//积分
bool isLose;//是否已经失败
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()
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(handle, &cursor_info);
}
//初始化
void startup()
{
	area_height = HEIGHT;
	area_width = WIDTH;
	ball_row = area_height / 2;
	ball_col = area_width / 2;
	ball_vv = DOWN;
	ball_vh = RIGHT;

	baffle_col = area_width / 2;
	baffle_row = area_height - 1;
	baffle_size = 5;
	brick_col = rand() % area_width;
	brick_row = 0;

	score = 0;
	isLose = false;
	pause = true;

	//数据进行处理,canvas数组
	//...
	int i, j;
	for (i = 0; i <= HEIGHT; i++)
		for (j = 0; j <= WIDTH; j++)
		{			
			canvas[i][j] = SPACE;
		//	if (i < 3)
			//	canvas[i][j] = BRICK;

			if (j == WIDTH)
				canvas[i][j] = VEDGE;
			if (i == HEIGHT)
				canvas[i][j] = HEDGE;			
		}
	canvas[brick_row][brick_col] = BRICK;
	canvas[ball_row][ball_col] = BALL;
	for (i = baffle_col; i < baffle_col + baffle_size; i++)
		canvas[baffle_row][i] = BAFFLE;
}
//显示
void show()
{
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i <= HEIGHT; i++)//
	{
		for (j = 0; j <= WIDTH; j++)//
		{
			if //(ball_row == i && ball_col == j)
				(canvas[i][j]==BALL)
				printf("O");//小球
			else if (canvas[i][j]==HEDGE)
				printf("-");//边界
			else if (canvas[i][j]==VEDGE)
				printf("|");//边界
			else if (canvas[i][j]==BAFFLE)
				printf("=");//挡板
			else if (canvas[i][j] == BRICK)
				printf("#");//砖块
			//SPACE
			else printf(" ");
		}
		printf("n");
	}
	printf("score:%dn", score);
}

//等待用户的输入,交互
void updateWithInput()
{
	int i;
	for (i = baffle_col; i < baffle_col + baffle_size; i++)
		canvas[baffle_row][i] = SPACE;
	char input;
	if (kbhit()) {
		input = getch();
		switch (input)
		{
		case 'a': if (baffle_col>0)
			baffle_col--;
			break;
		case 'w': if (baffle_row>0)
			baffle_row--;
			break;
		case 'd': if (baffle_col= baffle_col &&
		ball_col < baffle_col + baffle_size)
		ball_vv =UP;	
	if (ball_row == brick_row + 1 && ball_col == brick_col)
	{//击中砖块
		canvas[brick_row][brick_col] = SPACE;
		//ball_vv *= -1;
		ball_vv = DOWN;
		brick_col = rand() % area_width;
		brick_row = 0;
		canvas[brick_row][brick_col] = BRICK;
		score++;
	}
	static int count = 0;
	count++;
	if (count == 3)
	{//小球的更新
		canvas[ball_row][ball_col] = SPACE;
		ball_col += ball_vh;
		ball_row += ball_vv;
		canvas[ball_row][ball_col] = BALL;
		count = 0;
		//从下方掉出游戏区域
		if (ball_row >= area_height)
			isLose = true;
	}
}
int main()
{
	HideCursor();
	startup();
	while (1)
	{
		show();
		updateWithInput();
		if (!pause)
		{			
			updateWithoutInput();
		}
		if (score == 10)
		{
			printf("过关了,恭喜n");
			break;
		}
		if (isLose == true)
		{
			printf("你被击败了,重新来过n");
			break;
		}
	}
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692820.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存