DOS简易版C语言贪吃蛇

所属分类: 软件编程 / C 语言 阅读数: 64
收藏 0 赞 0 分享

本文实例为大家分享了C语言实现贪吃蛇的具体代码,供大家参考,具体内容如下

#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不少,当记录了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C++无法重载点符号、::、sizeof等的原因

这篇文章主要介绍了C++无法重载点符号、::、sizeof等的原因的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

实例讲解C语言编程中的结构体对齐

这篇文章主要介绍了C语言编程中的结构体对齐,值得注意的是一些结构体对齐的例子在不同编译器下结果可能会不同,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言的结构体中成员变量偏移问题

这篇文章主要介绍了C语言的结构体中成员变量偏移问题,以讲解如何编写宏来对成员变量进行修改为主,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言结构体中的函数指针

这篇文章主要介绍了详解C语言结构体中的函数指针,文中对函数指针的基本概念也有讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

C++编程中的函数指针初步解析

这篇文章主要介绍了C++编程中的函数指针初步解析,函数指针在C语言和C++学习中都是非常重要的知识,需要的朋友可以参考下
收藏 0 赞 0 分享

实例解析C++中类的成员函数指针

这篇文章主要介绍了C++中类的成员函数指针,例子中以讨论用函数指针调用类的成员函数为主,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针基础学习教程

这篇文章主要介绍了C语言中的函数指针基础学习教程,包括函数指针作为参数来传递等重要知识,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解析C语言中函数指针的定义与使用

这篇文章主要介绍了C语言中函数指针的定义与使用,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言编程中的函数指针以及函数回调

这篇文章主要介绍了C语言编程中的函数指针以及函数回调,函数回调实际上就是让函数指针作函数参数、调用时传入函数地址,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针学习笔记

这篇文章主要介绍了C语言中的函数指针的一些学习知识点记录,文中作者整理了一些比较interesting的函数指针用法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多