#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
#define HIGH 20
#define WIDE 41
int wall[HIGH][WIDE];
int food_x,food_y;
enum {UP,DOWN,RIGHT,LEFT}point;
typedef struct snake
{
int x,y;
struct snake *prior;
struct snake *next;
}snake;
snake *head,* tail;
void add_head(int a,int b)
{
snake *temp=new snake;
temp->x=a;
temp->y=b;
temp->prior=NULL;
if(head==NULL)
{
head=tail=temp;
head->next=NULL;
}
else
{
head->prior=temp;
temp->next=head;
head=head->prior;
}
wall[a]=1;
}
void delete_tail()
{
snake *temp=tail;
tail=tail->prior;
tail->next=NULL;
delete temp;
}
void create_food()
{
srand(unsigned(time(0)));
do
{
food_x=rand()%18+1;
food_y=rand()%39+1;
}while(wall[food_x][food_y]==1);
wall[food_x][food_y]=1;
}
void output()
{
int i,j;
for(i=0;i<HIGH;i++)
{
for(j=0;j<WIDE;j++)
{
if(wall[j])
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void change_point(char keydown)
{
switch (keydown)
{
case 'W':
case 'w':point=UP;break;
case 'A':
case 'a':point=LEFT;break;
case 'S':
case 's':point=DOWN;break;
case 'D':
case 'd':point=RIGHT;break;
}
}
void move()
{
int a=head->x;
int b=head->y;
switch(point)
{
case UP:--a;break;
case DOWN:++a;break;
case RIGHT:++b;break;
case LEFT:--b;break;
}
if(wall[a]==1 && food_x != a && food_y != b)
{
output();
printf("game over");
}
if(food_x == a && food_y == b)
{
add_head(a,b);
create_food();
return;
}
add_head(a,b);
delete_tail();
}
void init()
{
int i;
for(i=0;i<WIDE;i+=2)
{
wall[0]=1;
wall[HIGH-1]=1;
}
for(i=0;i<HIGH;i++)
{
wall[0]=1;
wall[WIDE-1]=1;
}
head=tail;
point=RIGHT;
add_head(6,4);
add_head(6,5);
add_head(6,6);
}
int main()
{
init();
create_food();
output();
printf("説明:W ↑,A ←,S↓,D →\n press any key to start ");
while(true)
{
char keydown=getch();
change_point(keydown);
while(!kbhit())
{
system("cls");
move();
output();
Sleep(500);
}
}
return 0;
}
我這個貪吃蛇不吃食物也會自動長大,而且無法GAME OVER, 并且蛇生成也是固定位置,
問題很多,求高手解決,不過是在這個基礎上的修改,不要全部改掉,VC++6.0環境能運行的簡單貪吃
蛇,不要太復雜
http://wenku.baidu.com/view/33b6f9d2195f312b3169a5ab.html
我是照這個教程編的,但是這個教程本身就有問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/49133.html
標籤:基礎類
上一篇:請教
