跪求C++编写的贪吃蛇程序啊!!!!
c++吧
全部回复
仅看楼主
level 1
阴霾12 楼主
大侠
帮帮忙啊!!
2010年12月15日 15点12分 1
level 6
srand(time(0));
r=(rand()%(row-2))+1; //横坐标
l=(rand()%(line-2))+1;//竖坐标
//如果随机产生的坐标不是蛇身,则可行
//否则重新产生坐标
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇头和社颈的横坐标不相同,执行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否则,如下在原本方向上继续移动
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头
{
//删除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇头
secondSign=firstSign;
//压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//没有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
main.cpp
#include <iostream>
#include "snake.h"
#include <windows.h>
using namespace std;
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}

2010年12月16日 00点12分 3
level 1
阴霾12 楼主
回复:3楼
谢谢大哥
能不能发给我下
[email protected]
2010年12月19日 15点12分 4
level 12
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
难道这就是传说中的暴力刷屏么?强大!
2010年12月19日 16点12分 5
level 12
前几天刚学了gotoxy,今天太晚了,明天我也编一个好了
2010年12月19日 16点12分 6
level 12
//随手编了一个,VS2010与gcc 4.5编译通过(8过也没用C++0x的东西)
//用了WinAPI实现gotoxy,GetKeyState没用。对于API的原则是能少用则少用,能不用就不用
//边框什么的没画,要画也很简单,不过我懒得画了
#define WIN32_LEAN_AND_MEAN      //忽略一些windows.h中定义的函数,使编译时快些
#include <iostream>
#include <deque>
#include <algorithm>
#include <ctime>
#include <windows.h>
#include <conio.h>
#define WIDTH         40
#define HEIGHT         20
#define WAIT_TIME     300     //ms
using namespace std;
class Snake
{
public:
     struct Position: COORD
     {
         Position()
         {
             X=Y=0;
         }
         Position(short x, short y)
         {
             operator ()(x, y);
         }
         Position operator +(const Position &pos) const
         {
             return Position(X+pos.X,Y+pos.Y);
         }
         bool operator ==(const Position &pos) const
         {
             return X==pos.X && Y==pos.Y;
         }
         bool Out_of_Range()
         {
             return X>WIDTH || Y>HEIGHT || X<0 || Y<0;
         }
         Position& operator ()(short x, short y)
         {
             X=x;
             Y=y;
             return *this;
         }
     };
private:
     deque<Position> Body;
     const HANDLE StdHandle;
     Position Food;
private:
     void GenerateFood()
     {
         while (find(Body.begin(), Body.end(), Food(rand()%WIDTH, rand()%HEIGHT))!=Body.end());

2010年12月20日 01点12分 7
level 12
         SetConsoleCursorPosition(StdHandle, Food);
         cout.put('*');
     }
public:
     Snake(): StdHandle(GetStdHandle(STD_OUTPUT_HANDLE))
     {
         Position pos;
         system("CLS");
         Body.push_back(pos(1,0));
         Body.push_back(pos(0,0));
         cout << "#@";
         GenerateFood();
     }
     bool Go(Position pos)
     {
         pos=pos+Body.front();
         if (pos==Food)
             GenerateFood();
         else
         {
             if (pos.Out_of_Range() || find(Body.begin(), Body.end(), pos)!=Body.end())
                 return true;
             SetConsoleCursorPosition(StdHandle, Body.back());
             cout.put(' ');
             Body.pop_back();
         }
         SetConsoleCursorPosition(StdHandle, Body.front());
         cout.put('#');
         SetConsoleCursorPosition(StdHandle, pos);
         cout.put('@');
         Body.push_front(pos);
         return false;
     }
     unsigned GetLength()
     {
         return Body.size();
     }
};
const Snake::Position& GetDirection()
{
     typedef Snake::Position Pos;
     static Pos pos( 1, 0);
     Sleep(WAIT_TIME);
     if (!_kbhit())
         return pos;
     if (_getch()!=0xE0)
         exit(0);
     switch (_getch())
     {
     case 'H':     return pos=Pos( 0, -1);         //UP
     case 'P':     return pos=Pos( 0, 1);         //DOWN
     case 'K':     return pos=Pos(-1, 0);         //LEFT
     case 'M':     return pos=Pos( 1, 0);         //RIGHT
     default:
         exit(0);
     }
}
int main()
{
     srand((unsigned)time(NULL));
     Snake snake;
     while (!snake.Go(GetDirection()));
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Snake::Position(0, HEIGHT+1));
     cout << "蛇身长度为" << snake.GetLength() << endl;
     system("PAUSE");
     return 0;
}

2010年12月20日 01点12分 8
level 12
2010年12月20日 01点12分 9
level 12
if (_getch()!=0xE0)
          exit(0);
这两句是什么意思
2010年12月20日 03点12分 10
level 12
用_getch()检测方向键时,要读取两次。第一次_getch()返回固定的0xE0(224),非0xE0表示按了其他键,则程序结束;第二次_getch()返回真正的方向信息
2010年12月20日 03点12分 11
level 6
有米有搞错,原来你是小吧,我只是对你的代码有些疑问而已,咱们可以讨论,删我楼什么意思?
2010年12月20日 03点12分 14
level 12
回复:14楼
楼是我删的。
看乃说CUI不能编游戏,所以手抖了一下
2010年12月20日 04点12分 16
level 6
回复:16楼
我说了吗?你还原看我怎么说的
2010年12月20日 04点12分 17
level 6
我用win2 application编译说我连接错误,
2010年12月20日 05点12分 18
level 6
我看到了#include <windows.h>,我以为是窗口图形界面的。。
这个cui是什么
2010年12月20日 05点12分 19
level 4
CUI : Command User Interface 命令行用户接口
应该在控制台下创建程序
2010年12月20日 05点12分 20
level 6
好吧,谁能告诉我应该新建一个什么样的工程来编译运行二楼那个程序
2010年12月20日 05点12分 21
level 6
请原谅我的愚蠢[拍砖]
2010年12月20日 05点12分 22
1