64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y) //光标定位
{
COORD pos = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void initinet() //初始化游戏函数
{
system("title C++弹小球"); //控制台标题
system("mode con cols=80 lines=26"); //控制台界面大小
HideCursor(); //隐藏光标
cout << "得分:0 ";
for (int i = 9; i <= 79; i++)
cout << "-";
gotoxy(36, 24);
cout << "========";
}
class Ball //小球类
{
private:
int m_dir; //运动方向
int m_x; //x轴坐标
int m_y; //y坐标
void alterdir();//小球遇墙改变方向
void adicoor(); //方向改变坐标改变
void show(); //显示和清除小球
public:
Ball(); //构造初始化
int getballx(); //获取小球x坐标
int getbally(); //获取小球y坐标
void move(); //小球移动
};
Ball::Ball() //小球构造函数
{
srand(time(NULL));
m_x = rand() % 76 + 4; //小球初始x坐标为一个随机数
m_y = 2;
m_x > 40 ? m_dir = 3 : m_dir = 2;//根据小球最开始的x坐标确定初始的运动方向
gotoxy(m_x, m_y);
cout << "o"; //显示小球
}
int Ball::getballx()
{
return m_x;
}
int Ball::getbally()
{
return m_y;
}
inline void Ball::alterdir() //小球遇墙改变方向
{
if (m_y == 23) //边界宽高
m_dir == 2 ? m_dir = 1 : m_dir = 4;
if (m_y == 1)
m_dir == 1 ? m_dir = 2 : m_dir = 3;
if (m_x == 0)
m_dir == 4 ? m_dir = 1 : m_dir = 2;
if (m_x == 79)
m_dir == 1 ? m_dir = 4 : m_dir = 3;
}
inline void Ball::adicoor() //方向改变坐标改变
{
switch (m_dir)
{
case 1: //右上
m_x++; m_y--; break;
case 2: //右下
m_x++; m_y++; break;
case 3: //左下
m_x--; m_y++; break;
case 4: //左上
m_x--; m_y--; break;
}
}
inline void Ball::show() //显示和清除小球
{
gotoxy(m_x, m_y);
cout << "o";
Sleep(38); //游戏速度
gotoxy(m_x, m_y);
cout << " ";
}
void Ball::move() //小球移动函数
{
adicoor(); //方向改变坐标改变
alterdir(); //遇墙壁改变方向
show(); //显示小球
}
class Stick //棍子类
{
private:
int m_x;
const int m_y = 24; //棍子y坐标不动
public:
Stick(); //构造初始化
int getstickx(); //获取棍子头x坐标
void shift(); //棍子移动
};
Stick::Stick() //棍子构造
{
m_x = 36; //棍子的x坐标
}
int Stick::getstickx() //获取棍子头x坐标
{
return m_x;
}
void Stick::shift() //棍子移动
{
char shifxy = _getch();
switch (shifxy)
{
case 'a':
if (m_x >= 1) //棍子不可以越界
m_x -= 3;
gotoxy(m_x + 8, m_y); //在棍子首位的地方清除痕迹
cout << " ";
break;
case'd':
if (m_x <= 71)
m_x += 3;
gotoxy(m_x - 3, m_y);
cout << " ";
}
gotoxy(m_x, m_y);
cout << "========";
}
inline void dgefin(Ball& ball, Stick& stick, int& speed) //游戏规则全局函数
{
if (ball.getbally() < 23) //小球的y坐标没有到棍子y轴范围时不进行范围判断
return;
static int score = 0; //累加得分
if (ball.getballx() < stick.getstickx() || ball.getballx() > stick.getstickx() + 8 || score == 23) //不位于棍子范围的球直接结束游戏,或得分达到一定数值
{
gotoxy(37, 12);
cout << "游戏结束";
gotoxy(37, 14);
cout << "得分:" << score;
gotoxy(0, 25);
exit(0);
}
score++; //接住则得分+1
gotoxy(6, 0);
cout << score;
if (speed >= 0)
speed--; //每次接住小球速度加快1毫秒
}
int main()
{
initinet(); //初始化游戏函数
Ball ball;
Stick stick;
_getch(); //游戏开始前先暂停
gotoxy(ball.getballx(), ball.getbally()); //用于清除刚开始的小球
cout << " ";
int speed = 24; //递增的游戏速度的值
while (true)
{
ball.move(); //小球运动
if(_kbhit()) //键盘按下时棍子移动
stick.shift();
dgefin(ball, stick, speed);//判断游戏规则全局函数
Sleep(speed); //递增的游戏速度
}
return 0;
};