C 實現基於控制台介面的吃豆子遊戲

2022-10-04 04:00:09 字數 4002 閱讀 2850

程式執行介面如下所示:

esc鍵可退出遊戲。

main.cpp原始檔如下:

#include "lib.h"

#pragma once

extern int level;

int main()

showscore(&curpos,&food);

} }return 0;

}lib.h標頭檔案如下:

#include

#include

#include

#include

//標識使用者按下的方向鍵

#define key_up wm_user + 1

#define key_down wm_user + 2

#define key_left wm_user + 3

#define key_right wm_user + 4

//標識使用者當前運動方向

#define direction_up wm_user + 5

#define direction_down wm_user + 6

#define direction_left wm_user + 7

#define direction_right wm_user + 8

//標識要列印的元素型別

#define print_wall wm_user + 9

#define print_food wm_user + 10

#define print_body wm_user + 11

#define print_clean wm_user + 12

#define knock_wall wm_user + 13

#define knock_food wm_user + 14

struct pos

;struct wall

;struct food

;struct body

;struct hall;

void init(food *pfood,wall *pwall,body *pbody,hall *phall);

//進行一些初始化操作

void print(pos *pos,int type,int ix = 0);

//完成列印功能

void makewall(wall *pwall);

//生成牆

void makefood(hall *phall,food *pfood);

//生成食物

int move(food *pfo程式設計客棧od,wall *pwall,body *pbody);

//使用者移動

void showscore(body *pbody,food *pfood);

//顯示等級 和剩餘豆子數

int isover(pos pos1,pos pos2,int type);

//判斷2點是否重合

int isknock(wall *pwall,body *pbody,food *pfood,int type);

//判斷是否撞牆、吃到豆子

int getkey();

//得到使用者按鍵

lib.cpp原始檔如下:

#include "lib.h"

#pragma once

handle hmain_out;

handle hmain_in;

console_cursor_info info;

int ibeans = 0;

int level = 1;

//extern short wall[17][24];

short wall[17][24] = ;

/**********************************

* 功能:

* 判斷使用者與牆相撞或吃到食物

***********************************/

int isknock(wall *pwall,body *pbody,food *pfood,int type)

} else if(knock_food == type) }

return 0;

}/**********************************

* 功能:

* 使用者移動

***********************************/

int move(food *pfood,wall *pwall,body *pbody)

if(0 == ikey)

if(isknock(pwall,pbody,pf

int ix = isknock(pwall,pbody,pfood,knock_food);

if(ix) }

print(&prepos.pos,print_clean); //先刪除上乙個輸出

print(&pbody->pos,print_body);

return 0;

}/**********************************

* 功能:

* 判斷2點是否重合

***********************************/

int isover(pos pos1,pos pos2,int type)

/**********************************

* 功能:

* 生成牆

***********************************/

void makewall(wall *pwall)

} }pwall->len = ix; //更新牆的數量

}/**********************************

* 功能:

* 完成初始化操作

***********************************/

void init(food *pfood,wall *pwall,body *pbody,hall *phall)

} }phall->len = ix;

pbody->pos.x = 2;

pbody->pos.y = 1;

pbody->direction = direction_down;

printf("%d %d\n",phall->pos[0].x,phall->pos[0].y);

//輸出食物

int i;

makefood(phall,pfood);

for(i = 1 ; i <= 7 ; ++i) }

/**********************************

* 功能:

* 得到使用者按鍵

***********************************/

int getkey()

/**********************************

* 功能:

* 完成列印功能

***********************************/

void print(pos *p,int type,int ix)

/**********************************

* 功能:

* 顯示等級 和剩餘豆子數

***********************************/

void showscore(body *pbody,food *pfood)

/**********************************

* 功能:

* 生成食物

***********************************/

void makefood(hall *phall,food *pfood)

pfood->len = 7;

}本文標題: c++實現基於控制台介面的吃豆子遊戲

本文位址:

C 基於控制台的迷宮實現(中)

路徑是由許多點組成的。因此表示路徑的struct中包含了起始點的指標和終止點的指標,以及該路徑中包含點的最大值。typedef struct mstack 對路徑的初始化實際上就是為路徑中的所有點分配空間。void initstack mstack s s top s base s stacksiz...

控制台介面相關操作 C 實現

主要包括 1 輸出座標控制 2 顏色修改 3 高亮顯示方向鍵選取選單項 4 尺寸修改。說明 1 每種顏色對應的十六進製制數如下 0 黑色 1 藍色 2 綠色 3 湖藍色 4 紅色 5 紫色 6 黃色 7 白色 8 灰色 9 淡藍色 10 淡綠色 11 淡淺綠色 12 淡紅色 13 淡紫色 14 淡黃...

2048 控制台版 c實現

這是效果圖 用c 寫的,其實就是用了c 的輸入輸出,剩下全是c的內容 中有詳解,很簡單,歡迎借鑑和交流 include include include include include 為了讀取方向鍵 include 設定控制台填充字元等 using namespace std int score 0...