怎么编一个c++小游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么编一个c++小游戏相关的知识,希望对你有一定的参考价值。

#include "stdafx.h"
#include <boost\lexical_cast.hpp>
#include<stdlib.h>
#include<string.h>
#include <iostream>
#include <map>
#include<iterator>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include <cstring>
#include<stdio.h>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <fstream>
#include<iostream>
#include<cmath>
#include<ctime>
#include<windows.h>
#include <boost\lexical_cast.hpp>
#include <conio.h>
#include<dos.h>
#include<stdexcept>
using namespace std;
class JQYX

private:
int army_color;
string s;
int lever;
bool isvisiby;
bool ismove;
bool isjq;
public:
JQYX(int c = 0, string ss = "X", int l = 0, bool v = 0, bool m = 0, bool jq = 0) :
army_color(c), s(ss), lever(l), isvisiby(v), ismove(m), isjq(jq)
int getcolor()

return army_color;

bool jcvisiby()

return isvisiby;

void setvisiby(bool b)

isvisiby = b;

bool jcmove()

return ismove;

void setmove(bool b)

ismove = b;

bool jcisjq()

return isjq;

void setjq(bool b)

isjq = b;

friend bool swap(JQYX &j, JQYX& k,int w)

if ((j.getcolor()==w)&&(j.jcmove())&&((j.s == "炸"&&k.s=="司")|| j.s == k.s))

k.army_color = 2;
k.s = " ";
k.lever = -99;
k.isjq = 0;
k.ismove = 0;
k.isvisiby = 1;
j.army_color = 2;
j.s = " ";
j.lever = -99;
j.isjq = 0;
j.ismove = 0;
j.isvisiby = 1;

else

k.army_color = j.army_color;
k.s = j.s;
k.lever = j.lever;
k.isjq = j.isjq;
k.ismove = j.ismove;
k.isvisiby = j.isvisiby;
j.army_color = 2;
j.s = " ";
j.lever = -99;
j.isjq = 0;
j.ismove = 0;
j.isvisiby = 1;

return 0;

friend bool operator>(const JQYX& j1, const JQYX& j2)

return j1.lever > j2.lever;

friend ostream& operator<<(ostream& os, JQYX& j)

if (j.jcvisiby())
os << j.s;
else
cout << "XX ";
return os;

;
const JQYX l1(1, "军", 10, 0, 1, 0);
const JQYX l2(1, "师", 9, 0, 1, 0);
const JQYX l3(1, "旅", 8, 0, 1, 0);
const JQYX l4(1, "团", 7, 0, 1, 0);
const JQYX l5(1, "营", 6, 0, 1, 0);
const JQYX l6(1, "连", 5, 0, 1, 0);
const JQYX l7(1, "排", 4, 0, 1, 0);
const JQYX l8(1, "班", 3, 0, 1, 0);
const JQYX l9(1, "工", 2, 0, 1, 0);
const JQYX l0(1, "雷", 1, 0, 0, 0);
const JQYX l66(1, "炸", 66, 0, 1, 0);
const JQYX l88(1, "司", 11, 0, 1, 0);
const JQYX zl(0, "旗", 88, 0, 0, 0);
const JQYX h1(-1, "军", 10, 0, 1, 0);
const JQYX h2(-1, "师", 9, 0, 1, 0);
const JQYX h3(-1, "旅", 8, 0, 1, 0);
const JQYX h4(-1, "团", 7, 0, 1, 0);
const JQYX h5(-1, "营", 6, 0, 1, 0);
const JQYX h6(-1, "连", 5, 0, 1, 0);
const JQYX h7(-1, "排", 4, 0, 1, 0);
const JQYX h8(-1, "班", 3, 0, 1, 0);
const JQYX h9(-1, "工", 2, 0, 1, 0);
const JQYX h0(-1, "雷", 1, 0, 0, 0);
const JQYX h66(-1, "炸", 66, 0, 1, 0);
const JQYX h88(-1, "司", 11, 0, 1, 0);

void initializ(vector<JQYX> &v)

v.push_back(l1);
v.push_back(l2);
v.push_back(l3);
v.push_back(l4);
v.push_back(l5);
v.push_back(l6);
v.push_back(l7);
v.push_back(l8);
v.push_back(l9);
v.push_back(l0);
v.push_back(l66);
v.push_back(l88);
v.push_back(zl);
v.push_back(h1);
v.push_back(h2);
v.push_back(h3);
v.push_back(h4);
v.push_back(h5);
v.push_back(h6);
v.push_back(h7);
v.push_back(h8);
v.push_back(h9);
v.push_back(h0);
v.push_back(h66);
v.push_back(h88);

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bf;
INPUT_RECORD mouseRec;
DWORD res;
COORD crPos, crHome = 0,0 ;
void pressmouse()

while (true)

ReadConsoleInput(hin, &mouseRec, 1, &res);
if (mouseRec.EventType == MOUSE_EVENT)

if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)

if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)

break;



crPos = mouseRec.Event.MouseEvent.dwMousePosition;
GetConsoleScreenBufferInfo(h, &bf);
SetConsoleCursorPosition(h, crHome);
SetConsoleCursorPosition(h, bf.dwCursorPosition);
switch (mouseRec.Event.MouseEvent.dwButtonState)

case FROM_LEFT_1ST_BUTTON_PRESSED:

//左键选取
break;

case RIGHTMOST_BUTTON_PRESSED:

//右键比较大小,击杀
break;




void print(JQYX j[][5])

system("cls");
for (int ii = 0; ii != 5; ++ii)

for (int jj = 0; jj != 5; ++jj)

if (j[ii][jj].jcvisiby() && j[ii][jj].getcolor() == 1)
SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_RED);
if (j[ii][jj].jcvisiby() && j[ii][jj].getcolor() == -1)
SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
if (j[ii][jj].jcvisiby() && j[ii][jj].getcolor() == 0)
SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << j[ii][jj];
SetConsoleTextAttribute(h, 1 | 2 | 4);

cout << endl;


int main()

vector<JQYX> vjq(0);
initializ(vjq);
/*
for(vector<JQYX>::iterator it=vjq.begin();it!=vjq.end();++it)

cout << *it << " ";

*/
JQYX jq[5][5] = 0 ;
int sj = 0;
srand(unsigned(time(NULL)));
for (int i = 0; i != 25;)

sj = rand() % 25;
if (vjq[sj].jcisjq() == 0)

// cout << sj << " ";
jq[i / 5][i % 5] = vjq[sj];
vjq[sj].setjq(true);
++i;


//cout << endl;
print(jq);
/*
for (int ii = 0; ii != 5; ++ii)

for (int jj = 0; jj != 5; ++jj)

cout << jq[ii][jj] << " ";

cout << endl;

*/
char c;
int x, y;
int a, d;
int wanjia = -1;
while (cin.good())

if (wanjia==-1)
cout << "请红方进行操作" << endl;
else
cout << "请蓝方进行操作" << endl;
cout << "F/f 翻牌 M/m 移动" << endl;
cin >> c;
switch (c)

case 'F':
case 'f':

cout << "输入要翻牌的坐标:";
cin >> x >> y;
if(!jq[x - 1][y - 1].jcvisiby())
jq[x - 1][y - 1].setvisiby(true);
else
continue;
print(jq);
break;

case 'M':
case 'm':

cout << "输入要移动的坐标:";
cin >> x >> y;
cout << endl << "移动到哪里:";
cin >> a >> d;
if (jq[x - 1][y - 1] > jq[a - 1][d - 1])

swap(jq[x - 1][y - 1], jq[a - 1][d - 1],wanjia);
print(jq);

else

continue;

break;


wanjia *= -1;

参考技术A 可以用directx

结对-结对编项目作业名称-需求分析

初步设置一个游戏窗口,调节窗口大小

游戏的主界面:开始游戏

       重新开始游戏

       游戏结束

游戏流程:开始游戏的时候,会根据提示来选择游模式

如果选的对手是电脑。则会提示是哪个级别的。初级、中级和高级。

 系统会根据你下棋子来判断来怎么下。

在任何一方下到五个子的时候,系统会提示输赢,以及下棋过程中有无悔棋,和总共下了几次。

「初始化棋盘,加载棋盘图片, 点鼠标左键落子, 加载棋子图片到棋盘 ,五个连在一起就游戏结束。如果没有连在一起扫描棋盘,根据预设权值,选择最佳地点落子,加载棋子图片到棋盘。

以上是关于怎么编一个c++小游戏的主要内容,如果未能解决你的问题,请参考以下文章

用c/c++作为游戏代码编程的游戏引擎

《结对编项目作业名称-设计文档》

游戏开发需要的知识

结对-结对编项目作业名称-需求分析

结对-结对编项目作业名称-需求分析

用python编一个扔骰子猜大小的游戏,要求三局两胜制