静止的小球

/*函数组织的消砖块,静止的小球,增加边界,增加档板,挡板反弹
2021.9.14
木C402
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
using namespace std;
#include<Windows.h>
#define area_height 15    //游戏区域  0-n-1
#define area_width 20
int ball_vx, ball_vy;//小球速度,vv垂直速度,vh水平速度
int ball_x, ball_y;//小球位置
int score;//积分
int baffle_x = area_height - 1, baffle_y=area_width / 2;//挡板中心坐标
int baffle_right, baffle_left;//挡板左右边界
int baffle_size;//挡板半径
bool isLose;//是否已经失败
int canvas[area_height][area_width] = { 0 };//0为空格,1为小球,2为挡板,3为砖块
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()
{
	ball_y = baffle_y;
	ball_x = baffle_x-1;
	ball_vx = -1;
	ball_vy = 1;
	baffle_size = 5;
	baffle_left = baffle_y - baffle_size;
	baffle_right = baffle_y + baffle_size;
	score = 0;
	canvas[ball_x][ball_y] = 1;
	for (int i = baffle_left; i <= baffle_right; i++)
		canvas[baffle_x][i] = 2;
	for (int k = 0; k < area_width; k++)
		for (int i = 0; i < area_height/4; i++)
			canvas[i][k] = 3;
	isLose = false;
}
//显示
void show()
{
	
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i <area_height; i++)//行遍历
	{
		for (j = 0; j < area_width; j++)//列遍历
		{
			if (canvas[i][j] == 0)
				printf(" ");
			else if (canvas[i][j] == 1)
				printf("0");//输出小球
			else if (canvas[i][j] == 2)
				printf("*");//输出挡板
			else if (canvas[i][j] == 3)
				printf("#");//输出砖块
		}
		printf("|\n");//显示右边界
	}
	for (j = 0; j < area_width; j++)
		printf("-");//显示下边界
	printf("\n");
	printf("score:%d\n", score);
}

//更新
void updateWithourInput()
{
	if (ball_x== (area_height-2) )
	{
		if((ball_y>=baffle_left)&&(ball_y<=baffle_right))
		{

		}
		else
		{
			printf("你被击败了,重新来过\n");
			system("pause");
			exit(0);
		}
	}
	static int speed = 0;
	if (speed < 7)
		speed++;
	if (speed == 7)
	{
		speed = 0;
		canvas[ball_x][ball_y] = 0;
		ball_x += ball_vx;
		ball_y += ball_vy;
		canvas[ball_x][ball_y] = 1;
		//碰到边界反弹
		if ((ball_x == 0) || (ball_x == area_height - 2))
			ball_vx = -ball_vx;
		if ((ball_y == 0) || (ball_y == area_width - 1))
			ball_vy = -ball_vy;
		//碰到砖块后反弹
		if (canvas[ball_x-1 ][ball_y] == 3)
		{
			ball_vx = -ball_vx;
			canvas[ball_x -1][ball_y] = 0;
		}
	}
		
}
//等待用户的输入,交互
void updateWithInput()
{
	char input;
	if (_kbhit()) {
		input = _getch();
		if (input == 'a' && baffle_left > 0)
		{
			canvas[baffle_x][baffle_right] = 0;
			baffle_y--;
			baffle_left = baffle_y - baffle_size;
			baffle_right = baffle_y + baffle_size;
			canvas[baffle_x][baffle_left] = 2;
		}
		if (input == 'd' && baffle_right < area_width - 1)
		{
			canvas[baffle_x][baffle_left] = 0;
			baffle_y++;
			baffle_left = baffle_y - baffle_size;
			baffle_right = baffle_y + baffle_size;
			canvas[baffle_x][baffle_right] = 2;
		}
	}
}
int main()
{
	
	startup();
	while (1)
	{
		show();
		updateWithInput();
		updateWithourInput();
		if (score == 10)
		{
			printf("过关了,恭喜\n");
			break;
		}
		//Sleep(100);
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值