21xrx.com
2025-04-28 14:06:56 Monday
文章检索 我的文章 写文章
C++编写简单游戏
2023-06-29 07:27:19 深夜i     13     0
C++ 游戏开发 程序设计 游戏制作 编程语言

C++是程序员们广泛使用的编程语言之一,它被用于开发各种类型的软件,包括游戏。C++在游戏开发中的应用非常广泛,并且是一种非常灵活和强大的语言,因此从基本到高级游戏都可以用C++编写。也有一些简单的游戏,可以用C++编写,这里介绍一个简单的游戏。

这个游戏名为“石头、剪刀、布”,它是一种非常简单的游戏,可以用C++编写。游戏玩法非常简单,两个人同时出示手势,谁赢了谁就会得到一分。玩家可以在控制台中输入他们的选择,而游戏会输出胜利者和比分。

这个游戏的基本代码如下:

#include<iostream>
#include<sstream>
#include<time.h>
using namespace std;
int main()
{
  int computer = 0;
  int player = 0;
  int i = 1;
  string choice;
  cout<<"Welcome to the rock-paper-scissors game!"<<endl;
  while(i <= 3)
  {
    srand((unsigned)time(NULL));
    computer = rand()%3 + 1;
    cout<<"Round "<<i<<endl;
    cout<<"Please enter your choice: rock, paper or scissors"<<endl;
    cin>>choice;
    if(choice == "rock")
    
      player = 1;
    
    else if(choice == "paper")
    
      player = 2;
    
    else if(choice == "scissors")
    
      player = 3;
    
    else
    
      cout<<"Invalid choice
    if((player == 1 && computer == 2) || (player == 2 && computer == 3) || (player == 3 && computer == 1))
    {
      cout<<"Computer wins!"<<endl;
      i++;
    }
    else if((player == 1 && computer == 3) || (player == 2 && computer == 1) || (player == 3 && computer == 2))
    {
      cout<<"Player wins!"<<endl;
      i++;
      player++;
    }
    else
    
      cout<<"Tie!"<<endl;
    
  }
  cout<<"Game over!"<<endl;
  if(computer > player)
  
    cout<<"Computer wins with "<<computer<<" points."<<endl;
  
  else if(computer < player)
  
    cout<<"Player wins with "<<player<<" points."<<endl;
  
  else
  
    cout<<"Tie game!"<<endl;
  
  return 0;
}

这个游戏代码将“石头、剪刀、布”游戏分为三个回合,并在每一回合要求玩家输入其选择。然后,用随机函数生成计算机的选择,并将玩家和计算机的选择进行比较。赢家会得到一分,如果玩家获胜,则玩家得分加一。游戏将最后三轮的得分总结起来,然后输出胜者。

  
  

评论区

请求出错了