鱼C论坛

 找回密码
 立即注册
查看: 4009|回复: 4

贪吃蛇程序。。。

[复制链接]
发表于 2012-2-20 12:52:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
// 99.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h> //使用当前时间做种子

enum dir{up,down,left,right};//枚举类型enum dir

//围墙
class Fence
{
public:
        void InitFence();
        void OutputF();
public:
        char game[20][20];
}f;
//画框
void Fence::InitFence()
{
        for(int i=0;i<20;i++)
                for(int j=0;j<20;j++)
                {
                        if(i==0||i==19||j==0||j==19)
                        {
                                game[i][j]='*';
                        }
                        else
                        {
                                game[i][j]=' ';
                        }
                }
}
//显示
void Fence::OutputF()
{
        for(int i=0;i<20;i++)
        {
                for(int j=0;j<20;j++)
                {
                        cout <<game[i][j]<<' ';
                        cout <<endl;
                }
        }
}
//蛇结点
class SnakeNode
{
private:
        int x,y;
        SnakeNode *prior,*next;
public:
        void add_head(int x,int y);
        int get_x();
        int get_y();
        void delete_tail();
}*head=NULL,*tail=NULL;
//插入头结点
void SnakeNode::add_head(int x,int y)
{
        SnakeNode *q=new SnakeNode;
        q->x=x;
        q->y=y;
        q->next=head;
        q->prior=NULL;
        if(head)
        {
                head->prior=q;
        }
        head = q;
        if(!tail)
        {
                tail = head;
        }
        f.game[x][y]='*';
}
int SnakeNode::get_x()
{
        return x;
}
int SnakeNode::get_y()
{
        return y;
}
//删除尾结点
void SnakeNode::delete_tail()
{
        SnakeNode *p=tail;
        f.game[tail->get_x()][tail->get_y()]=' ';//把尾结点
        //的坐标表示的‘*’置为空格
        if(tail==head)
        {
                tail=head=NULL;
        }
        else
        {
                tail=tail->prior;
                tail->next=NULL;
        }
        delete p;
}
//移动
class move
{
public:
        dir point;//枚举变量POINT控制方向
        int food_x;
        int food_y;
public:
        void moving();
        void change_point(char);//改变方向
        void get_food();
};

void move::moving()
{
        int a,b;
        a=head->get_x();//取得头结点横坐标
        b=head->get_y();
        switch(point)
        {
                case up:
                        --a;
                        break;
               
                case down:
                        ++a;
                        break;
                       
                case left:
                        --b;
                        break;
                       
                case right:
                        ++b;
                        break;
        }
        if(a==19||b==19||a==0||b==0)
        {
                cout<<"game over!!!!"<<endl;
                exit(0);
        }
        if(a==food_x&&b==food_y)
        {
                head->add_head(a,b);
                get_food();
        }
        else
        {
                head->add_head(a,b);//插入头结点
                head->delete_tail();//删除尾结点
               
        }
}
void move::change_point(char keydown)
{
        switch(keydown)
        {
                case 'w':
                        point=up;
                        break;
               
                case 's':
                        point=down;
                        break;
                       
                case 'a':
                        point=left;
                        break;
                       
                case 'd':
                        point=right;
                        break;
                       
        }
}

void move::get_food()
{
        srand((unsigned int)time(NULL));//程序运行时间
        food_x=rand()%18+1;
        food_y=rand()%18+1;
        f.game[food_x][food_y]='*';
}

int main(int argc,char argv[])
{
        move m;
        f.InitFence();
        head->add_head(4,3);
        m.get_food();
        f.OutputF();
        while(true)
        {
                char keydown=getch();//getch()返回键盘上读取的字符
                //包含头文件<conio.h>
                m.change_point(keydown);
                while(!kbhit())
                {
                        system("cls");
                        m.moving();
                        f.OutputF();
                        Sleep(200);
                }
        }
        return 0;
}

程序是网上找的,编译没错误,但是运行后只会出现一列*号,不知道要怎么修改
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-20 22:27:34 | 显示全部楼层
我运行了一下!应该是#include "stdafx.h"出了问题!!
stdafx文件的缺失导致了程序的无法运行

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-20 22:59:35 | 显示全部楼层
这个文件那里有呢
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-20 23:32:12 | 显示全部楼层

  1. #include <iostream.h>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. #include <time.h> //使用当前时间做种子

  7. enum dir{up,down,left,right};//枚举类型enum dir

  8. //围墙
  9. class Fence
  10. {
  11. public:
  12.         void InitFence();
  13.         void OutputF();
  14. public:
  15.         char game[20][20];
  16. }f;
  17. //画框
  18. void Fence::InitFence()
  19. {
  20.         for(int i=0;i<20;i++)
  21.                 for(int j=0;j<20;j++)
  22.                 {
  23.                         if(i==0||i==19||j==0||j==19)
  24.                         {
  25.                                 game[i][j]='*';
  26.                         }
  27.                         else
  28.                         {
  29.                                 game[i][j]=' ';
  30.                         }
  31.                 }
  32. }
  33. //显示
  34. void Fence::OutputF()
  35. {
  36.         for(int i=0;i<20;i++)
  37.         {
  38.                 for(int j=0;j<20;j++)
  39.                 {
  40.                         cout <<game[i][j]<<' ';
  41.                 }
  42.                 cout <<endl;
  43.         }
  44. }
  45. //蛇结点
  46. class SnakeNode
  47. {
  48. private:
  49.         int x,y;
  50.         SnakeNode *prior,*next;
  51. public:
  52.         void add_head(int x,int y);
  53.         int get_x();
  54.         int get_y();
  55.         void delete_tail();
  56. }*head=NULL,*tail=NULL;
  57. //插入头结点
  58. void SnakeNode::add_head(int x,int y)
  59. {
  60.         SnakeNode *q=new SnakeNode;
  61.         q->x=x;
  62.         q->y=y;
  63.         q->next=head;
  64.         q->prior=NULL;
  65.         if(head)
  66.         {
  67.                 head->prior=q;
  68.         }
  69.         head = q;
  70.         if(!tail)
  71.         {
  72.                 tail = head;
  73.         }
  74.         f.game[x][y]='*';
  75. }
  76. int SnakeNode::get_x()
  77. {
  78.         return x;
  79. }
  80. int SnakeNode::get_y()
  81. {
  82.         return y;
  83. }
  84. //删除尾结点
  85. void SnakeNode::delete_tail()
  86. {
  87.         SnakeNode *p=tail;
  88.         f.game[tail->get_x()][tail->get_y()]=' ';//把尾结点
  89.         //的坐标表示的‘*’置为空格
  90.         if(tail==head)
  91.         {
  92.                 tail=head=NULL;
  93.         }
  94.         else
  95.         {
  96.                 tail=tail->prior;
  97.                 tail->next=NULL;
  98.         }
  99.         delete p;
  100. }
  101. //移动
  102. class move
  103. {
  104. public:
  105.         dir point;//枚举变量POINT控制方向
  106.         int food_x;
  107.         int food_y;
  108. public:
  109.         void moving();
  110.         void change_point(char);//改变方向
  111.         void get_food();
  112. };

  113. void move::moving()
  114. {
  115.         int a,b;
  116.         a=head->get_x();//取得头结点横坐标
  117.         b=head->get_y();
  118.         switch(point)
  119.         {
  120.                 case up:
  121.                         --a;
  122.                         break;
  123.                
  124.                 case down:
  125.                         ++a;
  126.                         break;
  127.                         
  128.                 case left:
  129.                         --b;
  130.                         break;
  131.                         
  132.                 case right:
  133.                         ++b;
  134.                         break;
  135.         }
  136.         if(a==19||b==19||a==0||b==0)
  137.         {
  138.                 cout<<"game over!!!!"<<endl;
  139.                 exit(0);
  140.         }
  141.         if(a==food_x&&b==food_y)
  142.         {
  143.                 head->add_head(a,b);
  144.                 get_food();
  145.         }
  146.         else
  147.         {
  148.                 head->add_head(a,b);//插入头结点
  149.                 head->delete_tail();//删除尾结点
  150.                
  151.         }
  152. }
  153. void move::change_point(char keydown)
  154. {
  155.         switch(keydown)
  156.         {
  157.                 case 'w':
  158.                         point=up;
  159.                         break;
  160.                
  161.                 case 's':
  162.                         point=down;
  163.                         break;
  164.                         
  165.                 case 'a':
  166.                         point=left;
  167.                         break;
  168.                         
  169.                 case 'd':
  170.                         point=right;
  171.                         break;
  172.                         
  173.         }
  174. }

  175. void move::get_food()
  176. {
  177.         srand((unsigned int)time(NULL));//程序运行时间
  178.         food_x=rand()%18+1;
  179.         food_y=rand()%18+1;
  180.         if (f.game[food_x][food_y]!='*') //做下判断随机产生*是否与初始化重复
  181.         {
  182.             f.game[food_x][food_y] = '*';
  183.         }
  184.         else
  185.         {
  186.             get_food();
  187.         }
  188. }

  189. int main(int argc,char argv[])
  190. {
  191.         move m;
  192.         f.InitFence();
  193.         head->add_head(4,3);
  194.         m.get_food();
  195.         f.OutputF();
  196.         while(true)
  197.         {
  198.                 char keydown=getch();//getch()返回键盘上读取的字符
  199.                 //包含头文件<conio.h>
  200.                 m.change_point(keydown);
  201.                 while(!kbhit())
  202.                 {
  203.                         m.moving();
  204.                         system("cls");
  205.                         
  206.                         f.OutputF();
  207.                         Sleep(200);
  208.                         
  209.                 }
  210.         }
  211.         return 0;
  212. }

复制代码
伤眼睛啊 sleep控制速度的
这里用了system("cls")我觉得不好
这里如果设计外面边框静态显示
里面东西动态显示就不会显得那么晃了

            
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-20 23:33:13 | 显示全部楼层

这是头文件出错了,头文件是C的格式
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-11 11:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表