前面写了一篇C语言的打字母游戏的博客,但是未用上贴图,今天升级一下,加上贴图。即使是刚学完C语言也可以这种简单的方法做出高逼格的小游戏!
下面是源代码,关于其中一些从图形库中调用的函数,在前面讲“坦克大战地图”一篇博客中有讲解。
#include<stdio.h>
#include<conio.h>//_kbhit()
#include<graphics.h>
#include<time.h>//srand()
//播放音乐
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")//加载静态包,lib库
IMAGE background;//背景
IMAGE LB1, LB2;//LB1背景图,LB2屏蔽图
int main()
{
initgraph(1000,600);//初始画布
//加载图片
loadimage(&background,"background.jpg",1000,600);
loadimage(&LB1,"LB1.jpg",90,90);
loadimage(&LB2, "LB2.jpg", 90, 90);
//播放音乐
mciSendString("open 1.mp3 alias music",0,0,0);//打开音乐
mciSendString("play music repeat",0,0,0);//repeat循环播放音乐
int x, y;//字母坐标
char target;//字母
int key;//获取用户按键值
//char s[10] = { 0 };//输出分数
//char f[10] = { 0 };
//int score = 0;
//int wrong = 0;
srand((unsigned int)time(NULL));//生成随机种子
//循环输出字母
while (1)
{
target = 65 + rand()%26;//随机生成字母
x = rand() % 910;//字母随机在x轴出现的位置