大一的第一个作品--【贪吃蛇】

来源:互联网 发布:希腊人英语知乎 编辑:程序博客网 时间:2024/04/30 04:42
#include<iostream>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<cstring>
#include <conio.h>  
#include<windows.h>
#define w 19
#define h 19
using namespace std; 
void firstshow(struct snak * head);
void button(struct snak * head);
void show(struct snak * head);
void go(struct snak * head);
void food();
struct snak * new1=NULL;
int a[h][w];
int movee=0;
int gameover=0;
char key=0;
int foodx,foody;
struct snak{
int x;
int y;
int xf;
int yf;
struct snak *next;
struct snak *front;
};
struct snak *tail;
void food(){
while(1){
srand(time(NULL));
foodx=rand()%w;
srand(time(NULL));
foody=rand()%h;
if(a[foody][foodx]!=1){a[foody][foodx]=2;break;}
}
}
void go(struct snak * head){
struct snak *pt=head;
while(1){
if(pt->next==NULL)break;
pt=pt->next;

}
   while(pt!=head){
    if(pt->front==head){
    pt->x=pt->front->xf;
pt->y=pt->front->yf;
pt=pt->front;
   }
   else{
    pt->x=pt->front->x;
pt->y=pt->front->y;
pt=pt->front;
    }

}
}
void button(struct snak * head){

if(kbhit()!=0){
while(kbhit()!=0)
key=getch();
head->xf=head->x;
head->yf=head->y;
switch(key){
case 'w':head->y=head->y-1;movee=1;break;
case 'a':head->x=head->x-1;movee=1;break;
case 's':head->y=head->y+1;movee=1;break;
case 'd':head->x=head->x+1;movee=1;break;
}

}
else{
head->xf=head->x;
   head->yf=head->y;
switch(key){
case 'w':head->y=head->y-1;movee=1;break;
case 'a':head->x=head->x-1;movee=1;break;
case 's':head->y=head->y+1;movee=1;break;
case 'd':head->x=head->x+1;movee=1;break;
}
}
if(a[head->y][head->x]==2){
new1=(struct snak *)malloc(sizeof(struct snak));
new1->x=foodx;
new1->y=foody;
tail->next=new1;
new1->front=tail;
foodx=0;
foody=0;
tail=new1;
tail->next=NULL;
new1=NULL;

}
if(head->x==w||head->y==h||head->x<0||head->y<0)gameover=1;
}
void firstshow(struct snak * head){
int i,j;
memset(a,0,sizeof(a));
struct snak *pt=head;
while(pt!=NULL){
a[pt->y][pt->x]=1;
pt=pt->next;
}
food();
printf("     小游戏:贪吃蛇\n     作者:Mr.李\n");
for(i=0;i<w+2;i++)
printf("▽");
printf("\n");
for(i=0;i<h;i++){
printf("▽");
     for(j=0;j<w;j++){
  if(a[i][j]==0)printf("  ");
  else printf("◇");
  }
  printf("▽\n");
}
for(i=0;i<w+2;i++)
        printf("▽");
   printf("\n");
button(head);
}


void show(struct snak * head){

int i,j;
while(1){

_sleep(500);
button(head);
if(gameover==1)break;
if(movee==1){go(head);movee=0;}
if(key!=0){system("cls");
memset(a,0,sizeof(a));
struct snak *pt=head->next;
a[head->y][head->x]=3;
while(pt!=NULL){
a[pt->y][pt->x]=1;
pt=pt->next;
}
if(a[head->y][head->x]==1)gameover=1;
if(foodx==0&&foody==0)food();
a[foody][foodx]=2;
printf("     小游戏:贪吃蛇\n     作者:Mr.李\n");
for(i=0;i<w+2;i++)
printf("▽");
printf("\n");
for(i=0;i<h;i++){
printf("▽");
     for(j=0;j<w;j++){
  if(a[i][j]==0)printf("  ");
  else printf("◇");
  }
  printf("▽\n");
}
for(i=0;i<w+2;i++)
        printf("▽");
   printf("\n");
 }
}
}
int main(){
struct snak *head=NULL,*p1,*p2;
p1=(struct snak*)malloc(sizeof(struct snak));
srand(time(NULL));
p1->x=rand()%w;
srand(time(NULL));
p1->y=rand()%h;
head=p1;
head->next=NULL;
tail=head;
firstshow(head);
show(head);    
if(gameover==1)printf("恭喜你 失败了!\n");
while(p1){
p2=p1->next;
free(p1);
p1=p2;
}  
    return 0;}         
1 0