| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1452 人关注过本帖
标题:【原创】贪吃蛇 大家进来提点意见
取消只看楼主 加入收藏
xxwpk007
Rank: 4
等 级:业余侠客
帖 子:166
专家分:289
注 册:2007-7-29
结帖率:100%
收藏
 问题点数:0 回复次数:2 
【原创】贪吃蛇 大家进来提点意见
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

#define TIME 200

void rect(void);
void snake_init(POINT *snake);
void gotoxy(int,int);

int main(void)
{
    int i,j,last_ch,num=0;
    int start,end;
    int ch = 'd';
    int food = 0;

    POINT snake[100];
    POINT snake_food;

    rect();

    snake_init(snake);
    start = 4;
    end = 0;
    gotoxy(snake[end].x,snake[end].y);
    printf("*****");

    while(1)
    {
        while(!kbhit())
        {
            switch(ch)
            {
                case 'a':
                    if(last_ch == 'd')
                    {
                        ch = last_ch;
                        break;
                    }
                    _sleep(TIME);
                    gotoxy(snake[end].x,snake[end].y);
                    printf(" ");
                    for(j=0;j < start;j++)
                        snake[j] = snake[j+1];
                    gotoxy(snake[start].x-1,snake[start].y);
                    printf("*");
                    snake[start].x -= 1;
                    break;
                case 'd':
                    if(last_ch == 'a')
                    {
                        ch = last_ch;
                        break;
                    }
                    _sleep(TIME);
                    gotoxy(snake[end].x,snake[end].y);
                    printf(" ");
                    for(j=0;j < start;j++)
                        snake[j] = snake[j+1];
                    gotoxy(snake[start].x+1,snake[start].y);
                    printf("*");
                    snake[start].x += 1;
                    break;
                case 's':
                    if(last_ch == 'w')
                    {
                        ch = last_ch;
                        break;
                    }
                    _sleep(TIME);
                    gotoxy(snake[end].x,snake[end].y);
                    printf(" ");
                    for(j=0;j < start;j++)
                        snake[j] = snake[j+1];
                    gotoxy(snake[start].x,snake[start].y+1);
                    printf("*");
                    snake[start].y += 1;
                    break;
                case 'w':
                    if(last_ch == 's')
                    {
                        ch = last_ch;
                        break;
                    }
                    _sleep(TIME);
                    gotoxy(snake[end].x,snake[end].y);
                    printf(" ");
                    for(j=0;j < start;j++)
                        snake[j] = snake[j+1];
                    gotoxy(snake[start].x,snake[start].y-1);
                    printf("*");
                    snake[start].y -= 1;
                    break;
                default:
                    ch = last_ch;
                    break;
            }

            /* 撞墙处理 */
            if(snake[start].x <= 0 || snake[start].x >= 42 || snake[start].y <=0 || snake[start].y >= 13)
            {
                gotoxy(17,5);
                printf("你输了\n");
                gotoxy(13,6);
                system("pause");

                gotoxy(0,0);
                rect();
                snake_init(snake);
                start = 4;
                end = 0;
                gotoxy(snake[end].x,snake[end].y);
                printf("*****");

                ch = 'd';
                food = 0;
                num = 0;
            }

            /* 撞到自己 */
            for(i=start-1;i >= 0;i--)
            {
                if(snake[start].x == snake[i].x && snake[start].y == snake[i].y)
                {
                    gotoxy(17,5);
                    printf("你输了\n");
                    gotoxy(13,6);
                    system("pause");

                    gotoxy(0,0);
                    rect();
                    snake_init(snake);
                    start = 4;
                    end = 0;
                    gotoxy(snake[end].x,snake[end].y);
                    printf("*****");

                    ch = 'd';
                    food = 0;
                    num = 0;
                }
            }

            /* 生成食物 */
            if(!food)
            {
                srand(time(0));
                snake_food.x = rand() % 40 + 2;
                snake_food.y = rand() % 11 + 2;
                gotoxy(snake_food.x,snake_food.y);
                printf("*");
                food = 1;
            }

            /* 吃到食物 */
            if(snake[start].x == snake_food.x && snake[start].y == snake_food.y)
            {
                start += 1;
                snake[start].x = snake_food.x;
                snake[start].y = snake_food.y;
                gotoxy(65,6);
                num += 10;
                printf("%d",num);
                food = 0;
            }
            last_ch = ch;
        }
        ch = getch();
    }
    
    return 0;
}

void gotoxy(int X, int Y)
{
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord;
    coord.X = X;
    coord.Y = Y;
    SetConsoleCursorPosition(
        hOutput,
        coord
        );
}

void rect(void)
{
    printf("┌────────────────────┐\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │         你的分数为  0\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("│                                        │\n");
    printf("└────────────────────┘\n");
}

void snake_init(POINT *snake)
{
    snake[0].x = 2;
    snake[0].y = 1;
    
    snake[1].x = 3;
    snake[1].y = 1;
    
    snake[2].x = 4;
    snake[2].y = 1;
    
    snake[3].x = 5;
    snake[3].y = 1;
    
    snake[4].x = 6;
    snake[4].y = 1;
}

图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 xxwpk007 于 2010-7-13 18:41 编辑 ]
搜索更多相关主题的帖子: 提点 贪吃 意见 
2010-07-13 16:27
xxwpk007
Rank: 4
等 级:业余侠客
帖 子:166
专家分:289
注 册:2007-7-29
收藏
得分:0 
回复 4楼 一路向西
可以控制的 w,s,a,d => 上,下,左,右
2010-07-14 08:30
xxwpk007
Rank: 4
等 级:业余侠客
帖 子:166
专家分:289
注 册:2007-7-29
收藏
得分:0 
回复 11楼 molin
用于记录蛇的坐标。
2010-07-14 17:21
快速回复:【原创】贪吃蛇 大家进来提点意见
数据加载中...
 
   



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

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