标题:求助!我做了一个贪吃蛇但是围墙和蛇都在闪,求令围墙不闪的方法~
只看楼主
jayzeng
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-16
 问题点数:0 回复次数:2 
求助!我做了一个贪吃蛇但是围墙和蛇都在闪,求令围墙不闪的方法~
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>



#define N 20

int SPEED;

int game[N][N];

enum
{
    UP,DOWN,LEFT,RIGHT
}point;

typedef struct snake
{
    struct snake* perior;
    struct snake* next;
        int x;
        int y;
}snake;
snake *head,*tail;

struct Foodinfo
{
    int food_x;
    int food_y;
}food;

void greate_food()
{
    srand((unsigned)time(NULL));
    do
    {
    food.food_x=rand()%18;
    food.food_y=rand()%18;
    }while(1==game[food.food_x][food.food_y]);
    game[food.food_x][food.food_y]=1;
}
void add_head(int a,int b)
{
    snake *temp=(struct snake*)malloc(sizeof(struct snake));
    temp->x=a;
    temp->y=b;
    temp->perior =NULL;
    if(head==NULL)
    {
        head=tail=temp;
        temp->perior=NULL;
    }
    else
    {
        head->perior = temp;
        temp->next = head;
        head=head->perior;
    }
    game[a][b]=1;
}

void delete_tail()
{
    snake *temp=tail;
    game[tail->x][tail->y]=0;
    tail=temp->perior;
    tail->next=NULL;
    free(temp);
}

void move()
{
    void output();
    void greate_food();   
    int    a=head->x;
    int    b=head->y;
   
    switch(point)
    {
        case UP:--a;break;
        case DOWN:++a;break;
        case LEFT:--b;break;
        case RIGHT:++b;break;
    }

    if(1==game[a][b]&& a !=food.food_x && b!=food.food_y)
    {
        output();
        printf("YOU ARE LOSE !\n");
        exit(0);
    }
    if(a==food.food_x && b==food.food_y)
    {
        add_head(a,b);
        greate_food();
        return ;
    }

    add_head(a,b);
    delete_tail();

}

void change_point(char keydown)
{
   
    switch(keydown)
    {
        case 'w':
        case 'W':point=UP;break;
        case 's':
        case 'S':point=DOWN;break;
        case 'a':
        case 'A':point=LEFT;break;
        case 'd':
        case 'D':point=RIGHT;break;
    }
}

void init()
{
    int i;
    for(i=0;i<=N-1;i++)
    {
        game[i][0]=game[i][N-1]=1;
        game[0][i]=game[N-1][i]=1;
    }
    head=tail;
    point=RIGHT;
    add_head(6,4);
    add_head(6,5);
    add_head(6,6);
}

void output()
{
    int i,j;
    for(i=0;i<=    N-1;i++)
    {
        for(j=0;j<=N-1;j++)
        {
            if(1==game[i][j])
                printf("* ");
            else
                printf("  ");
        }
            printf("\n");
    }
}



int main()
{
    char level;
c1:    printf("请选择游戏难度:\n 简单: 1 / 普通: 2 / 困难: 3\n");
        level=getch();
    switch(level)
    {
    case '1':SPEED=500;break;
    case '2':SPEED=200;break;
    case '3':SPEED=50;break;
    default :
        {
            system("cls");
            printf("请输入正确的选项。\n");
            goto c1;
        }
    }
    init();
    greate_food();
    output();
    printf("操作方法:W ↑S ↓A ←D →\n按任意键开始游戏");
    while(1)
    {
        char keydown=getch();
        change_point(keydown);
        while(!kbhit())
        {   
                system("cls");
            move();
            output();
            _sleep(SPEED);
        }
    }
}
搜索更多相关主题的帖子: game head include 贪吃蛇 
2012-06-02 00:26
奔放蜗牛
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-6-5
得分:0 
大哥  你能简单的解释一下你写的程序吗?就是在必要的行上加上行注释
2012-06-24 18:51
zhangqi_gsts
Rank: 6Rank: 6
来 自:甘肃天水
等 级:侠之大者
威 望:1
帖 子:227
专家分:457
注 册:2011-3-27
得分:0 
每移动一次你就清屏,果断闪
2012-06-25 14:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-368499-1-1.html




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

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