欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

DOS簡易版C語言貪吃蛇

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

本文實例為大家分享了C語言實現(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不少,當記錄了。

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

相關文章

  • C語言實現(xiàn)簡易版掃雷小游戲

    C語言實現(xiàn)簡易版掃雷小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡易版掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語言中scanf的用法舉例

    C語言中scanf的用法舉例

    本節(jié)介紹輸入函數(shù)?scanf?的用法,scanf?和?printf?一樣,非常重要,而且用得非常多,所以一定要掌握,這篇文章主要介紹了C++中scanf的用法舉例,需要的朋友可以參考下
    2022-11-11
  • 詳解C++11 線程休眠函數(shù)

    詳解C++11 線程休眠函數(shù)

    這篇文章主要介紹了C++11 線程休眠函數(shù)的相關資料,幫助大家更好的理解和學習C++11,感興趣的朋友可以了解下
    2020-10-10
  • c++利用windows函數(shù)實現(xiàn)計時示例

    c++利用windows函數(shù)實現(xiàn)計時示例

    這篇文章主要介紹了c++利用windows函數(shù)實現(xiàn)計時示例,需要的朋友可以參考下
    2014-05-05
  • 關于c語言的一個小bug詳解

    關于c語言的一個小bug詳解

    以下是對c語言中的一個小bug進行了詳細的分析介紹。需要的朋友可以過來參考下
    2013-08-08
  • c語言實現(xiàn)的帶通配符匹配算法

    c語言實現(xiàn)的帶通配符匹配算法

    這篇文章主要介紹了c語言實現(xiàn)的帶通配符匹配算法,需要的朋友可以參考下
    2015-03-03
  • c++ 實現(xiàn)文件逐行讀取與字符匹配

    c++ 實現(xiàn)文件逐行讀取與字符匹配

    這里嘗試通過C++來實現(xiàn)一個文件IO的功能,看看是否能夠比python的表現(xiàn)更好一些,感興趣的朋友可以參考下
    2021-05-05
  • 如何使用遞歸和非遞歸方式反轉單向鏈表

    如何使用遞歸和非遞歸方式反轉單向鏈表

    以下是對使用遞歸和非遞歸方式反轉單向鏈表的示例進行了詳細的分析介紹,需要的朋友可以過來參考下
    2013-07-07
  • C++深復制和淺復制講解

    C++深復制和淺復制講解

    這篇文章主要介紹了C++深復制和淺復制講解,C++中深復制和淺復制最大的區(qū)別在“類包含指針類型的數(shù)據(jù)成員”時,下面感興趣的小伙伴和小編一起進入文章了解更多相關內(nèi)容吧
    2022-03-03
  • C語言由淺入深講解文件的操作上篇

    C語言由淺入深講解文件的操作上篇

    C語言具有操作文件的能力,比如打開文件、讀取和追加數(shù)據(jù)、插入和刪除數(shù)據(jù)、關閉文件、刪除文件等。與其他編程語言相比,C語言文件操作的接口相當簡單和易學
    2022-04-04

最新評論