您的位置:首页 > 编程语言 > C语言/C++

贪吃蛇项目之游戏控制面板的实现

2018-02-10 17:07 405 查看
一:准备前奏
     敲代码之前要有简单的思路,这里我写了一个简单的思维导图。



图片比较简单,接下来,我附上部分代码,整个代码在项目完成后上传。我所用的编译器是Clion,并通过运行。
二:学习了解Windows API
之所以要学习这块,因为我们要在Win32控制台完成这款小游戏,需要控制窗口和光标的位置。实现必要的组件如分数、游戏开始等。下面附上API的头文件和API的实现文件。
Windows ApiHelper定义文件:
/*
windows API 帮助头文件
作者:涵小书
windows API 帮助头文件
Created by asus on 2018/2/6.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/
#ifndef SNAKE_WINAPIHELPER_H
#define SNAKE_WINAPIHELPER_H
#include<windows.h>
/*建立窗口标题*/
void SetTitle(char * title);
/*
*设置窗体大小
* 长和宽
*/
void SetWinSize(int width,int height);
/*
*设置控制台中光标的位置
* x 坐标
* y 坐标
*/
void SetcursorPosition(int x,int y);
/*
* 设置窗体中文字的颜色
*/
void SetColor(int color);
/*
* 定义横线
* x 坐标
* y 坐标
* 颜色
* 绘制字符
* 绘制长度
*/
void DrawHline(int x,int y,int color,char letter,int len);
/*
* 定义竖线
* x 坐标
* y 坐标
* 颜色
* 绘制字符
* 绘制长度
*/
void DrawVline(int x,int y,int color,char letter,int len);
/*
*要打印出来的文本
* 文本的长度
*/
void printText(char *txt,int size);
/*
* 字符填充
*
*/
void fillText(int x,int y,int color,int letter,int width,int height);

/*
* 读取一个字符
*/
char readchar();
#endif //SNAKE_WINAPIHELPER_HWindows ApiHelper帮助实现文件:/*
windows Api 帮助实现文件
作者:涵小书
Created by asus on 2018/2/6.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/
#include<conio.h>
#include <stdio.h>
#include<string.h>
#include"WinApiHelper.h"
/*
* 定一个全局变量表示窗体的句柄
*/
HANDLE set_win_handle = 0;
/*
*获得标准的输出的句柄
*/
void SetWinhandle(){
if(set_win_handle==0){
set_win_handle =GetStdHandle(STD_OUTPUT_HANDLE);
}
}
/*
* 设置窗口的名字
*/
void SetTitle(char *title) {
SetConsoleTitle(TEXT(title));
}
/*
* 这里要设置窗口大小 这里需要引入setWindowInfo 实现 还需要一些窗体的句柄
*/
void SetWinSize(int width, int height) {
SMALL_RECT rect={0,0,width-1,height-1}; //窗口的大小 起点-终点
COORD coord={width,height}; //缓冲区的区域
SetWinhandle();
SetConsoleScreenBufferSize(set_win_handle,coord); //设置缓冲区
SetConsoleWindowInfo(set_win_handle,TRUE,&rect); //设置窗体的大小
}

void SetcursorPosition(int x, int y) {
COORD pos={x,y};
SetWinhandle();
SetConsoleCursorPosition(set_win_handle,pos);
}

void SetColor(int color) {
SetWinhandle();
SetConsoleTextAttribute(set_win_handle,color);
}

void DrawHline(int x, int y, int color, char letter, int len) {
SetcursorPosition(x,y);
SetColor(color);
int i;
for(i=0;i<len;i++){
printf("%c",letter);
}

}

void DrawVline(int x, int y, int color, char letter, int len) {
SetcursorPosition(x,y);
SetColor(color);
int i;
for(i=0;i<len;i++){
printf("%c",letter);
SetcursorPosition(x,++y);
}
}
/*
* 将汉字从左向右输出保持美观
*/
void printText(char *txt, int size) {
char format[30]="%-";    //转化字符文本格式
char sizebuf[5];
itoa(size,sizebuf,10);
strcat(format,sizebuf);
strcat(format,"s");
printf(format,txt);

}

void fillText(int x, int y, int color, int letter, int width, int height) {
SetWinhandle();
DWORD relen;
int i;
for(i=0;i<height;i++){
COORD coord={x,y++};
FillConsoleOutputAttribute(set_win_handle,color,width,coord,&relen);
}
}

char readchar() {
char rech = '0';
if(kbhit()){
rech = getch();
}
return 0;
}
 三:基本面板的实现       接下来写控制面板的定义文件和实现文件。在此之前我们要知道计算机的各种颜色的代码,这里有个小技巧。打开搜索 - > cmd - > color /?会显示各个颜色的十六进制数字。这样我们可以设置背景色和前景色。          现在我们开始实现控制面板的代码。代码我已经写上了注释方便阅读。基本面板的定义文件:/*
基础面板的定义文件
作者:涵小书
Created by asus on 2018/2/6.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/

#ifndef SNAKE_PANEL_H
#define SNAKE_PANEL_H
#include"../common/WinApiHelper.h"

/*
* 基础面板结构定义
*/

typedef struct _base_panel{
int x; //x.坐标
int y; //y.坐标
int width; // 宽度
int height; //长度
char hLetter; //横线
char vLetter; //竖线
int color; //前景色
int bgcolor; //背景色
}Panel;
/**
*面板的初始化函数
* @return
*/
Panel *PanelInit();
/**
* 面板绘制函数
* @param panel 需要绘制的面板
*/
void drawPanel(Panel * panel);
/**
*基本的展示组件
*/
typedef struct best_text_view{
int x;
int y;
int color;
int bgcolor;
int size;
char txt[50];
_Bool selectAble; //默认值为0, 1代表可以选择
}TextView;
/**
* 展示组件的绘制函数
* @param panel
* @param views
* @param len
*/

void drawViews(Panel*panel,TextView*views,int len);
#endif //SNAKE_PANEL_H
基础面板的实现文件:/*
基本面板的实现文件
作者:涵小书
Created by asus on 2018/2/6.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/

#include"Panel.h"

/**
* 面板初始化
* @return
*/
Panel *PanelInit() {
Panel* panel = (Panel*)malloc(sizeof(Panel));
memset(panel,0,sizeof(Panel));
panel->width=5;
panel->height=5;
panel->hLetter='-';
panel->vLetter='|';
panel->color=0xf;
panel->bgcolor=0x0;
return panel;
}

/**
* 面板的绘制
* @param panel
*/
void drawPanel(Panel *panel) {
/**
* 控制面板的背景绘制
*/
fillText(panel->x,panel->y,panel->bgcolor<<4|panel->color,' ',panel->width,panel->height);
/*
* 绘制竖线
*/
DrawVline(panel->x,panel->y,panel->bgcolor<<4|panel->color,panel->vLetter,panel->height);
DrawVline(panel->x+panel->width-1,panel->y,panel->bgcolor<<4|panel->color,panel->vLetter,panel->height);
/**
* 横线的绘制
*/
DrawHline(panel->x,panel->y,panel->bgcolor<<4|panel->color,panel->hLetter,panel->width);
DrawHline(panel->x,panel->y+panel->height,panel->bgcolor<<4|panel->color,panel->hLetter,panel->width);
}

void drawViews(Panel *panel, TextView *views, int len) {
int i;
for(i=0;i<len;i++){
TextView*v=views+i;
int x=panel->x+v->x;
int y=panel->y+v->y;
SetcursorPosition(x,y);
SetColor(v->bgcolor<<4|v->color);
printText(v->txt,v->size);
}
}现在基本面板已经完成,来完成面板的控制。控制面板的头文件:/*
* 基础面板的头文件
作者:涵小书
Created by asus on 2018/2/10.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/
9307

#ifndef SNAKE_CONTROLPANEL_H
#define SNAKE_CONTROLPANEL_H

#include "Panel.h"
/**
* 声明控制面板的绘制函数
*/

void DrawControlPanel();
#endif //SNAKE_CONTROLPANEL_H
控制面板的实现文件:/*
控制面板的实现文件
作者:涵小书
Created by asus on 2018/2/10.
让我感谢你 赠我空欢喜 by 琳小呆
版权所有 by GSH 转发注明作者
此版仅供学习参考
*/
#include"ControlPanel.h"

Panel * control_panel =NULL;
/**
* 在控制面板中所要展示的文件
*/

TextView control_view[]={
{5,2,0xE,0x0,8,"分数:0"},
{5,5,0xE,0x0,8,"长度:0"},
{5,8,0xE,0x0,8,"秒/格:0"},
{5,11,0xE,0x0,8,"游戏",1},
{5,14,0xE,0x0,8,"设置",1}
};
/**
* 基础面板的实现
*/
void DrawControlPanel() {
if(control_panel==NULL){
control_panel=PanelInit();
control_panel->x=2;
control_panel->y=2;
control_panel->width=18;
control_panel->height=18;
control_panel->color=0xE;
control_panel->bgcolor=0x0;
}
drawPanel(control_panel);
int view_len= sizeof(control_view)/ sizeof(TextView);
drawViews(control_panel,control_view,view_len);
}
四:效果实现



五:结语人生如之如初见,​​何事秋风悲画扇----纳兰性德
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息