C++实现打飞机小游戏(源代码)

你们要的打飞机
还想让我做什么☺☺☺
>求关注,求点赞,求评论<
Thanks♪(・ω・)ノ
代码如下 ☟ ☟ ☟

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
 
/*=============== all the structures ===============*/
 
typedef struct Frame
{
   
	COORD position[2];
	int flag;
}Frame;
 
 
/*=============== all the functions ===============*/
 
void SetPos(COORD a)// set cursor 
{
   
	HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);
}
 
void SetPos(int i, int j)// set cursor
{
   
	COORD pos={
   i, j};
	SetPos(pos);
}
 
void HideCursor()
{
   
	CONSOLE_CURSOR_INFO cursor_info = {
   1, 0}; 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
   
	SetPos(x1,y);
	for(int i = 0; i <= (x2-x1); i++)
		cout<<ch;
}
 
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
   
	if(a.Y == b.Y)
		drawRow(a.Y, a.X, b.X, ch);
	else
	{
   
		SetPos(0, 25);
		cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
		system("pause");
	}
}
 
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
   
	int y=y1;
	while(y!=y2+1)
	{
   
		SetPos(x, y);
		cout<<ch;
		y++;
	}
}
 
//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{
   
	if(a.X == b.X)
		drawCol(a.X, a.Y, b.Y, ch);
	else
	{
   
		SetPos(0, 25);
		cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
		system("pause");
	}
}
 
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD  b, char row, char col)
{
   
	drawRow(a.Y, a.X+1, b.X-1, row);
	drawRow(b.Y, a.X+1, b.X-1, row);
	drawCol(a.X, a.Y+1, b.Y-1, col);
	drawCol(b.X, a.Y+1, b.Y-1, col);
}
 
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
   
	COORD a={
   x1, y1};
	COORD b={
   x2, y2};
	drawFrame(a, b, row, col);
}
 
void drawFrame(Frame frame, char row, char col)
{
   
	COORD a = frame.position[0];
	COORD b = frame.position[1];
	drawFrame(a, b, row, col);
}
 
void drawPlaying()
{
   
	drawFrame(0, 0, 48, 24, '=', '|');//	draw map frame;
	drawFrame(49, 0, 79, 4, '-', '|');//		draw output frame
	drawFrame(49, 4, 79, 9, '-', '|');//		draw score frame
	drawFrame(49, 9, 79, 20, '-', '|');//	draw operate frame
	drawFrame(49, 20, 79, 24, '-', '|');//	draw other message frame
	SetPos(52, 6);
	cout<<"得分:";
	SetPos(52, 7);
	cout<<"称号:";
	SetPos(52,10);
	cout<<"操作方式:";
	SetPos(52,12);
	cout<<"  a,s,d,w 控制战机移动。";
	SetPos(52,14);
	cout<<"  p 暂停游戏。";
	SetPos(52,16);
	cout<<"  e 退出游戏。";
}
 
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
   
	int c=(rand() % (a-b))+ a;
	return c;
}
 
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
   
	int x=random(a.X
  • 175
    点赞
  • 425
    收藏
    觉得还不错? 一键收藏
  • 49
    评论
以下是一个基于 C++ 的简单小游戏示例,大约有1000行代码: ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; class Player { private: string name; int score; public: Player(string n) { name = n; score = 0; } void increaseScore() { score++; } int getScore() { return score; } string getName() { return name; } }; class Game { private: Player* player; int target; public: Game(string playerName) { player = new Player(playerName); target = 10; } void play() { cout << "=== 猜数字游戏 ===" << endl; cout << "游戏规则:猜测一个在1到100之间的数字,看你能猜对几次!" << endl; srand(time(0)); while (true) { int guess; cout << "请输入你的猜测(1-100):"; cin >> guess; if (guess < 1 || guess > 100) { cout << "无效的猜测!请输入1到100之间的数字。" << endl; continue; } int randomNumber = rand() % 100 + 1; cout << "随机数字是:" << randomNumber << endl; if (guess == randomNumber) { player->increaseScore(); cout << "恭喜!你猜对了!" << endl; } else { cout << "很遗憾,你猜错了!" << endl; } cout << "你的分数是:" << player->getScore() << endl; if (player->getScore() == target) { cout << "恭喜你达到目标分数!游戏结束。" << endl; break; } char playAgain; cout << "是否继续游戏?(Y/N):"; cin >> playAgain; if (playAgain != 'Y' && playAgain != 'y') { cout << "游戏结束。" << endl; break; } } } ~Game() { delete player; } }; int main() { string playerName; cout << "请输入你的名字:"; cin >> playerName; Game game(playerName); game.play(); return 0; } ``` 这个示例是一个猜数字游戏,玩家需要猜测一个在1到100之间的随机数字。玩家每次猜对加一分,猜错不加分。游戏目标是达到10分。玩家可以选择继续游戏或结束游戏。 请注意,这只是一个简单的示例代码,实际游戏中可能需要更多的功能和交互设计。你可以根据自己的需求进行进一步的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值