c語言編的貪吃蛇
⑴ 求c語言的貪吃蛇的代碼 能運行的
//ConsoleApplication1.cpp:定義控制台應用程序的入口點。
//
#include"stdafx.h"
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#defineframe_height20//definemapsize
#defineframe_width40
#defineUP'w'//defineoperatekey
#defineDOWN's'
#defineLEFT'a'
#defineRIGHT'd'
inti,j,k;
charch=UP;//initialdirection
intgrow=0;//flag:ifsnakegrow
structFood{
intx;
inty;
}food;
structSnake{
intx[50];
inty[50];
intlen;
intspeed;
}snake;//snake[0]ishead
voidinit_map(void);
voipdate_food(void);
voidmove_snake(void);
intis_alive(void);
voidget_speed(void);
voidgotoxy(intx,inty);
intmain()
{
init_map();//初始化地圖
while(1)
{
update_food();
//是否產生食物
get_speed();//獲取速度
move_snake();//移動蛇身
Sleep(snake.speed);
//移動速度
if(!(is_alive()))
//蛇的死活(撞牆或自食)
break;
}
printf("GameOver!");
_getch();
return0;
}
//initialize
voidinit_map(void)
{
//initialfood
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");
//initialsnake
snake.x[0]=frame_height/2;
snake.y[0]=frame_width/2;
gotoxy(snake.x[0],snake.y[0]);
printf("@");
snake.len=3;
snake.speed=200;
for(k=1;k<snake.len;k++)
{
snake.x[k]=snake.x[k-1]+1;
snake.y[k]=snake.y[k-1];
gotoxy(snake.x[k],snake.y[k]);
printf("@");
}
//initialbar
for(j=0;j<frame_width;j++)
{
gotoxy(0,j);
printf("#");
gotoxy(frame_height-1,j);
printf("#");
}
for(i=1;i<frame_height-1;i++)
{
gotoxy(i,0);
printf("#");
gotoxy(i,frame_width-1);
printf("#");
}
}
//generatefood
voipdate_food()
{
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");
snake.len++;
grow=1;
}
}
//movesnake
voidmove_snake()
{
if(_kbhit())
ch=_getch();
if(!grow)
{
gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf("");
}
for(k=snake.len-1;k>0;k--)
{
snake.x[k]=snake.x[k-1];
snake.y[k]=snake.y[k-1];
}
switch(ch)
{
caseUP:snake.x[0]--;break;
caseDOWN:snake.x[0]++;break;
caseLEFT:snake.y[0]--;break;
caseRIGHT:snake.y[0]++;break;
default:break;
}
gotoxy(snake.x[0],snake.y[0]);
printf("@");
grow=0;
gotoxy(frame_height,0);
}
//isalive
intis_alive(void)
{
if(snake.x[0]==0||snake.x[0]==frame_height-1||snake.y[0]==frame_width-1||snake.y[0]==0)
return0;
for(k=1;k<snake.len;k++)
if(snake.x[k]==snake.x[0]&&snake.y[k]==snake.y[0])
return0;
return1;
}
//speep
voidget_speed(void)
{
if(snake.len<=6)
snake.speed=200;
elseif(snake.len<=10)
snake.speed=100;
elseif(snake.len<=20)
snake.speed=50;
elseif(snake.len<=30)
snake.speed=30;
elsesnake.speed=20;
}
//movecursor
voidgotoxy(intx,inty)
{
HANDLEhout;
COORDcor;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
cor.X=y;
cor.Y=x;
SetConsoleCursorPosition(hout,cor);
}
⑵ 如何用C語言寫貪吃蛇
#include<conio.h> #include<graphics.h> #include<time.h> #include<string.h> #include<malloc.h> #include<stdio.h> int grade=5,point=0,life=3; void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug { int x; int y; struct bug *last; struct bug *next; }; struct fd { int x; int y; int judge; }food={0,0,0}; struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL; void main() { char ch; initgraph(800,600); set(); init_insect(); while(1) { food_f(); Sleep(grade*10); setcolor(BLACK); circle(head_l->x,head_l->y,2); setcolor(WHITE); move_body(); if(kbhit()) { ch=getch(); if(ch==27) { ahead(); set(); } else if(ch==-32) { switch(getch()) { case 72:upon();break; case 80:down();break; case 75:left();break; case 77:right();break; } } else ahead(); } else { ahead(); } if(head_f->x==food.x&&head_f->y==food.y) { Sleep(100); crate(); food.judge=0; point=point+(6-grade)*10; if(food.x<30||food.y<30||food.x>570||food.y>570) life++; menu(); } if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); } for(p1=head_f->next;p1!=NULL;p1=p1->next) { if(head_f->x==p1->x&&head_f->y==p1->y) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戲結束!"); getch(); return; } move(); }; } void init_graph() { clearviewport(); setlinestyle(PS_SOLID,1,5); rectangle(2,2,600,598); setlinestyle(PS_SOLID,1,1); } void set() { init_graph(); outtextxy(640,50,"1、開始 / 返回"); outtextxy(640,70,"2、退出"); outtextxy(640,90,"3、難度"); outtextxy(640,110,"4、重新開始"); switch(getch()) { case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return; case '2': exit(0);break; case '3': outtextxy(700,90,":1 2 3 4 5");grade=getch()-48;set();break; case '4': food.judge=0,grade=5;point=0;life=3;init_insect();menu();break; default: outtextxy(250,300,"輸入錯誤!"); set();break; } } void menu() { char str[20],str1[]={"分數:"},str2[]={"難度:"},str3[]={"生命值:"}; init_graph(); sprintf(str,"%d",point); strcat(str1,str); outtextxy(640,50,str1); sprintf(str,"%d",grade); strcat(str2,str); outtextxy(640,70,str2); sprintf(str,"%d",life); strcat(str3,str); outtextxy(640,90,str3); outtextxy(640,110,"設置:ESC"); } void init_insect() { head_f=(struct bug *)malloc(sizeof(struct bug)); head_f->last=NULL; head_f->x=300; head_f->y=300; p2=head_f->next=p1=(struct bug *)malloc(sizeof(struct bug)); p1->last=head_f; p1->x=295; p1->y=300; p1=p1->next=(struct bug *)malloc(sizeof(struct bug)); p1->next=NULL; p1->x=290; p1->y=300; p1->last=p2; head_l=p1; } void move() { for(p1=head_f;p1!=NULL;p1=p1->next) { circle(p1->x,p1->y,2); } } void move_head() { } void move_body() { for(p1=head_l,p2=p1->last;p2!=NULL;p1=p2,p2=p2->last) { p1->x=p2->x; p1->y=p2->y; } } void ahead() { p1=head_f; p2=p1->next; p2=p2->next; if(p1->x==p2->x) { if(p1->y>p2->y) head_f->y+=5; else head_f->y-=5; } else { if(p1->x>p2->x) { head_f->x+=5; } else head_f->x-=5; } } void upon() { p1=head_f->next; p1=p1->next; head_f->y-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y+=5; ahead(); } } void down() { p1=head_f->next; p1=p1->next; head_f->y+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y-=5; ahead(); } } void left() { p1=head_f->next; p1=p1->next; head_f->x-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x+=5; ahead(); } } void right() { p1=head_f->next; p1=p1->next; head_f->x+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x-=5; ahead(); } } void food_f() { if(!food.judge) { food.x=(rand()%117+1)*5; food.y=(rand()%117+1)*5; food.judge=1; if(food.x<30||food.y<30||food.x>570||food.y>570) { setcolor(RED); circle(f
⑶ c語言貪吃蛇源代碼怎麼用
C語言貪吃蛇源代碼必須經過相應的C/C++編譯器編譯成EXE文件後才能運行。
由於我們通常使用的操作系統是Windows系統,而在該系統下最長用的C/C++編譯器是VC++編譯器,目前在大專院校常用的版本還是VC++6.0
下面就以VC++6.0來說明編譯過程:
1.在VC++6.0中通過「File」菜單下的 「Open」子菜單打開貪吃蛇代碼
/*
C/C++貪吃蛇游戲,zjlj,2015.3.16
*/
#defineDEBUG0//當程序在調試階段時DEBUG為1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
usingnamespacestd;
voidreadini(FILE**fphead,int*score,char*argv[])//創建或打開一個和運行文件對應的ini文件,讀取最高紀錄
{
charfilename[200],*pfilename;
intflag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='