holyya.com
2026-01-17 18:41:29 Saturday
登录
文章检索 我的文章 写文章
"带用户登录的C++贪吃蛇代码"
2023-06-29 10:41:34 深夜i     --     --
C++ 贪吃蛇 用户登录 代码 游戏

贪吃蛇游戏是一款经典的游戏,因其简单易学、容易上手而深受大家喜欢。在这篇文章中,我们将会介绍一款带用户登录的C++贪吃蛇游戏代码,让你的游戏更具人性化。

这款游戏支持用户登录,每位用户拥有自己的游戏记录和等级排名。为了实现这一功能,我们需要一个用户类,存储用户信息,并将其储存在本地文件中。同时,我们需要对游戏内容进行修改,增加游戏记录的记录,并根据每次游戏得分计算玩家的排名。

以下是代码实现:


#include <fstream>

#include <iostream>

#include <conio.h>

#include <string>

#include <cstdlib>

#include <time.h>

#include <windows.h>

using namespace std;

class Snake {

public:

  int score, length, head_x, head_y, fruit_x, fruit_y;

  int body_x[100], body_y[100];

  bool gameOver;

  Snake () {

    score = 0;

    gameOver = false;

    head_x = 20;

    head_y = 10;

    fruit_x = rand() % 40;

    fruit_y = rand() % 20;

    length = 1;

    body_x[0] = head_x;

    body_y[0] = head_y;

  }

  void draw () {

    system("cls");

    for (int i = 0; i < 22; i++) {

      for (int j = 0; j < 42; j++) {

        if (i == 0 || i == 21 || j == 0 || j == 41)

          cout << "#";

         else if (i == head_y && j == head_x)

          cout << "O";

         else if (i == fruit_y && j == fruit_x)

          cout << "F";

         else {

          bool printed = false;

          for (int k = 0; k < length; k++) {

            if (body_x[k] == j && body_y[k] == i)

              cout << "o";

              printed = true;

            

          }

          if (!printed)

            cout << " ";

          

        }

      }

      cout << endl;

    }

    cout << "Score: " << score << endl;

    cout << "Length: " << length << endl;

  }

  void move () {

    if (_kbhit()) {

      switch(_getch()) {

        case 'w':

          head_y--;

          break;

        case 'a':

          head_x--;

          break;

        case 's':

          head_y++;

          break;

        case 'd':

          head_x++;

          break;

      }

    }

    if (head_x == fruit_x && head_y == fruit_y) {

      score += 10;

      fruit_x = rand() % 40;

      fruit_y = rand() % 20;

      length++;

    }

    if (head_x == 0 || head_x == 41 || head_y == 0 || head_y == 22)

      gameOver = true;

    

    for (int i = 1; i < length; i++) {

      if (head_x == body_x[i] && head_y == body_y[i])

        gameOver = true;

        break;

      

    }

    for (int i = length - 1; i > 0; i--) {

      body_x[i] = body_x[i - 1];

      body_y[i] = body_y[i - 1];

    }

    body_x[0] = head_x;

    body_y[0] = head_y;

  }

};

class User {

public:

  string name;

  int score;

  int rank;

  User ()

    score = 0;

    rank = 0;

  

  void save () {

    fstream file("users.txt", ios::in | ios::out | ios::app);

    file << name << " " << score << endl;

  }

};

void updateRanking () {

  fstream file("users.txt");

  string line, name;

  int score, rank = 1;

  while (getline(file, line)) {

    name = line.substr(0, line.find(" "));

    score = stoi(line.substr(line.find(" ") + 1, line.length() - name.length() - 1));

    if (score > currentUser.score)

      currentUser.rank = rank;

      break;

    

    rank++;

  }

  file.close();

}

User currentUser;

void login () {

  string name;

  bool exists = false;

  fstream file("users.txt");

  while (!exists) {

    cout << "Enter your name: ";

    cin >> name;

    string line;

    while (getline(file, line)) {

      if (line.substr(0, name.length()) == name) {

        exists = true;

        currentUser.name = name;

        currentUser.score = stoi(line.substr(name.length() + 1, line.length() - name.length()));

        break;

      }

    }

    if (!exists) {

      cout << "User not found. Create user? (y/n)" << endl;

      char c = _getch();

      if (c == 'y') {

        User newUser;

        newUser.name = name;

        newUser.save();

        exists = true;

        currentUser = newUser;

      }

      system("cls");

    }

  }

  file.close();

  updateRanking();

  cout << "Welcome, " << currentUser.name << "! Your current score is: " << currentUser.score << endl;

  cout << "Your rank is: " << currentUser.rank << endl;

}

void runGame () {

  srand(time(NULL));

  Snake game;

  while (!game.gameOver) {

    game.draw();

    game.move();

    currentUser.score = game.score;

    currentUser.save();

  }

  cout << "Game Over" << endl;

}

int main () {

  login();

  runGame();

  return 0;

}

在这份代码中,我们新增了一个 `User` 类,这个类中我们定义了用户的相关信息,包括用户名、总得分、排名等。我们在其中实现了一个 `save` 方法,会将用户信息储存到本地文件中。

login() 函数中,我们新增了根据用户名查找用户的逻辑,并在没有找到用户的情况下,询问用户是否创建新用户。如果用户选择创建新用户,我们会新建一个 `User` 对象,并把它保存到本地文件中。

在游戏结束后,我们会将当前游戏的分数更新到当前用户的信息中,并将用户信息再次保存到本地,以便下次登录时能够知道当前用户的最新得分。

总结:

本文介绍了一款带用户登录的C++贪吃蛇游戏代码,以帮助开发者实现更多样化、更人性化的游戏功能。希望对您有所帮助!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复