很多朋友對于c語言游戲代碼貪吃蛇和貪吃蛇游戲編程代碼不太懂,今天就由小編來為大家分享,希望可以幫助到大家,下面一起來看看吧!
什么軟件有c語言游戲代碼
經(jīng)典的貪吃蛇就有C語言游戲代碼。
c語言貪吃蛇代碼及解析
以下是一個使用C語言編寫的簡單貪吃蛇游戲,包括初始化游戲界面、繪制蛇和食物、移動蛇和檢測碰撞等功能。
```c
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//定義常量
constintwidth=20;
constintheight=20;
constintmax_length=5;
constintblock_size=20;
constchardirection[]="RDLU";
constintfood_x=10;
constintfood_y=10;
constintsnake_speed=100;
//定義結(jié)構(gòu)體,存儲蛇的身體坐標和方向
structSnake{
intx,y;
intlength;
chardirection;
};
//定義結(jié)構(gòu)體,存儲食物的位置和狀態(tài)(是否被吃掉)
structFood{
intx,y;
};
//定義全局變量,存儲蛇和食物的信息
structSnakesnake;
structFoodfood;
intscore;
//初始化游戲界面和蛇的狀態(tài)(位置和長度)
voidinit(){
//初始化窗口大小和標題欄
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),width*block_size,height*block_size);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&buffer_info);
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE),TRUE,NULL,NULL,buffer_info.dwMaximumWindowSize);
printf("SnakeGame!
");
fflush(stdout);
//初始化蛇的位置和長度為3個方塊,方向為左移符('L')
snake.x=height/2;
snake.y=height/2;
snake.length=3;
snake.direction='L';
//隨機生成一個食物的位置和狀態(tài)(是否被吃掉)
srand((unsigned)time(NULL));
food.x=(rand()%(width*block_size))+food_x;
food.y=(rand()%(height*block_size))+food_y;
}
//在屏幕上繪制蛇和食物的圖像
voiddraw(){
RECTrect;
inti;
//根據(jù)蛇的位置和方向計算出每個方塊的坐標和顏色值(RGB)
i=snake.length;
intcolorR=(snake.direction&'R')=='R'?155:155-(snake.length-i)*20;
intcolorG=(snake.direction&'G')=='G'?180:180-(snake.length-i)*20;
intcolorB=(snake.direction&'B')=='B'?25:25-(snake.length-i)*20;
intcolorD=(snake.direction&'D')=='D'?0:0-(snake.length-i)*20;
intcolorE=(snake.direction&'E')=='E'?7:7-(snake.length-i)*20;
intcolorF=(snake.direction&'F')=='F'?145:145-(snake.length-i)*20;
intcolorY=(snake.direction&'Y')=='Y'?11:11-(snake.length-i)*20;
intcolorX=(snake.direction&'X')=='X'?191:191-(snake.length-i)*20;
intcolorN=(snake.direction&'N')=='N'?165:165-(snake.length-i)*20;
intcolorM=(snake.direction&'M')=='M'?135:135-(snake.length-i)*20;
程序員web前端-java-網(wǎng)頁制作-20行代碼怎樣編輯貪吃蛇小游戲
<!doctypehtml>
<html>
<body>
<canvasid="can"width="400"height="400"style="background:Black"></canvas>
<script>
varsn=[42,41],dz=43,fx=1,n,ctx=document.getElementById("can").getContext("2d");
functiondraw(t,c){
ctx.fillStyle=c;
ctx.fillRect(t%20*20+1,~~(t/20)*20+1,18,18);
}
document.onkeydown=function(e){
fx=sn[1]-sn[0]==(n=[-1,-20,1,20][(e||event).keyCode-37]||fx)?fx:n
};
!function(){
sn.unshift(n=sn[0]+fx);
if(sn.indexOf(n,1)>0||n<0||n>399||fx==1&&n%20==0||fx==-1&&n%20==19)
returnalert("GAMEOVER");
draw(n,"Lime");
if(n==dz){
while(sn.indexOf(dz=~~(Math.random()*400))>=0);
draw(dz,"Yellow");
}else
draw(sn.pop(),"Black");
setTimeout(arguments.callee,130);
}();
</script>
</body>
</html>
八個led燈單片機使用C語言怎么做貪吃蛇
這就是一個簡單的雙層循環(huán)程序,沒有任何難度的。
第一層控制做減法循環(huán)
第二層控制做LED亮燈
就這么簡單
用C語言,能在100行之內(nèi)實現(xiàn)貪吃蛇嗎
恕我直言貪吃蛇這個游戲雖小但涉及的方面還是不少的,想要做一個完整的貪吃蛇100行應(yīng)該不好實現(xiàn)。
建議樓主試試這個。
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#defineU1
#defineD2
#defineL3
#defineR4//蛇的狀態(tài),U:上;D:下;L:左R:右
typedefstructSNAKE//蛇身的一個節(jié)點
{
intx;
inty;
structSNAKE*next;
}snake;
//全局變量//
intscore=0,add=10;//總得分與每次吃食物得分。
intstatus,sleeptime=200;//每次運行的時間間隔
snake*head,*food;//蛇頭指針,食物指針
snake*q;//遍歷蛇的時候用到的指針
intendgamestatus=0;//游戲結(jié)束的情況,1:撞到墻;2:咬到自己;3:主動退出游戲。
//聲明全部函數(shù)//
voidPos();
voidcreatMap();
voidinitsnake();
intbiteself();
voidcreatefood();
voidcantcrosswall();
voidsnakemove();
voidpause();
voidgamecircle();
voidwelcometogame();
voidendgame();
voidgamestart();
voidPos(intx,inty)//設(shè)置光標位置
{
COORDpos;
HANDLEhOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
voidcreatMap()//創(chuàng)建地圖
{
inti;
for(i=0;i<58;i+=2)//打印上下邊框
{
Pos(i,0);
printf("■");
Pos(i,26);
printf("■");
}
for(i=1;i<26;i++)//打印左右邊框
{
Pos(0,i);
printf("■");
Pos(56,i);
printf("■");
}
}
voidinitsnake()//初始化蛇身
{
snake*tail;
inti;
tail=(snake*)malloc(sizeof(snake));//從蛇尾開始,頭插法,以x,y設(shè)定開始的位置//
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1;i<=4;i++)
{
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL)//從頭到為,輸出蛇身
{
Pos(tail->x,tail->y);
printf("■");
tail=tail->next;
}
}
intbiteself()//判斷是否咬到了自己
{
snake*self;
self=head->next;
while(self!=NULL)
{
if(self->x==head->x&&self->y==head->y)
{
return1;
}
self=self->next;
}
return0;
}
voidcreatefood()//隨機出現(xiàn)食物
{
snake*food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0)//保證其為偶數(shù),使得食物能與蛇頭對其
{
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL)
{
if(q->x==food_1->x&&q->y==food_1->y)//判斷蛇身是否與食物重合
{
free(food_1);
createfood();
}
q=q->next;
}
Pos(food_1->x,food_1->y);
food=food_1;
printf("■");
}
voidcantcrosswall()//不能穿墻
{
if(head->x==0||head->x==56||head->y==0||head->y==26)
{
endgamestatus=1;
endgame();
}
}
voidsnakemove()//蛇前進,上U,下D,左L,右R
{
snake*nexthead;
cantcrosswall();
nexthead=(snake*)malloc(sizeof(snake));
if(status==U)
{
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x&&nexthead->y==food->y)//如果下一個有食物//
{
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}
else//如果沒有食物//
{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==D)
{
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food->x&&nexthead->y==food->y)//有食物
{
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}
else//沒有食物
{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==L)
{
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y)//有食物
{
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}
else//沒有食物
{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf("");
free(q->next);
q->next=NULL;
}
}
if(status==R)
{
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y)//有食物
{
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL)
{
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
}
else//沒有食物
{
nexthead
文章分享結(jié)束,c語言游戲代碼貪吃蛇和貪吃蛇游戲編程代碼的答案你都知道了嗎?歡迎再次光臨本站哦!