【EasyX】C++双人桌面冰球

以前写的,翻出来发一下吧。

代码不是很长,适合新手练手用。

可以双人对战哦。

一些其它的可以看注释,不需要图片之类的,直接就可以运行。

差不多长下面这个样子。(按照记忆画的)

424381543bd84af69c757a4f2179e6c6.png

记得要先下EasyX图形库哦。

具体的请看注释吧!


下面放代码:


#include <conio.h>
#include <graphics.h>
#include<windows.h>
#define High 480  // 游戏画面尺寸
#define Width 640
// 全局变量
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int radius; // 小球的半径
int bar1_left, bar1_right, bar1_top, bar1_bottom; // 挡板1的上下左右位置坐标
int bar2_left, bar2_right, bar2_top, bar2_bottom; // 挡板2的上下左右位置坐标
int bar_height, bar_width; // 挡板的高度、宽度

void startup()  // 数据初始化
{
	ball_x = Width / 2;
	ball_y = High / 2;
	ball_vx = 5;
	ball_vy = 5;
	radius = 20;

	bar_width = Width / 30;
	bar_height = High / 2;

	bar1_left = Width * 1 / 20;
	bar1_top = High / 4;
	bar1_right = bar1_left + bar_width;
	bar1_bottom = bar1_top + bar_height;

	bar2_left = Width * 18.5 / 20;
	bar2_top = High / 4;
	bar2_right = bar2_left + bar_width;
	bar2_bottom = bar2_top + bar_height;

	initgraph(Width, High);
	BeginBatchDraw();
}

void clean()  // 消除画面
{
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball_x, ball_y, radius);
	fillcircle(ball_x, ball_y, radius);
	bar(bar1_left, bar1_top, bar1_right, bar1_bottom);
	bar(bar2_left, bar2_top, bar2_right, bar2_bottom);
}

void show()  // 显示画面
{
	setcolor(GREEN);
	setfillcolor(GREEN);
	fillcircle(ball_x, ball_y, radius);	// 绘制绿圆	

	setcolor(YELLOW);
	setfillcolor(YELLOW);
	bar(bar1_left, bar1_top, bar1_right, bar1_bottom);	// 绘制黄色挡板
	bar(bar2_left, bar2_top, bar2_right, bar2_bottom);

	FlushBatchDraw();
	// 延时
	Sleep(3);
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	// 挡板和小圆碰撞,小圆反弹
	if (ball_x + radius >= bar2_left && ball_y + radius >= bar2_top && ball_y + radius <= bar2_bottom)
		ball_vx = -ball_vx;
	else if (ball_x - radius <= bar1_right && ball_y + radius >= bar1_top && ball_y + radius <= bar1_bottom)
		ball_vx = -ball_vx;

	// 更新小圆坐标
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;

	if ((ball_x <= radius) || (ball_x >= Width - radius))
		ball_vx = -ball_vx;
	if ((ball_y <= radius) || (ball_y >= High - radius))
		ball_vy = -ball_vy;
}

void updateWithInput()  // 与用户输入有关的更新
{
	int step = 1;
	if (GetAsyncKeyState(0x57) & 0x8000)  // w
		bar1_top -= step;
	if ((GetAsyncKeyState(0x53) )) //s
		bar1_top += step;
	if ((GetAsyncKeyState(VK_UP)))     // 上方向键
		bar2_top -= step;
	if ((GetAsyncKeyState(VK_DOWN) & 0x8000))  // 下方向键
		bar2_top += step;

	bar1_bottom = bar1_top + bar_height;
	bar2_bottom = bar2_top + bar_height;
}

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

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		clean();  // 把之前绘制的内容取消
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
		show();  // 显示新画面
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

后记:这个游戏框架实话很好用————》简单的游戏框架icon-default.png?t=N7T8https://blog.csdn.net/m0_63288666/article/details/126154576?spm=1001.2014.3001.5502

有很多经典游戏都可以套用这个模板。

很推荐给初学者。

然后就是继续加油吧,有很多事儿呢。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljl2107

感谢我能帮助到你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值