1.用数组来 实现墙
2.用链表来 实现蛇
下面直接贴代码了。
(代码写的比较幼稚,也没有注释,大佬勿喷)
common.c
#include "common.h"
#include <windows.h>
char game[15][15] = {0};
void gotoxy(int x,int y)
{
COORD pos = {x,y};
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,pos);
}
common.h
#pragma once
#define CHAR_WALL ¡®*¡¯
#define CHAR_HEAD ¡¯@¡®
#define CHAR_BODY ¡®#¡¯
#define CHAR_FOOD ¡¯%¡®
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ROW 15
#define COL 15
char game[ROW][COL];
extern void gotoxy(int x,int y);
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include "wall.h"
#include "snake.h"
#include "common.h"
int main()
{
int is_start = 1;
int is_dead = 0;
while(is_start==1)
{
int is_dead = 0;
init_wall();
init_snake();
set_food();
system("cls");
draw();
while(0==is_dead)
{
char key = getch();
while(0==_kbhit())
{
if(72==key||80==key||75==key||77==key)
{
gotoxy(0,0);
if(1==snake_move(key))
{
draw();
Sleep(300);
}
else
{
is_dead = 1;
break;
}
}
}
}
}
return 0;
}
snake.c
#include "snake.h"
#include <stdio.h>
#include <stdlib.h>
#include "wall.h"
#include "common.h"
struct point *head = NULL;
static void add_point(int x,int y)
{
struct point *pnew=(point *)malloc(sizeof(point));
pnew->x=x;
pnew->y=y;
if(head==NULL)
{
head=pnew;
pnew->next = NULL;
}
else
{
set(head->x,head->y,'#');
pnew->next=head;
head = pnew;
}
set(head->x,head->y,'@');
}
void free_snake()
{
point *del = head;
while(del!=NULL)
{
head =head->next;
free(del);
del = head;
}
head = NULL;
}
void init_snake()
{
free_snake();
add_point(4, 2);
add_point(4, 3);
add_point(4, 4);
}
static void del_point()
{
if(head == NULL||head->next == NULL)
{
return;
}
point *pre = head;
point *tmp = head->next;
while(tmp->next!=NULL)
{
pre = pre->next;
tmp = tmp->next;
}
set(tmp->x,tmp->y,' ');
free(tmp);
tmp=NULL;
pre->next=NULL;
}
int snake_move(char key)
{
int i = head->x;
int j = head->y;
switch(key)
{
case 72 : --i;break;
case 80 : ++i;break;
case 75 : --j;break;
case 77 : ++j;break;
}
if(1==is_touch()||game[i][j]=='#')
{
return 0;
}
if(game[i][j]=='%')
{
add_point(i,j);
set_food();
}
if(1!=is_touch()&&game[i][j]!='#'&&game[i][j]!='%')
{
add_point(i,j);
del_point();
}
return 1;
}
snake.h
#pragma once
typedef struct point
{
int x,y;
struct point *next;
}point;
void init_snake();
void free_snake();
wall.h
#pragma once
extern void init_wall();
extern void draw();
extern void set(int i,int j,char ch);
extern char get(int i,int j);
extern int is_touch();
extern void set_food();
extern int get_food_x();
extern int get_food_y();
wall.c
#include "common.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int food_x,food_y;
void init_wall()
{
memset(game, 0, sizeof(game));
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(i==0||i==ROW-1||j==0||j==COL-1)
{
game[i][j]= '*';
}
else
{
game[i][j]=' ';
}
}
}
}
void draw()
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("%c ",game[i][j]);
}
printf("\n");
}
}
void set(int i,int j,char ch)
{
game[i][j]=ch;
}
char get(int i,int j)
{
return game[i][j];
}
int is_touch()
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(i==0||i==ROW-1||j==0||j==COL-1)
{
if(game[i][j]=='@')
return 1;
}
}
}
return 0;
}
void set_food()
{
while(1)
{
srand((unsigned int)time(NULL));
food_x=rand()%(15-2)+1;
food_y=rand()%(15-2)+1;
if(game[food_x][food_y]!='@'&&game[food_x][food_y]!='#')
{
game[food_x][food_y] = '%';
break;
}
}
}
int get_food_x()
{
return food_x;
}
int get_food_y()
{
return food_y;
}