这篇文章主要讲解了“怎么用C语言实现小游戏打砖块”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用C语言实现小游戏打砖块”吧!
游戏目标:消除所有的方块即可过关。操作指南:游戏中使用键盘方向键←→控制移动
OK,了解游戏的基本操作以及游戏玩法之后就可以开始我们的编程之旅了,今天我会一步代码一个图片的来分布展示,希望这种方式可以让大家更容易的理解,如果有其他更好的方式,也欢迎大家向我提出建议
首先是创建一个游戏窗口,我们用EasyX图形库来做,只需要一行代码
hwnd = initgraph(800, 800);
这样我们就创建了一个800*800的窗口,非常简单,非常好用,这也是非常适合初学者去尝试的,这里我们写在主函数里面就可以了
接下来就是我们的老朋友结构体了,木板、球、以及砖块,这没什么好说的,不管啥项目用结构体都是很常见的
//木板的过程
struct Board
{
int x;
int y;
int speed;
COLORREF color;
int width;
int height;
};
//struct Board board = { 300, 800 - 25,1, WHITE, 200, 25 };
struct Board* createBoard(int x, int y, int speed, COLORREF color, int width, int height)
{
struct Board* pBoard = (struct Board*)malloc(sizeof(struct Board));
//结构体指针->成员 ->指针指向运算符
//(*指针).成员;
pBoard->x = x;
pBoard->y = y;
pBoard->speed = speed;
pBoard->color = color;
//结构体变量.成员
(*pBoard).width = width;
(*pBoard).height = height;
return pBoard;
}
//球:
struct Ball
{
int x;
int y;
int r; //半径
int dx;
int dy;
COLORREF color;
};
struct Ball* createBall(int x, int y, int r, int dx, int dy, COLORREF color)
{
struct Ball* pBall = (struct Ball*)malloc(sizeof(struct Ball));
pBall->x = x;
pBall->y = y;
pBall->r = r;
pBall->dx = dx;
pBall->dy = dy;
pBall->color = color;
return pBall;
}
后面就是我们来画我们的游戏界面了(砖块、球、以及木板),这我是分开写的,可以更好的理解
void drawMap()
{
setlinestyle(PS_SOLID, 2);
setlinecolor(WHITE);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
int x = 100 * j; //j=x/100
int y = 25 * i; //i=y/i
switch (map[i][j]) //map[i][j]!=0
{
case 0: //做消除用的
break;
case 1:
setfillcolor(YELLOW);
fillrectangle(x, y, x + 100, y + 25);
break;
case 2:
setfillcolor(LIGHTBLUE);
fillrectangle(x, y, x + 100, y + 25);
break;
case 3:
setfillcolor(LIGHTGREEN);
fillrectangle(x, y, x + 100, y + 25);
break;
}
}
}
}
void drawBoard(struct Board* pBoard)
{
setfillcolor(pBoard->color);
fillrectangle(pBoard->x, pBoard->y,
pBoard->x + pBoard->width, pBoard->y + pBoard->height);
}
void drawBall(struct Ball* pBall)
{
setfillcolor(pBall->color);
solidcircle(pBall->x, pBall->y, pBall->r);
}
做完之后我们就可以看到这样的界面了
到现在我们的基本游戏界面就已经出来了,现在差的就是判断逻辑问题了,这也是我们的重点中的重点,包括球的移动、球的弹射角度、木板的移动、砖块的消失、游戏的输赢判断都需要我们考虑到,希望大家可以好好看,好好学! 首先是木板的移动函数,我们就简单控制了,因为他只用左右移就行
//木板的按键操作
void keyDown(struct Board* pBoard)
{
//C语言: scanf函数 getch() getchar() gets()
//异步的按键操作
if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && pBoard->x >= 0)
{
pBoard->x -= pBoard->speed;
}
if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && pBoard->x <= 800 - 200)
{
pBoard->x += pBoard->speed;
}
}
接下来就是球的移动函数
void moveBall(struct Ball* pBall, struct Board* pBoard)
{
if (pBall->x - pBall->r <= 0 || pBall->x + pBall->r >= 800)
{
pBall->dx = -pBall->dx;
}
if (pBall->y - pBall->r <= 0 || hitBoard(pBall, pBoard) || hitBricks(pBall))
{
pBall->dy = -pBall->dy;
}
pBall->x += pBall->dx;
pBall->y += pBall->dy;
球的反射以及撞击木板时的判断函数
//1.反射
//2.撞击木板
int hitBoard(struct Ball* pBall, struct Board* pBoard)
{
if (pBall->y + pBall->r == pBoard->y) //y满足
{
if (pBall->x >= pBoard->x && pBall->x <= pBoard->x + pBoard->width)
{
return 1;
}
}
return 0;
}
球撞击砖块的判断函数
//3.撞击砖块
int hitBricks(struct Ball* pBall)
{
//1.算出球的行的列是属于地图
int ballJ = pBall->x / 100;
int ballI = (pBall->y - pBall->r) / 25;
//2.当前下标下,数组中不等于表示有砖块需要反射
if (ballJ < 8 && ballI < 5 && map[ballI][ballJ] != 0)
{
map[ballI][ballJ] = 0;
return 1;
}
return 0;
}
在这个过程中还需要一个定时器,我们来定义一个定时器,记住调用头文件<time.h>
int Timer(time_t num, int id)
{
static time_t start[10];
time_t end = clock();
if (end - start[id]>num)
{
start[id] = end;
return 1;
}
return 0;
}
游戏结束的判断函数
int gameOver()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (map[i][j] != 0)
{
return 0;
}
}
}
return 1;
}
最后是我们的主函数
int main()
{
srand((unsigned int)time(0)); //设置随机数的范围跟随时间改变而改变
hwnd = initgraph(800, 800);
struct Board* pBoard = createBoard(300, 800 - 25, 5, WHITE, 200, 25);
struct Ball* pBall = createBall(400, 600, 15, 5, -5, RED);
initMap();
BeginBatchDraw();
while (1)
{
cleardevice();
drawMap();
drawBoard(pBoard);
drawBall(pBall);
if (Timer(10, 0))
moveBall(pBall, pBoard);
keyDown(pBoard);
if (die(pBall))
{
MessageBox(hwnd, L"you die", L"gameOver", MB_OK);
exit(0);
}
if (gameOver())
{
MessageBox(hwnd, L"win game", L"gameOver", MB_OK);
exit(0);
}
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
感谢各位的阅读,以上就是“怎么用C语言实现小游戏打砖块”的内容了,经过本文的学习后,相信大家对怎么用C语言实现小游戏打砖块这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。