| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1434 人关注过本帖
标题:用C++写个简单的贪吃蛇游戏
只看楼主 加入收藏
爱上对方法国
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2010-10-28
结帖率:66.67%
收藏
已结贴  问题点数:29 回复次数:3 
用C++写个简单的贪吃蛇游戏
设计一个简化的贪吃蛇程序                                    

>         
>  *      
>         
>         
>         
>       S   
>         
1.随机在地图中产生一个星号,代表蛇的食物。

2.手控  用wasd四个字母键 代表上下左右。

3.S字母代表蛇,使用wasd进行手动控制蛇的移动。

4.S碰到食物时,再随机生成另一个食物。

5.注意不要求蛇的身体动态加长以及蛇身的惯性移动。



我是初学者,请各位帮忙做下,谢谢了
搜索更多相关主题的帖子: 贪吃 游戏 
2010-11-28 11:04
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:15 
#include <iostream.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <stdio.h>

const int LENGTH = 25;//最大的图


class SNAKE
{
public:
    SNAKE():size(10),s1(1),s2(1){}
    int step();
    int init();
    int show();
    int change();
private:
    int size;
    int table[LENGTH][LENGTH];
    int s1, s2;//初始位置
    int x, y;//食物
};
int SNAKE::init()
{
    int i=0, j=0;
    for(i=0; i<=size; ++i)
        for( j=0; j<=size; ++j )
            table[i][j] = 0;
            
    for( i=0; i<=size; ++i )
    {
        table[0][i] = 1;
        table[i][size] = 1;
        table[i][0] = 1;
        table[size][i] = 1;
    }
   
    return 0;
}
int SNAKE::change()
{
    x = rand()%(size-1) + 1;
    y = rand()%(size-1) + 1;
    table[x][y] = 2;
    return 0;
}
int SNAKE::step()
{
    int m;
    while( s2!=y || s1!=x )
    {
        system("cls");
        table[s1][s2] = 'S';
        show();
        m = getch();   
        if( m=='w' )
        {
            table[s1][s2] = 0;
            --s1;
            if( s1==0 )
            {
                s1 = size-1;
            }
            table[s1][s2] = 'S';
        }
        else if( m=='s' )
        {
            table[s1][s2] = 0;
            ++s1;
            if( s1==size )
            {
                s1 = 1;
            }
            table[s1][s2] = 'S';
        }
        else if( m=='a' )
        {
            table[s1][s2] = 0;
            --s2;
            if( s2==0 )
            {
                s2 = size-1;
            }
            table[s1][s2] = 'S';
        }
        else if( m=='d' )
        {
            table[s1][s2] = 0;
            ++s2;
            if( s2==size )
            {
                s2 = 1;
            }
            table[s1][s2] = 'S';
        }
        system("cls");
        show();
    }
    return 0;
}
int SNAKE::show()
{
    int i, j;
    for(i=0; i<=size; ++i)
    {
        for( j=0; j<=size; ++j )
            printf("%c", table[i][j]);
        printf("\n");
    }

    return 0;
}
int main()
{
    SNAKE s;
    s.init();
    srand(time(0));
    s.change();
    while(1)
    {
        if(!s.step())
        {   
            s.change();
        }
    }
    return 0;
}
2010-12-04 19:09
linshijin
Rank: 2
来 自:厦门
等 级:论坛游民
帖 子:40
专家分:24
注 册:2010-12-8
收藏
得分:15 
高手!!领教了

情不知所起,一往情深
2010-12-08 14:23
阿波罗57
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-6-30
收藏
得分:0 
为什么我用c++6.0运行就是这样的错误呢


--------------------Configuration: 12 - Win32 Debug--------------------
Compiling...
12.c
c:\users\16253\desktop\12.c(10) : error C2061: syntax error : identifier 'SNAKE'
c:\users\16253\desktop\12.c(10) : error C2059: syntax error : ';'
c:\users\16253\desktop\12.c(11) : error C2449: found '{' at file scope (missing function header?)
c:\users\16253\desktop\12.c(23) : error C2059: syntax error : '}'
c:\users\16253\desktop\12.c(41) : error C2143: syntax error : missing '{' before ':'
c:\users\16253\desktop\12.c(41) : error C2059: syntax error : ':'
c:\users\16253\desktop\12.c(48) : error C2143: syntax error : missing '{' before ':'
c:\users\16253\desktop\12.c(48) : error C2059: syntax error : ':'
c:\users\16253\desktop\12.c(102) : error C2143: syntax error : missing '{' before ':'
c:\users\16253\desktop\12.c(102) : error C2059: syntax error : ':'
c:\users\16253\desktop\12.c(116) : error C2065: 'SNAKE' : undeclared identifier
c:\users\16253\desktop\12.c(116) : error C2146: syntax error : missing ';' before identifier 's'
c:\users\16253\desktop\12.c(116) : error C2065: 's' : undeclared identifier
c:\users\16253\desktop\12.c(117) : error C2224: left of '.init' must have struct/union type
c:\users\16253\desktop\12.c(119) : error C2224: left of '.change' must have struct/union type
c:\users\16253\desktop\12.c(122) : error C2224: left of '.step' must have struct/union type
c:\users\16253\desktop\12.c(124) : error C2224: left of '.change' must have struct/union type
执行 cl.exe 时出错.

12.obj - 1 error(s), 0 warning(s)
2017-06-30 15:12
快速回复:用C++写个简单的贪吃蛇游戏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021665 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved