| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:我想了很久了,新手,给点意见,贪吃蛇还是没做出来
取消只看楼主 加入收藏
cjyzwg
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-11-18
结帖率:100%
收藏
 问题点数:0 回复次数:1 
我想了很久了,新手,给点意见,贪吃蛇还是没做出来
程序代码:
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#define N 200
int map[21][11] = { 0 };

 struct Snake
{
    int x[N];
    int y[N];
    int node;
}snake;
void gotoxy(int x, int y)
{
    COORD c;
    c.X = x - 1;
    c.Y = y - 1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void draw(int show[21][11])
{
    int i, j;
    for (i = 0; i < 21; i++)
    {
        for (j = 0; j < 11; j++)
        {
            if (i == 0 && j == 0 || i == 0 && j == 10 || i == 20 && j == 0 || i == 20 && j == 10)
                show[i][j] = 0;
            else if (j == 0 || j == 10)
                show[i][j] = 1;
            else if (i == 0 || i == 20)
                show[i][j] = 2;
            else
                show[i][j] = 0;
            if (show[i][j] == 0)printf("  ");
            if (show[i][j] == 1) printf("|");
            if (show[i][j] == 2)printf("--");
            if (show[i][j] == 3)printf("");
            if (show[i][j] == 4)printf("");
        }
        printf("\n");
    }
}
    void gameover()
    {
        int i, j;
        for (i = 1; i < 19;i++)
        for (j = 1; j < 9; j++)
            printf("");
        gotoxy(4,10);
        printf("game over");
        gotoxy(1,20);
        exit(0);
    }
    void main()
    {
        int i, a, b;
        snake.x[0] = 10; snake.y[0] = 6;
        snake.x[1] = 9; snake.y[1] = 6;
        snake.node = 2;
        map[snake.x[0]][snake.y[0]] = 3;
        map[snake.x[1]][snake.y[1]] = 3;
        draw(map);
        while (1)
            switch (_getch())
        {
            case'A':while (!_kbhit())
            {
                        if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]][snake.y[0] - 1] == 4)
                        {
                            srand(time(NULL));
                            a = rand() % 18 + 1;
                            b = rand() % 8 + 1;
                            snake.node++;
                            map[snake.x[0]][snake.y[0] - 1] = 0;
                            if (snake.y[0] > 1 && map[snake.x[0]][snake.y[0] - 1] == 0)
                            {
                                for (i = snake.node - 1; i > 0; i--)
                                {
                                    snake.x[i] = snake.x[i - 1];
                                    snake.y[i] = snake.y[i - 1];
                                }
                                for (i = 0; i < snake.node; i++)
                                    map[snake.x[0]][snake.y[0]] = 3;
                            }

                            else gameover();
                        }
                        else
                        if (snake.y[0] > 1 && map[snake.x[0]][snake.y[0] - 1] == 0)
                        {
                            for (i = snake.node - 1; i > 0; i--)
                            {
                                snake.x[i] = snake.x[i - 1];
                                snake.y[i] = snake.y[i - 1];
                            }
                            for (i = 0; i < snake.node; i++)
                                map[snake.x[0]][snake.y[0]] = 3;
                        }
                        else gameover();
            }break;
            case'D':while (!_kbhit())
            {
                        if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]][snake.y[0] + 1] == 4)
                        {
                            srand(time(NULL));
                            a = rand() % 18 + 1;
                            b = rand() % 8 + 1;
                            snake.node++;
                            map[snake.x[0]][snake.y[0] + 1] = 0;
                            if (snake.y[0] < 9 && map[snake.x[0]][snake.y[0] + 1] == 0)
                            {
                                for (i = snake.node - 1; i > 0; i--)
                                {
                                    snake.x[i] = snake.x[i - 1];
                                    snake.y[i] = snake.y[i - 1];
                                }
                                for (i = 0; i < snake.node; i++)
                                    map[snake.x[0]][snake.y[0]] = 3;
                            }

                            else gameover();
                        }
                        else
                        if (snake.y[0] < 9 && map[snake.x[0]][snake.y[0] + 1] == 0)
                        {
                            for (i = snake.node - 1; i > 0; i--)
                            {
                                snake.x[i] = snake.x[i - 1];
                                snake.y[i] = snake.y[i - 1];
                            }
                            for (i = 0; i < snake.node; i++)
                                map[snake.x[0]][snake.y[0]] = 3;
                        }
                        else gameover();
            }break;
            case'W':while (!_kbhit())
            {
                        if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]-1][snake.y[0]] == 4)
                        {
                            srand(time(NULL));
                            a = rand() % 18 + 1;
                            b = rand() % 8 + 1;
                            snake.node++;
                            map[snake.x[0]-1][snake.y[0]] = 0;
                            if (snake.x[0] > 1 && map[snake.x[0]-1][snake.y[0]] == 0)
                            {
                                for (i = snake.node - 1; i > 0; i--)
                                {
                                    snake.x[i] = snake.x[i - 1];
                                    snake.y[i] = snake.y[i - 1];
                                }
                                for (i = 0; i < snake.node; i++)
                                    map[snake.x[0]][snake.y[0]] = 3;
                            }

                            else gameover();
                        }
                        else
                        if (snake.x[0] > 1 && map[snake.x[0]-1][snake.y[0]] == 0)
                        {
                            for (i = snake.node - 1; i > 0; i--)
                            {
                                snake.x[i] = snake.x[i - 1];
                                snake.y[i] = snake.y[i - 1];
                            }
                            for (i = 0; i < snake.node; i++)
                                map[snake.x[0]][snake.y[0]] = 3;
                        }
                        else gameover();
            }break;
            case'S':while (!_kbhit())
            {
                        if (map[snake.x[0]][snake.y[0]] == 3 && map[snake.x[0]+1][snake.y[0]] == 4)
                        {
                            srand(time(NULL));
                            a = rand() % 18 + 1;
                            b = rand() % 8 + 1;
                            snake.node++;
                            map[snake.x[0]+1][snake.y[0]] = 0;
                            if (snake.x[0] < 19 && map[snake.x[0]+1][snake.y[0]] == 0)
                            {
                                for (i = snake.node - 1; i > 0; i--)
                                {
                                    snake.x[i] = snake.x[i - 1];
                                    snake.y[i] = snake.y[i - 1];
                                }
                                for (i = 0; i < snake.node; i++)
                                    map[snake.x[0]][snake.y[0]] = 3;
                            }

                            else gameover();
                        }
                        else
                        if (snake.x[0] < 19 && map[snake.x[0]+1][snake.y[0]] == 0)
                        {
                            for (i = snake.node - 1; i > 0; i--)
                            {
                                snake.x[i] = snake.x[i - 1];
                                snake.y[i] = snake.y[i - 1];
                            }
                            for (i = 0; i < snake.node; i++)
                                map[snake.x[0]][snake.y[0]] = 3;
                        }
                        else gameover();
            }break;
            default:printf("error\n");
        }
    }
    
搜索更多相关主题的帖子: 贪吃蛇 color 
2015-01-15 14:09
cjyzwg
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2014-11-18
收藏
得分:0 
回复 楼主 cjyzwg
还有排版程序的话,新手不太会,可以有指导的吗,谢谢
2015-01-15 14:12
快速回复:我想了很久了,新手,给点意见,贪吃蛇还是没做出来
数据加载中...
 
   



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

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