今天用C语言+EasyX写一个键盘控制角色移动的项目
原理其实和前面写的贴图的打字母游戏差不多
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
IMAGE background, boy1, boy2;
int num = 0;
int x0 = 0;
int y0 = 450;
void InitInstance()
{
loadimage(&background, "background.jpg");//加载游戏背景
loadimage(&boy1, "boy1.bmp");//背景图
loadimage(&boy2, "boy2.bmp");//屏蔽图
}
void Interface()//初始化基本数据
{
initgraph(1024, 768);//初始化画布大小
putimage(0, 0, &background);
putimage(x0, 450, 70, 124, &boy2, 70 * num, 124 * 2, SRCAND);//角色初始位置
putimage(x0, 450, 70, 124, &boy1, 70 * num, 124 * 2, SRCPAINT);
}
void key_Down()
{
char key = _getch();
//Sleep(30);
switch (key)//获取按键
{
//向上
case 72:
case 'w':
case 'W':
putimage(0, 0, &background);
putimage(x0, y0, 70, 124, &boy2, 70 * num, 124 * 3, SRCAND);
putimage(x0, y0, 70, 124, &boy1, 70 * num, 124 * 3, SRCPAINT);
num++;
if (num == 4)
num = 0;
y0 -= 7;

这篇博客介绍如何使用C语言结合EasyX库,编写一个简单的程序,通过键盘控制角色在游戏背景上的移动。程序通过监听键盘输入,改变角色的位置,并限制其在画布范围内移动。
最低0.47元/天 解锁文章
994

被折叠的 条评论
为什么被折叠?



