最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

DOS簡易版C語言貪吃蛇

 更新時間:2019年11月18日 15:13:11   作者:xxb2008  
這篇文章主要為大家詳細(xì)介紹了DOS簡易版C語言貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
 
 
 
#define WALL_LENGTH 22
 
#define LEFT 0x4b 
#define RIGHT 0x4d 
#define DOWN 0x50 
#define UP 0x48 
 
struct Snakes{
 int x;
 int y;
 struct Snakes *prev;
 struct Snakes *next;
};
 
struct Food{
 int x;
 int y;
};
 
 
struct Snakes *header; 
struct Snakes *tailer; 
struct Food *food; 
 
int wall[WALL_LENGTH][WALL_LENGTH];
int direction = RIGHT;
 
 
 
/**/
void init();
void draw();
void move();
void doMove(int x1, int y1);
void eat();
 
void keydown();
 
void foods();
int isOver();
int isDrawSnake(int x, int y);
int isDrawFood(int x, int y);
 
 
int main(){
 init();
 
 while(1){
 
 
 
 if(isOver()){
 break;
 } 
 
 
 
 move(); 
 eat();
 draw();
  _sleep( 100 );
 keydown();
 
 
 
 }
 
 printf("GAME OVER!");
 
 system("pause");
}
 
 
void init(){
 int y, x;
 
 for(y=0; y < WALL_LENGTH; y++){ 
 for(x=0; x < WALL_LENGTH; x++){
 if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1){
  wall[y][x] = 1;
 }
 }
 }
 
 
 header=(struct Snakes *)malloc(sizeof(struct Snakes));
 header->x=10;
 header->y=10;
 header->prev=NULL;
 
 
 tailer=(struct Snakes *)malloc(sizeof(struct Snakes));
 tailer->x=9;
 tailer->y=10;
 tailer->next=NULL;
 tailer->prev=header;
 
 header->next=tailer;
 
 foods();
 
}
 
void draw(){
 int y, x;
 
 system("cls");
 for(y=0; y < WALL_LENGTH; y++){ 
 for(x=0; x < WALL_LENGTH; x++){
 
 if(wall[y][x] == 1){
 printf("[]");
 }else if(isDrawSnake(x, y)){
 printf("[]");
 }else if(isDrawFood(x, y)){
 printf("()");
 }else{
 printf(" "); 
 
 }
 }
 printf("\n");
 }
 
}
 
void move(){
 
 switch(direction){
 case LEFT : 
 doMove(-1, 0); 
 break;
 case RIGHT : 
 doMove(1, 0); 
 break;
 case UP : 
 doMove(0, -1); 
 break;
 case DOWN : 
 doMove(0, 1); 
 break;
 }
 
}
 
void doMove(int x1, int y1){
 
 struct Snakes *temp_tailer = tailer->prev;
 
 tailer->x=header->x + x1;
 tailer->y=header->y + y1;
 
 tailer->next=header;
 tailer->prev->next = NULL;
 tailer->prev = NULL;
 
 header->prev=tailer;
 
 header = tailer;
 tailer = temp_tailer; 
 
}
 
void eat(){
 
 if(header->x == food->x && header->y == food->y){
 int x1=0, y1 =0;
 struct Snakes *temp_tailer = tailer; 
 tailer=(struct Snakes *)malloc(sizeof(struct Snakes));
 
 switch(direction){
 case LEFT : 
 x1 = -1;
 y1 = 0;
 break;
 
 case RIGHT : 
 x1 = 1;
 y1 = 0;
 break;
 
 case UP : 
 x1 = 0;
 y1 = -1;
 break;
 
 case DOWN : 
 x1 = 0;
 y1 = 1;
 break;
 }
 
 tailer->x=temp_tailer->x + x1;
 tailer->y=temp_tailer->y + y1;
 tailer->next=NULL;
 tailer->prev=temp_tailer;
 
 temp_tailer->next = tailer; 
 foods();
 }
 
}
 
 
void foods(){
 
 int y,x; 
 struct Snakes *temp = header;
 
 _sleep(20); 
 srand((unsigned)time(NULL)); 
 y=rand()%WALL_LENGTH; 
 x=rand()%WALL_LENGTH; 
 
 if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1 ){ 
 return foods();
 }
 
 do{
 if(temp->x == x && temp->y == y){
 return foods();
 }
 temp = temp->next;
 }while(temp != NULL);
 
 
 
 if(food == NULL){
 food=(struct Food *)malloc(sizeof(struct Food));
 }
 food->x = x;
 food->y = y;
 
}
 
void keydown(){
 char keycode; 
 if(_kbhit()&&(keycode =_getch())) {  
  switch(keycode) { 
 case LEFT: 
 if(RIGHT!=direction) {
  direction=LEFT;
 // move();
 // draw();
 }
 break; 
 
 case RIGHT: 
 if(LEFT!=direction) {
  direction=RIGHT;
 // move();
 // draw();
 }
 break; 
 
 case UP: 
 if(DOWN!=direction) {
  direction=UP;
 // move();
 // draw();
 }
 break; 
 
 case DOWN: 
 if(UP!=direction){
  direction=DOWN;
 // move();
 // draw();
 }
 break; 
 
  } 
 } 
 
}
 
 
int isDrawSnake(int x, int y){
 struct Snakes *temp = header;
 do{
 if(temp->x == x && temp->y == y){
 return 1;
 }
 temp = temp->next;
 }while(temp != NULL);
 return 0;
}
 
int isDrawFood(int x, int y){
 if(food->x == x && food->y == y){
 return 1;
 }
 return 0;
}
 
int isOver(){
 int x1=0, y1 =0;
 switch(direction){
 case LEFT : 
 x1 = -1;
 y1 = 0;
 break;
 
 case RIGHT : 
 x1 = 1;
 y1 = 0;
 break;
 
 case UP : 
 x1 = 0;
 y1 = -1;
 break;
 
 case DOWN : 
 x1 = 0;
 y1 = 1;
 break;
 }
 
 if(header->x + x1 <= 0 || header->x + x1 >= WALL_LENGTH - 1 
 || header->y + y1 <= 0 || header->y + y1 >= WALL_LENGTH - 1){
 
 return 1;
 }
 return 0;
 
} 

好久沒寫過C語言了,隨便寫個貪吃蛇玩一玩,BUG不少,當(dāng)記錄了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • LeetCode 單調(diào)棧內(nèi)容小結(jié)

    LeetCode 單調(diào)棧內(nèi)容小結(jié)

    這篇文章主要介紹了LeetCode 單調(diào)棧內(nèi)容小結(jié),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 對C語言中sizeof細(xì)節(jié)的三點(diǎn)分析介紹

    對C語言中sizeof細(xì)節(jié)的三點(diǎn)分析介紹

    以下是對C語言中sizeof的細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • 用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法詳解

    用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法詳解

    本篇文章是對用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 教你用Matlab制作立體動態(tài)相冊

    教你用Matlab制作立體動態(tài)相冊

    沒想到吧,MATLAB竟也能制作3D相冊!本文將為大家詳細(xì)介紹Matlab制作立體動態(tài)相冊的方法步驟,感興趣的小伙伴可以跟隨小編一起動手試一試
    2022-03-03
  • C語言控制進(jìn)程之進(jìn)程等待詳解

    C語言控制進(jìn)程之進(jìn)程等待詳解

    這篇文章主要介紹了C語言控制進(jìn)程之進(jìn)程等待即回收子進(jìn)程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C++內(nèi)存管理介紹

    C++內(nèi)存管理介紹

    這篇文章主要介紹了C++內(nèi)存管理,C++標(biāo)準(zhǔn)委員會給我們提供了auto_ptr智能指針,后面又引入了share_ptr以及weak_ptr幫助我們正確和安全的使用指針,本文主要是介紹boost庫提供的解決方案,需要的朋友可以參考一下
    2022-01-01
  • gazebo里通過節(jié)點(diǎn)發(fā)布topic讓關(guān)節(jié)轉(zhuǎn)動實(shí)現(xiàn)詳解

    gazebo里通過節(jié)點(diǎn)發(fā)布topic讓關(guān)節(jié)轉(zhuǎn)動實(shí)現(xiàn)詳解

    這篇文章主要介紹了gazebo里通過節(jié)點(diǎn)發(fā)布topic讓關(guān)節(jié)轉(zhuǎn)動實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • C和C++ const的聲明差異

    C和C++ const的聲明差異

    本文給大家簡單匯總了下C語言的const與C++的const的聲明差異,非常的簡單,也很實(shí)用,有需要的小伙伴可以參考下
    2016-03-03
  • VS2022配置編譯使用boost庫的實(shí)現(xiàn)

    VS2022配置編譯使用boost庫的實(shí)現(xiàn)

    本文介紹了如何在VS2022中配置和編譯使用Boost庫的步驟,包括下載Boost、解壓、配置環(huán)境變量和編譯等過程,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12
  • c++回調(diào)之利用sink示例

    c++回調(diào)之利用sink示例

    Sink的本質(zhì)是利用C++的封裝、繼承、多態(tài)的面向?qū)ο髞韺?shí)現(xiàn),從實(shí)現(xiàn)角度來說,更優(yōu)于函數(shù)指針回調(diào),下面是示例
    2014-04-04

最新評論

广西| 阳西县| 昭觉县| 虹口区| 陆良县| 合作市| 深州市| 芦山县| 乐山市| 清镇市| 伊吾县| 攀枝花市| 句容市| 汉沽区| 拉孜县| 故城县| 孝昌县| 健康| 潮州市| 陇南市| 鞍山市| 新安县| 新宾| 罗源县| 广灵县| 安远县| 西吉县| 新竹县| 三河市| 元氏县| 临猗县| 马公市| 阿勒泰市| 廊坊市| 许昌县| 临沭县| 金乡县| 沙坪坝区| 勐海县| 怀化市| 安国市|