笔者以前写过的贪吃蛇。。。。。感觉一般性虽然是C++的但是可以玩玩 嘿嘿嘿 大家可以自己尝试一下(如有雷同 我也不知道。。。。。)
#include <stdio.h>
#include <conio.h>#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define ESC 27
#define SPACE 32
struct node{
int x;
int y;
struct node *next;
};
struct qwe{
struct node *head;
struct node *tail;
int length;
};
struct qwe snake;
void gotoxy(int x,int y)
{
COORD c;
c.X = y;
c.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void enqueue(int x,int y)
{
struct node * p;
p = (struct node*)malloc(sizeof(struct node));
p->x = x;
p->y = y;
snake.head->next = p;
snake.head = p;
p->next = NULL;
snake.length++;
}
void dequeue()
{
struct node * p = snake.tail;
p->next->x = p->x;
p->next->y = p->y;
snake.tail = snake.tail->next;
snake.length--;
free(p);
}
void destroy_queue()
{
struct node * p;
while(p = snake.tail){
snake.tail = snake.tail->next;
free(p);
}
}
void snake_init()
{
int i;
snake.head=snake.tail=(struct node*)malloc(sizeof(struct node));
snake.head->next = NULL;
snake.length = 0;
for(i = 0;i < 3;i++){
enqueue(1,i + 1);
}
}
void show_food()
{
struct node* p;
srand(time(NULL));
while(1){
snake.tail->x = rand() % (23-1) + 1;
snake.tail->y = rand() % (50-1) + 1;
p = snake.tail->next;
while(p){
if(snake.tail->x == p->x && snake.tail->y == p->y){ break; }
else{ p = p->next; }
}
if( !p){ break; }
}
gotoxy(snake.tail->x,snake.tail->y);
putchar('\3');
}
int judge()
{
struct node * p;
if(snake.length >= 50){
system("cls");
gotoxy(4,20);
puts("恭喜你,赢了!!!");
exit(0);
}
p = snake.tail->next;
while(snake.head != p){
if(snake.head->x == p->x && snake.head->y == p->y){
break;
}
else{
p = p->next;
}
}
if(snake.head == p){
if(snake.head->x >= 1 && snake.head->y >= 1 && snake.head->x < 23 && snake.head->y < 50){
return 1;
}
}
system("cls");
gotoxy(4,20);
puts("GAME OVER!!!");
destroy_queue();
getch();
exit(0);
}
void drow_wall()
{
int i;
for(i = 0; i <= 50; i++){
gotoxy(0,i);
putchar('\3');
gotoxy(23,i);
putchar('\3');
if(i <= 23){
gotoxy(i,0);
putchar('\3');
gotoxy(i,50);
putchar('\3');
}
}
}
void drow_snake(int i)
{
struct node * p;
p = snake.head;
gotoxy(p->x,p->y);
if(i){ putchar('\3'); }
else{ putchar('\3'); }
}
void clear_snake_tail()
{
struct node * p = snake.tail->next;
gotoxy(p->x,p->y);
putchar(' ');
p = p->next;
gotoxy(p->x,p->y);
putchar('\3');
}
void snake_auto_move(char temp)
{
int x,y,speed;
do{
clear_snake_tail();
drow_snake(1);
x = snake.head->x;
y = snake.head->y;
switch(temp){
case 'w': x--;break;
case 's': x++;break;
case 'a': y--;break;
case 'd': y++;
}
enqueue(x,y);
if(snake.tail->x == x && snake.tail->y == y){
show_food();
}
else{ dequeue(); }
drow_snake(0);
speed = -2.3 * snake.length + 157;
Sleep(speed);
}while(!_kbhit() && judge());
}
void snake_move()
{
char c,pause;
static char temp = 0;
pause = 0;
if(!temp){
temp = 'd';
snake_auto_move(temp);
}
while(1){
c = getch();
if(c == 'w' || c == 'a' || c == 's' || c == 'd'){
if((temp == 'w' && c == 's') || (temp == 's' && c == 'w') || (temp == 'd' && c == 'a') || (temp == 'a' && c == 'd')){//不允许连续的两次内按相反的键
snake_auto_move(temp);
continue;
}
break;
}
snake_auto_move(temp);
}
temp = c;
snake_auto_move(temp);
}
int main()
{
snake_init();
show_food();
drow_wall();
while(judge()){
snake_move();
}
return 0;
}