- N +

貪吃蛇代碼c語言簡單?文本文檔簡單貪吃蛇代碼

八個led燈單片機(jī)使用C語言怎么做貪吃蛇

這就是一個簡單的雙層循環(huán)程序,沒有任何難度的。

第一層控制做減法循環(huán)

第二層控制做LED亮燈

就這么簡單

用C語言,能在100行之內(nèi)實(shí)現(xiàn)貪吃蛇嗎

恕我直言貪吃蛇這個游戲雖小但涉及的方面還是不少的,想要做一個完整的貪吃蛇100行應(yīng)該不好實(shí)現(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é)點(diǎn)

{

intx;

inty;

structSNAKE*next;

}snake;

//全局變量//

intscore=0,add=10;//總得分與每次吃食物得分。

intstatus,sleeptime=200;//每次運(yùn)行的時間間隔

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è)置光標(biāo)位置

{

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()//隨機(jī)出現(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()//蛇前進(jìn),上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

貪吃蛇c語言代碼最短

#include<bits/stdc++.h>

#include<windows.h>

#include<conio.h>

usingnamespacestd;

voidgotoxy(intx,inty){COORDpos={x,y};SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//光標(biāo)定位

classFood{//食物類

private:intm_x;intm_y;

public:

voidrandfood(){//隨機(jī)產(chǎn)生一個食物

srand((int)time(NULL));//利用時間添加隨機(jī)數(shù)種子,需要ctime頭文件

L1:{m_x=rand()%(85)+2;//2~86

m_y=rand()%(25)+2;//2~26

if(m_x%2)gotoL1;//如果食物的x坐標(biāo)不是偶數(shù)則重新確定食物的坐標(biāo)

gotoxy(m_x,m_y);//在確認(rèn)好的位置輸出食物

cout<<"★";}}

intgetFoodm_x(){returnm_x;}//返回食物的x坐標(biāo)

intgetFoodm_y(){returnm_y;}};//返回食物的y坐標(biāo)

classSnake{

private:

structSnakecoor{intx;inty;};//定義一個蛇的坐標(biāo)機(jī)構(gòu)

vector<Snakecoor>snakecoor;//將坐標(biāo)存入vector容器中

//判斷并改變前進(jìn)方向的函數(shù)

voiddegdir(Snakecoor&nexthead){//定義新的蛇頭變量

staticcharkey='d';//靜態(tài)變量防止改變移動方向后重新改回來

if(_kbhit()){

chartemp=_getch();//定義一個臨時變量儲存鍵盤輸入的值

switch(temp){//如果臨時變量的值為wasd中的一個,則賦值給key

default:break;//default是缺省情況,只有任何條件都不匹配的情況下才會執(zhí)行必須寫在前面!不然蛇無法轉(zhuǎn)向

case'w':case'a':case's':case'd':

//如果temp的方向和key的方向不相反則賦值因?yàn)閮纱我苿臃较虿荒芟喾磳⑸咴O(shè)置為初始向右走

if(key=='w'&&temp!='s'||key=='s'&&temp!='w'||key=='a'&&temp!='d'||key=='d'&&temp!='a')key=temp;}}

switch(key){//根據(jù)key的值來確定蛇的移動方向

case'd':nexthead.x=snakecoor.front().x+2;nexthead.y=snakecoor.front().y;break;

//新的蛇頭的頭部等于容器內(nèi)第一個數(shù)據(jù)(舊蛇頭)x坐標(biāo)+2因?yàn)樯哳^占兩個坐標(biāo),移動一次加2

case'a':nexthead.x=snakecoor.front().x-2;nexthead.y=snakecoor.front().y;break;

case'w':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y-1;break;

//因?yàn)榭刂婆_的x長度是y的一半,所以用兩個x做蛇頭,需要的坐標(biāo)是二倍

case's':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y+1;}}

//游戲結(jié)束時設(shè)計(jì)一個界面輸出“游戲結(jié)束”以及分?jǐn)?shù)

voidfinmatt(constintscore){

system("cls");gotoxy(40,14);//清屏然后輸出

cout<<"游戲結(jié)束";gotoxy(40,16);

cout<<"得分:"<<score;gotoxy(0,26);

exit(0);}//exit為C++的退出函數(shù)exit(0)表示程序正常退出,非0表示非正常退出

voidfinishgame(constintscore){//游戲結(jié)束

if(snakecoor[0].x>=88||snakecoor[0].x<0||snakecoor[0].y>=28||snakecoor[0].y<0)finmatt(score);//撞墻

for(inti=1;i<snakecoor.size();i++)if(snakecoor[0].x==snakecoor[i].x&&snakecoor[0].y==snakecoor[i].y)finmatt(score

c語言現(xiàn)在還有必要學(xué)嗎,如果有,優(yōu)勢是什么

這是這個問題具備很強(qiáng)的兩面性,對于想切入某些領(lǐng)域比如嵌入式開發(fā)等這些C語言是必須要學(xué)的,不但是C語言要學(xué)習(xí),數(shù)據(jù)結(jié)構(gòu),c++也要切入進(jìn)去,所以對于C語言的學(xué)習(xí),還是要選擇的方向,有些java的技術(shù)大牛本身也不懂c++,但java搞的風(fēng)生水起。

從系統(tǒng)學(xué)習(xí)的角度考慮學(xué)習(xí)C語言還是很有必要,畢竟懂得底層功能的實(shí)現(xiàn),對于系統(tǒng)的了解軟件框架還是有幫助。現(xiàn)在很多主流的編程語言的底層大多是基于C語言構(gòu)建出來的,了解底層的實(shí)現(xiàn),對于深刻了解底層代碼的實(shí)現(xiàn)有非常強(qiáng)烈的借鑒意義。

簡單總結(jié)下學(xué)習(xí)C語言的步驟:

1.選擇一本教材然后找到配對的視頻

2.制定學(xué)習(xí)計(jì)劃,計(jì)劃的推進(jìn)以書本的大綱為基準(zhǔn)

3.在推進(jìn)計(jì)劃的過程中,如果遇到不懂找到對應(yīng)的視頻學(xué)習(xí)

4.把指針,數(shù)組,函數(shù),結(jié)構(gòu)體,遞歸,回調(diào)等基礎(chǔ)學(xué)好之后

5.嘗試做點(diǎn)C語言的測試小項(xiàng)目,比如貪食蛇,圖書管理系統(tǒng)等等

6.找一些開源的代碼,通過查找資料爭取讓自己看明白

7.嘗試修改開源代碼,并且能夠順暢的運(yùn)行起來。

覺得有道理就點(diǎn)個贊

返回列表
上一篇:
下一篇: