【项目实战】C/C++自制程序:双人击球游戏

每天一个编程小项目,提升你的编程能力!

游戏说明

Player1 玩家通过 W、S 按键控制左板上下移动,Player2 玩家通过 O、L 按键控制右板上下移动。在游戏过程中,小球的速度会随着接触木板的次数增加而加快,增加游戏难度。当小球没有碰到木板时游戏结束。

游戏效果

简单了解游戏后我们就来试试吧!(直接上源码,大家可以看注释)

///
// 程序名称:双人击球游戏
// 编译环境:VS2010,EasyX_20200902
//

#include <graphics.h>
#include <conio.h>

int main()
{
	initgraph(640, 480);
	srand(GetTickCount());
	setbkcolor(WHITE);
	cleardevice();

	setfillcolor(BLACK);
	setlinecolor(BLACK);
	settextstyle(30, 0, L"verdana");
	settextcolor(BLACK);

	int p1_x, p1_y, p2_x, p2_y, ball_x, ball_y;
	int dx, dy;
	int win = 1;							// 赢玩家
	p1_x = 45;								// 左板
	p1_y= 240;
	p2_x = 595;								// 右板
	p2_y = 240;	
	ball_x = rand() % 240 + 200;			// 小球 x 坐标
	ball_y = rand() % 180 + 150;			// 小球 y 坐标
	dx = (rand() % 2 * 2 - 1)*2;			// 小球移动增量
	dy = (rand() % 2 * 2 - 1)*2;			// 小球移动增量

	outtextxy(190, 200, L"Put Enter To Begin");
	while(_getwch() != 13);

	cleardevice();

	// player
	settextstyle(20, 0, L"Verdana");
	outtextxy(20, 50, L"Payer1");
	outtextxy(565, 50, L"Player2");

	// 提示
	settextstyle(15, 0, L"Verdana");
	outtextxy(20, 400, L"W 向上");
	outtextxy(20, 425, L"S 向下");
	outtextxy(590, 400, L"O 向上");
	outtextxy(590, 425, L"L 向下");

	line(50, 100, 590, 100);											// 上边界
	line(50, 380, 590, 380);											// 下边界
	solidcircle(ball_x, ball_y, 10);

	wchar_t key=0;
	while(true)
	{
		clearcircle(ball_x, ball_y, 10);								// 清除小球位置
		clearrectangle(p1_x - 5, p1_y - 20, p1_x + 5, p1_y + 20);		// 清除左板
		clearrectangle(p2_x - 5, p2_y - 20, p2_x + 5, p2_y + 20);		// 清除右板

		if(_kbhit())
		{
			// 获取按键
			key = _getwch();		
	
			// 判断板移动的位置
			if(key == L's')
				p1_y += 30;
			else if(key == L'w')
				p1_y -= 30;
			else if(key == L'l')
				p2_y += 30;
			else if(key == L'o')
				p2_y -= 30;
		}

		// 判断左右俩板是否超出移动界限
		if((p1_y - 20) < 100)
			p1_y = 120;
		else if((p1_y + 20) > 380)
			p1_y = 360;
		else if((p2_y - 20) < 100)
			p2_y = 120;
		else if((p2_y + 20)> 380)
			p2_y = 360;

		// 画左右两板位置
		solidrectangle(p1_x - 5, p1_y - 20, p1_x + 5, p1_y + 20);
		solidrectangle(p2_x - 5, p2_y - 20, p2_x + 5, p2_y + 20);

		ball_x = ball_x + dx;
		ball_y = ball_y + dy;

		// 判断小球遇到左右板反弹
		if(ball_x < 62 )
		{
			if(ball_y < p1_y + 20 && ball_y > p1_y - 20)
			{
				dx = -dx;
				dx += dx / 10;
			}
			else 
			{
				win = 2;
				solidcircle(ball_x, ball_y, 10);
				break;
			}
		}
		else if(ball_x > 578)
		{
			if(ball_y < p2_y + 20 && ball_y > p2_y - 20)
			{
				dx = -dx;
				dx += dx / 10;
			}
			else
			{
				solidcircle(ball_x, ball_y, 10);
				break;
			}
		}

		// 判断小球遇到上下边界反弹
		if(ball_y < 115)
			dy = -dy;
		else if(ball_y > 364)
			dy = -dy;

		solidcircle(ball_x, ball_y, 10);

		Sleep(20);
	}

	settextstyle(30, 0, L"Verdana");
	if(win == 1)
		outtextxy(250, 200, L"Player1 赢");
	else
		outtextxy(250, 200, L"Player2 赢");
	
	_getch();
	closegraph();
	return 0;
}

大家赶紧去动手试试吧!

此外,我也给大家分享我收集的其他资源,从最零基础开始的教程到C语言C++项目案例,帮助大家在学习C语言的道路上披荆斩棘!

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!(资料领取↓↓↓↓↓↓

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题。以下是一个使用 switch 语句实现猜拳游戏C++ 代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int playerChoice; int computerChoice; // 生成随机数种子 srand(time(0)); // 让玩家输入选择(1:石头,2:剪刀,3:布) cout << "请出拳(1:石头,2:剪刀,3:布):" << endl; cin >> playerChoice; // 生成计算机的选择(1-石头,2-剪刀,3-布) computerChoice = rand() % 3 + 1; // 根据玩家和计算机的选择确定胜负 switch (playerChoice) { case 1: // 玩家出石头 if (computerChoice == 1) { cout << "平局!" << endl; } else if (computerChoice == 2) { cout << "你赢了!" << endl; } else { cout << "你输了!" << endl; } break; case 2: // 玩家出剪刀 if (computerChoice == 1) { cout << "你输了!" << endl; } else if (computerChoice == 2) { cout << "平局!" << endl; } else { cout << "你赢了!" << endl; } break; case 3: // 玩家出布 if (computerChoice == 1) { cout << "你赢了!" << endl; } else if (computerChoice == 2) { cout << "你输了!" << endl; } else { cout << "平局!" << endl; } break; default: cout << "选择无效!" << endl; break; } return 0; } ``` 这段代码首先让玩家输入选择(1:石头,2:剪刀,3:布),然后生成计算机的选择。接着使用 switch 语句判断玩家和计算机的选择,输出对应的胜负结果。注意,这里使用了 srand 函数来生成随机数种子,以保证每次运行程序生成的随机数不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值