DOS簡易版C語言貪吃蛇
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
本文實例為大家分享了C語言實現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內容如下
#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不少,當記錄了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10C++ 模版雙向鏈表的實現(xiàn)詳解
- 01-10哈希表實驗C語言版實現(xiàn)
- 01-10用位圖排序無重復數(shù)據(jù)集實例代碼(C++版)
- 01-10c語言版本二叉樹基本操作示例(先序 遞歸 非遞歸)
- 01-10簡易Dota改鍵外掛程序制作
- 01-10判斷本機office安裝版本的方法分享
- 01-10c語言網(wǎng)絡編程-標準步驟(改進版)
- 01-10c++版線程池和任務池示例
- 01-10Recommended C Style and Coding Standards中文翻譯版第1/3頁
- 01-10貪吃蛇游戲C++命令行版實例代碼


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?


