c++游戏集合{干货}(全是干货)

这篇文章包含了一系列经典游戏的C++实现代码,包括贪吃蛇、坦克大战、猜数字和俄罗斯方块等。同时,还展示了一个简单的加解密算法,展示了如何使用字符串和密钥进行加密和解密操作。
摘要由CSDN通过智能技术生成
//不建议全部复制,因为有多个游戏代码在里面,感谢支持!!!


#include <iostream>  //贪吃蛇
#include <cstdlib>  
#include <ctime>  
#include <vector>  
  
using namespace std;  
  
// 定义贪吃蛇类  
class Snake {  
public:  
    Snake(int x, int y);  
    void move(int dx, int dy);  
    bool isAlive() const;  
private:  
    vector<int> pos;  
    int length;  
};  
  
// 定义食物类  
class Food {  
public:  
    Food();  
    int getX() const;  
    int getY() const;  
private:  
    int x;  
    int y;  
};  
  
// 游戏主类  
class Game {  
public:  
    Game(int width, int height);  
    void play();  
private:  
    int width;  
    int height;  
    Snake snake;  
    Food food;  
};  
  
// 初始化蛇的移动方向和位置  
void initSnake(Snake& snake, int x, int y) {  
    snake.pos.push_back(x);  
    snake.pos.push_back(y);  
    snake.length = 1;  
}  
  
// 随机生成食物的位置  
void initFood(Food& food) {  
    srand(time(NULL)); // 设置随机数种子  
    do {  
        food.x = rand() % Game::getWidth() + 1;  
        food.y = rand() % Game::getHeight() + 1;  
    } while (food.x == snake.pos[0] && food.y == snake.pos[1]); // 避免食物和蛇头重合  
}  
  
// 初始化游戏环境  
Game::Game(int width, int height) {  
    this->width = width;  
    this->height = height;  
    initSnake(snake, width / 2, height / 2); // 设置蛇头位置为屏幕中心  
    initFood(food); // 随机生成食物位置  
}  
  
// 控制蛇的移动  
void Snake::move(int dx, int dy) {  
    pos.erase(pos.begin()); // 删除蛇尾位置  
    pos.push_back(pos.back() + dx); // 移动蛇身  
    pos.push_back(pos.back() + dy); // 移动蛇身  
    length++; // 增加蛇的长度  
}  
  
// 判断蛇是否吃到食物  
bool Snake::isAlive() const {  
    return length > 1; // 只有当蛇身体长度大于1时才表示蛇是活着的  
}  
  
// 游戏主循环  
void Game::play() {  
    bool gameOver = false;  
    while (!gameOver) {  
        // 随机生成食物  
        initFood(food);  
  
        // 检测玩家操作,控制蛇的移动  
        int dx = 0;  
        int dy = 0;  
        cout << "Press arrow keys to move the snake: ";  
        cin >> dx >> dy;  
        if (dx == 0 && dy == 0) continue; // 如果玩家没有操作,则跳过本次循环  
        snake.move(dx, dy); // 控制蛇的移动
        // 判断蛇是否吃到食物  
        if (snake.isAlive() && food.getX() == snake.pos[0] && food.getY() == snake.pos[1]) {  
            cout << "You got the food!" << endl;  
            snake.length++; // 增加蛇的长度  
            initFood(food); // 重新生成食物位置  
        }  
  
        // 判断蛇是否碰到自己  
        for (int i = 1; i < snake.length; i++) {  
            if (snake.pos[i-1] == snake.pos[i] && snake.pos[i+1] == snake.pos[i]) {  
                gameOver = true;  
                break;  
            }  
        }  
  
        // 判断游戏是否结束  
        if (gameOver || !snake.isAlive()) {  
            cout << "Game over!" << endl;  
            break;  
        }  
    }  
}  
  
int main() {  
    Game game(80, 25);  
    game.play();  
    return 0;  
}
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
#include <iostream>  //坦克大战
#include <conio.h>  
#include <windows.h>  
  
#define WIDTH 40  
#define HEIGHT 20  
  
using namespace std;  
  
// 游戏状态  
enum GameState {  
    GS_START,  
    GS_PLAY,  
    GS_GameOver  
};  
  
// 坦克类  
class Tank {  
public:  
    int x, y;  
    bool dir;  
    tank(int x, int y) : x(x), y(y), dir(false) {}  
    void move();  
};  
  
// 移动坦克  
void tank::move() {  
    if (dir) {  
        x += 2;  
    } else {  
        x -= 2;  
    }  
    if (x >= WIDTH || x <= 0) {  
        dir = !dir;  
    }  
}  
  
// 游戏窗口  
class GameWindow {  
public:  
    GameWindow() : gamestate(GS_START), score(0),难(' ') {  
        for (int i = 0; i < HEIGHT; i++) {  
            for (int j = 0; j < WIDTH; j++) {  
                board[i][j] = ' ';  
            }  
        }  
    }  
    void draw();  
    void update();  
    void input();  
private:  
    GameState gamestate;  
    int score;  
    char board[HEIGHT][WIDTH];  
    tank tanks[10];  
    int tankcount;  
    char难;  
};  
  
// 绘制游戏窗口  
void GameWindow::draw() {  
    system("cls");  
    for (int i = 0; i < HEIGHT; i++) {  
        for (int j = 0; j < WIDTH; j++) {  
            cout << board[i][j];  
        }  
        cout << endl;  
    }  
    cout << "Score: " << score << endl;  
}  
  
// 更新游戏状态  
void GameWindow::update() {  
    switch (gamestate) {  
        case GS_START:  
            draw();  
            break;  
        case GS_Play:  
            for (int i = 0; i < tankcount; i++) {  
                tanks[i].move();  
            }  
            for (int i = 0; i < tankcount; i++) {  
                for (int j = 0; j < HEIGHT; j++) {  
                    if (board[j][tanks[i].x] == '#') {  
                        continue;  
                    }  
                    if (tanks[i].x == j) {  
                        board[j][tanks[i].x] = 'O';  
                        score++;  
                        break;  
                    }  
                    board[j][tanks[i].x] = 'X';  
                    break;  
                }  
            }  
            draw();  
            break;  
        case GS_GameOver:  
            cout << "Game Over!" << endl;  
            break;  
    }  
}  
  
// 处理输入事件  
void GameWindow::input() {  
    if (_kbhit()) {  
        char c = _getch();  
        switch (c) {  
            case 'a':  
            case 'A':  
                for (int i = 0; i < tankcount; i++) {  
                    tanks[i].x -= 2;  
                }  
                break;  
            case 'd':  
            case 'D':  
                for (int i = 0; i < tankcount; i++) {  
                    tanks[i].x += 2;  
                }  
                break;  
            case 'w':  
            case 'W':  
                for (int i = 0; i < tankcount; i++) {  
                    tanks[i].y -= 2;  
                }  
                break;  
            case 's':  
            case 'S':  
                for (int i = 0; i < tankcount; i++) {  
                    tanks[i].y += 2;  
                }  
                break;  
            case ' ': //开始新游戏,清空游戏状态和分数,重新初始化坦克位置和方向,随机生成障碍物。
gamestate = GS_Play;  
            score = 0;  
            for (int i = 0; i < tankcount; i++) {  
                tanks[i].x = rand() % WIDTH;  
                tanks[i].y = rand() % HEIGHT;  
                tanks[i].dir = rand() % 2 == 0;  
            }  
            for (int i = 0; i < HEIGHT; i++) {  
                board[i][WIDTH - 1] = '#';  
            }  
            break;  
        case 'q':  
        case 'Q':  
            gamestate = GS_GameOver;  
            break;  
        default:  
            break;  
        }  
    }  
}
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
#include <iostream>  //猜数字
#include <cstdlib>  
#include <ctime>  
  
using namespace std;  
  
 int main()  
 {  
 int num, guess, tries = 0;  
  
 // 生成一个 1~100 的随机数  
 srand(time(NULL));  
 num = rand() % 100 + 1;  
  
 cout << "猜数字游戏\n";  
  
 do  
 {  
 cout << "请输入一个 1~100 的整数: ";  
 cin >> guess;  
 tries++;  
  
 if (guess > num)  
 {  
 cout << "太大了!\n";  
 }  
 else if (guess < num)  
 {  
 cout << "太小了!\n";  
 }  
 else  
 {  
 cout << "恭喜你,猜对了!你用了 " << tries << " 次猜对了!\n";  
 }  
 } while (guess != num);  
  
 return 0;  
 }
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
#include <iostream>  //俄罗斯方块
#include <conio.h>  
  
using namespace std;  
  
 const int ROWS = 20;  
 const int COLS = 10;  
  
 int blockTypes[4] = {  
 '{', '}', '|', '_'  
 };  
  
 int board[ROWS][COLS];  
 int currentType = 0;  
  
 void clearBoard()  
 {  
 for (int i = 0; i < ROWS; i++) {  
 for (int j = 0; j < COLS; j++) {  
 board[i][j] = ' ';  
 }  
 }  
 }  
  
 void printBoard()  
 {  
 for (int i = 0; i < ROWS; i++) {  
 for (int j = 0; j < COLS; j++) {  
 cout << board[i][j];  
 }  
 cout << endl;  
 }  
 }  
  
 bool isvalid(int x, int y)  
 {  
 if (x < 0 || x >= COLS || y < 0 || y >= ROWS) {  
 return false;  
 }  
 return true;  
 }  
  
 void dropBlock(int type)  
 {  
 int x = ROWS - 1;  
 int y = COLS - 1;  
  
 while (isvalid(x, y)) {  
 board[x][y] = type + 48;  
 x--;  
 y--;  
 }  
 }  
  
 void shiftRowsDown()  
 {  
 for (int i = 0; i < ROWS - 1; i++) {  
 for (int j = 0; j < COLS; j++) {  
 board[i][j] = board[i+1][j];  
 }  
 }  
 board[ROWS-1][COLS-1] = ' '; // empty the last row.  
 }  
  
 void shiftRowsUp()  
 {  
 for (int i = ROWS - 1; i > 0; i--) {  
 for (int j = 0; j < COLS; j++) {  
 board[i][j] = board[i-1][j];  
 }  
 }  
 board[0][Cols-1] = ' '; // empty the first row.  
 }  
  
 void moveBlockLeft()  
 {  
 int x = ROWS - 1;  
 int y = COLS - 1;  
  
 while (isvalid(x, y)) {  
 board[x][y] = ' '; // make the space empty.  
 x--; y--; // move the pointer to the right.  
 } x++; y++; // bring the pointer back to its original position. we didn't count the last empty space in the loop above. so it needs to be empty now.  
 board[x][y] = currentType + 48; // set the block type back to its original value. remember that we subtracted 48 to get the actual ASCII value of the character. so we need to add it back to get the actual character. } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } }}
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————
#include <iostream>  //加解密钥
#include <string>  
#include <unordered_map>  
  
using namespace std;  
  
string encrypt(string message, string key) {  
    unordered_map<char, char> cipher;  
    string result = "";  
  
    // 生成密钥映射表  
    for (int i = 0; i < key.length(); i++) {  
        cipher[key[i]] = message[i];  
    }  
  
    // 对消息进行加密  
    for (auto& pair : cipher) {  
        result += pair.second;  
    }  
  
    return result;  
}  
  
string decrypt(string message, string key) {  
    unordered_map<char, char> cipher;  
    string result = "";  
  
    // 根据密钥映射表进行解密  
    for (int i = 0; i < key.length(); i++) {  
        cipher[message[i]] = key[i];  
    }  
  
    // 对消息进行解密  
    for (int i = 0; i < message.length(); i++) {  
        result += cipher[message[i]];  
    }  
  
    return result;  
}  
  
int main() {  
    string message = "你好,世界!";  
    string key = "密匙";  
  
    cout << "原始消息: " << message << endl;  
    cout << "密钥: " << key << endl;  
  
    // 对消息进行加密  
    string encryptedMessage = encrypt(message, key);  
    cout << "加密后的消息: " << encryptedMessage << endl;  
  
    // 对加密后的消息进行解密  
    string decryptedMessage = decrypt(encryptedMessage, key);  
    cout << "解密后的消息: " << decryptedMessage << endl;  
  
    return 0;  
}

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#include<bits/stdc++.h> #include<windows.h> using namespace std; int choice; int a=0,b=0,c=0,d=0; int user_input; void S(int Color) { HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,Color); } void wait() { Sleep(100); user_input=getchar(); } void cls() { Sleep(100); system("cls"); } int main(int argc, char *argv[]) { S(172); start:cls(); goto a1; gameover:printf("游戏结束。"); printf("按回车键继续"); user_input=getchar(); goto start; a1:printf("现在游戏开始。"); user_input=getchar(); printf("一天,你正在开飞机经过一片雪山区。"); wait(); printf("忽然你的操作系统失控了,飞机直直地坠入山中。"); wait(); printf("你朝两旁看了看,左边是雪地,右边是树林。而你正前方是悬崖,所以你不得不转弯。\n1-左转。\n2-右转。\n"); s1:wait(); switch(user_input) { case'1':goto a8; case'2':goto a13; default:goto s1; } a2:cls(); printf("你下了飞机,走向树林。"); wait(); printf("过了一会儿,你感到又饿又冷,就喝了一点威士忌,坐下休息。"); wait(); printf("但是,威士忌并没有让你觉得温暖。你只是觉得很冷、很冷。\n"); goto gameover; a3:cls(); printf("你吃了一些果实,还摘了一些放在口袋,想晚一点吃。"); wait(); printf("过了一会儿,你的肚子剧烈的痛起来。这时,你才意识到,果子有毒!\n"); goto gameover; a4:cls(); printf("你是否要继续往隧道里走?\n1-是。\n2-不了。\n"); s4:wait(); switch(user_input) { case'1':goto a18; case'2':goto a22; default:goto s4; } a5:cls(); printf("你能带三件东西下飞机。请选择:\n1-香蕉、打火机、外套。\n2-威士忌、外套、匕首。\n"); s5:wait(); switch(user_input) { case'1':goto a10; case'2':goto a2; default:goto s5; } a6:cls(); printf("你走到了隧道里,隧道里很黑,忽然你被一个东西绊倒了。\n"); wait(); printf("你爬起来,看了看地上的东西,原来是一条麻绳。\n1-捡起来。\n2-置之不理。\n"); s6:wait(); switch(user_input) { case'1':goto a11; case'2':goto a23; default:goto s6; } a7:cls(); printf("你呆在飞机里,感到越来越冷,你不得不离开了飞机。\n"); goto a5; a8:cls(); printf("随着“噗”地一声闷响,你的飞机安全降落在雪地上。"); wait(); printf("你要离开飞机去找东西吗?\n1-离开。\n2-待着。\n"); s8:wait(); switch(user_input) { case'1':goto a5; case'2':goto a7; default:goto s8; } a9:cls(); printf("你走出了隧道,继续走。\n"); wait(); printf("你来到了一个山谷前。"); wait(); printf("要走过去吗?\n1-走。\n2-不走。\n"); s9:wait(); switch(user_input) { case'1':goto a17; case'2':goto a12; default:goto s9; } a10:cls(); printf("你下了飞机,漫无目的地寻找食物。\n"); wait(); printf("你看到了一棵树,树上结着一种你从未见过的果子。\n1-吃一点。\n2-离开。\n"); s10:wait(); switch(user_input) { case'1':goto a3; case'2':goto a15; default:goto s10; } a11:cls(); printf("你捡起了绳子,发现它很长。"); a++; wait(); wait(); goto a22; a12:cls(); printf("你没有走进山谷,而是继续走。\n"); wait(); printf("很快,你就因饥饿死去了。\n"); wait(); goto gameover; a13:cls(); printf("“轰”,你的飞机撞上了树,爆炸了。"); wait(); goto gameover; a14:cls(); printf("你追着飞机跑。但是,飞机比你快太多了,你没有追上。"); wait(); printf("你很累很累,倒在了地上。"); wait(); printf("你死于虚脱。\n"); wait(); goto gameover; a15:cls(); printf("你没有吃果子,而是继续走。你走了很久,来到一个隧道口。\n1-进去。\n2-不进去。\n"); s15:wait(); switch(user_input) { case'1':goto a6; case'2':goto a21; default:goto s15; } a16:cls(); printf("你捞起了很多鱼,找了些树枝,烤了鱼来吃。"); wait(); printf("你觉得好多了。"); wait(); printf("这时,天空中传来嗡嗡的响声。你抬头一看,有飞机!\n1-追着飞机跑。\n2-点篝火。\n"); s16:wait(); switch(user_input) { case'1':goto a14; case'2':goto a20; default:goto s16; } a17:cls(); printf("你走了很久,有些体力不支,于是你把香蕉吃了。"); wait(); printf("你走了很久,来到了一个废弃矿坑边。\n1-探索一下。\n2-离开。\n"); s17:wait(); switch(user_input) { case'1':goto a24; case'2':goto a19; default:goto s17; } a18:cls(); printf("你继续往隧道里走,你感觉呼吸越来越难受。\n"); wait(); wait(); printf("你死于窒息。\n"); wait(); goto gameover; a19:cls(); printf("你来到了一个冰封的湖边。"); wait(); if(a==1) { if(b==1) { printf("你用麻绳织了个渔网,用锤子砸破冰面,试图捞上一些鱼。\n"); wait(); goto a16; } else { printf("你用麻绳织了个渔网,却无法打破冰面。你无奈地望着湖面,绝望地叹了口气。\n"); wait(); printf("你饿死了。\n"); wait(); goto gameover; } } else { if(b==1) { printf("你用锤子砸破了冰面,却没办法捞鱼。你无奈地望着湖面,绝望地叹了口气。\n"); wait(); printf("你饿死了。\n"); wait(); goto gameover; } else { printf("你很想捞鱼,却无法打破冰面,又没办法捞鱼。你无奈地望着湖面,绝望地叹了口气。\n"); wait(); printf("你饿死了。\n"); wait(); goto gameover; } } a20:cls(); printf("你找来许多木头,点燃了篝火。飞机上的人看见了浓烟,降落下来,把你救上了飞机。\n"); wait(); printf("事后,你被送去了医院治疗。\n"); printf("生存成功!\n"); wait(); goto gameover; a21:cls(); printf("你绕过了隧道,继续走。\n"); goto a9; a22:cls(); printf("你要继续走吗?\n1-是。\n2-不了。\n"); s22:wait(); switch(user_input) { case'1':goto a18; case'2':goto a9; default:goto s22; } a23:cls(); printf("你没有捡起绳子。"); wait(); wait(); goto a22; a24:cls(); printf("你找到了一把锤子。\n"); b++; wait(); wait(); goto a19; return 0; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值