C语言—用EasyX实现反弹球消砖块游戏

代码效果如下

cpp 复制代码
#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>

#define width 640
#define high 480
#define brick_num 10

int ball_x, ball_y;
int ball_vx, ball_vy;
int radius;
int bar_x, bar_y;
int bar_high, bar_width;
int bar_left, bar_right, bar_top, bar_bottom;

int isbrickexisted[brick_num];
int brick_high, brick_width;

int score;

void startup()
{
	srand(time(NULL));
	ball_x = rand()%(width/3)+width/3;
	ball_y = rand()%(high/4)+high/6;
	ball_vx = 2;
	ball_vy = 1;
	radius = 20;

	bar_high = high / 20;
	bar_width = width / 5;
	bar_x = width / 2;
	bar_y = high - bar_high / 2;
	bar_left = bar_x - bar_width / 2;
	bar_right = bar_x + bar_width / 2;
	bar_top = bar_y - bar_high / 2;
	bar_bottom = bar_y + bar_high / 2;

	brick_width = width / brick_num;
	brick_high = high / brick_num;

	int i;
	for (i = 0; i < brick_num; i++)
		isbrickexisted[i] = 1;

	initgraph(width, high, SHOWCONSOLE);
	BeginBatchDraw();
	
	score = 0;
}

void clean()
{
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball_x, ball_y, radius);
	bar(bar_left, bar_top, bar_right, bar_bottom);

	int i, brick_left, brick_right, brick_top, brick_bottom;
	for (i = 0; i < brick_num; i++)
	{
		brick_left = i * brick_width;
		brick_right = brick_left + brick_width;
		brick_top = 0;
		brick_bottom = brick_high;
		if (!isbrickexisted[i])
			fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
	}
}

void show()
{
	setbkcolor(RGB(0, 200, 200));
	cleardevice();
	setcolor(YELLOW);
	setfillcolor(GREEN);
	fillcircle(ball_x, ball_y, radius);
	bar(bar_left, bar_top, bar_right, bar_bottom);

	int i, brick_left, brick_right, brick_top, brick_bottom;

	for (i = 0; i < brick_num; i++)
	{
		brick_left = i * brick_width;
		brick_right = (i + 1) * brick_width;
		brick_top = 0;
		brick_bottom = brick_high;

		if (isbrickexisted[i])
		{
			setcolor(GREEN);
			setfillcolor(YELLOW);
			fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
		}
	}

	char s[5];
	sprintf_s(s, "%d", score);
	settextcolor(YELLOW);
	settextstyle(30, 0, s);
	outtextxy(0, high-high/6, "消的砖块数:");
	outtextxy(30*6, high-high/6, s);

	FlushBatchDraw();
	Sleep(1);
}

void updatewithoutinput()
{
	if (ball_y + radius >= bar_top && ball_x >= bar_left && ball_x <= bar_right)
	{
		ball_vy = 4;
		ball_vy = -ball_vy;
		int sign = 1;
		if (ball_vx < 0)
			sign = -1;
		ball_vx = (rand() % 3 + 2) * sign;
	}

	ball_x += ball_vx;
	ball_y += ball_vy;

	float distant_right,distant_left;
	distant_right = (ball_x - bar_right) * (ball_x - bar_right) + (ball_y - bar_top) * (ball_y - bar_top);
	distant_left= (ball_x - bar_left) * (ball_x - bar_left) + (ball_y - bar_top) * (ball_y - bar_top);
	if (distant_right <= radius * radius + 1 || distant_left <= radius * radius + 1)
	{
		ball_vx = -ball_vx;
		ball_vy = -ball_vy;
	}

	if (ball_x - radius <= 0 || ball_x + radius >= width)
		ball_vx = -ball_vx;
	if (ball_y - radius <= 0)
		ball_vy = -ball_vy;
	if (ball_y + radius >= high)
	{
		printf("\n游戏失败!\n");
		Sleep(1000);
		EndBatchDraw();
		closegraph();
		exit(0);
	}

	int i, brick_left, brick_right, brick_top, brick_bottom;
	for (i = 0; i < brick_num; i++)
	{
		if (isbrickexisted[i])
		{
			brick_left = brick_width * i;
			brick_right = brick_width * (i + 1);
			brick_top = 0;
			brick_bottom = brick_high;
			if (ball_y - radius <= brick_bottom && ball_x >= brick_left && ball_x <= brick_right)
			{
				score++;
				printf("\a");
				isbrickexisted[i] = 0;
				ball_vy = -ball_vy;
			}
		}
	}
	int flag = 0;
	for (i = 0; i < brick_num; i++)
	{
		if (isbrickexisted[i] == 1)
			flag = 1;
	}
	if (flag == 0)
	{
		for (i = 0; i < brick_num; i++)
		{
			isbrickexisted[i] = 1;
		}
	}

}

void updatewithinput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == 'a'&&bar_left>0)
		{
			bar_x = bar_x - 15;
			bar_left = bar_x - bar_width/2;
			bar_right = bar_x + bar_width/2;
		}
		if (input == 'd'&&bar_right<width)
		{
			bar_x += 15;
			bar_left = bar_x - bar_width/2;
			bar_right = bar_x + bar_width/2;
		}
	}
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	system("pause");
	startup();
	while (1)
	{
		clean();
		updatewithoutinput();
		updatewithinput();
		show();
	}
	gameover();
	return 0;
}
相关推荐
IT从业者张某某3 小时前
基于EGE19.01完成恐龙跳跃游戏-V00-C++使用EGE19.01这个轮子
c++·游戏
Sator111 小时前
Unity关于射击游戏人物动画的设计经验
游戏·unity·游戏引擎
王杨游戏养站系统12 小时前
3分钟搭建1个游戏下载站网站教程!SEO站长养站系统!
开发语言·前端·游戏·游戏下载站养站系统·游戏养站系统
huwuhang21 小时前
索尼PS3游戏合集【中文游戏】8.12T 1430个游戏+PS3模拟器
android·游戏·智能手机·游戏机·电视
毕业设计-小慧21 小时前
计算机毕业设计springboot游戏数据管理系统 基于SpringBoot的电竞赛事数据管理平台 基于SpringBoot的在线游戏运营数据分析系统
spring boot·游戏·课程设计
黑客说1 天前
AI驱动剧情,解锁无限可能——AI游戏发展解析
人工智能·游戏
智算菩萨1 天前
【OpenGL】10 完整游戏开发实战:基于OpenGL的2D/3D游戏框架、物理引擎集成与AI辅助编程指南
人工智能·python·游戏·3d·矩阵·pygame·opengl
风酥糖1 天前
Godot游戏练习01-第20节-增加亿点点细节
游戏·游戏引擎·godot
聊点儿技术1 天前
游戏账号盗用频发,IP风险等级评估如何成为第一道防线?
安全·游戏·ip地址·风险评估·账号安全·ip风险等级评估
Swift社区1 天前
鸿蒙游戏里的 AI Agent 设计
人工智能·游戏·harmonyos