龙空技术网

程序员如何用代码的方式过中秋?看看170行代码写的中秋游戏

C语言编程 471

前言:

今天姐妹们对“c语言头文件conioh”大体比较注重,小伙伴们都需要知道一些“c语言头文件conioh”的相关知识。那么小编也在网摘上搜集了一些对于“c语言头文件conioh””的相关知识,希望兄弟们能喜欢,我们一起来了解一下吧!

程序员没有诗人那么的满腹经纶,能吟诗作曲,写出“明月几时有?把酒问青天。”的千古诗文;

没有达官贵人那般隆重庆祝,花费重金装饰庭院,摆满灯笼饰品,端上精美小盒的月饼、水果,美美享受中秋佳节。

但是,程序员的中秋,有代码相伴,一行行代码仿若诗词,显示出来的效果也是惊艳绝伦,下面带领大家,看看程序员如何用代码的方式过中秋吧!

今天我们要实现的月饼程序实际效果如下:

中秋佳节,月饼怎能少。其中月饼有各种各样的口味,绿豆味月饼、柠檬味月饼、蛋黄味月饼、莲蓉味月饼等等。

这是一款玉兔吃月饼的游戏。其中,各种月饼会随机从上方掉落,每一种月饼都有不一样的分数值,我们可以通过点击各种月饼吃掉它,来让兔兔获取分数。中秋佳节跟家里弟弟妹妹一起玩玩你亲手做出来的吃月饼游戏,不亦说乎。

不过在正式开始敲代码之前,我们还是要有所准备:

编译器:VS2019/2022【可选VC6.0,VScode(需要自己搭配环境)】

图形库插件:easyX图形库插件(可在我粉丝群文件直接下载)

需要准备的图片+音频素材:(文末获取)

准备好所有的东西之后,我们就来写代码吧!!

上面是必须要用到的一些头文件,大家千万不要因为缺少头文件导致程序报错二焦灼。

下面就是我们全部的源码展示了:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <graphics.h>#include <time.h>#include <mmsystem.h>#pragma comment(lib,"winmm.lib")  //加载静态库#define MAX 15IMAGE background;IMAGE moon[2];int count[4];struct Moon{	int x;				//字母的x坐标	int y;				//字母的y坐标	int type;			//存储当前字母	int speed;}; struct Moon moonCake[MAX];void loadResource(){	loadimage(&background, "img/background.jpg");	loadimage(moon+0, "img/月饼_y.jpg", 300, 300);	loadimage(moon+1, "img/月饼.jpg", 300, 300);}void drawMoonCake(struct Moon moonCake) {	int j = moonCake.type % 2;	int i = moonCake.type / 2;	int w = moon[i].getwidth() / 2;	int h = moon[i].getheight() / 2;	int xx = j * w;	int yy = i * h;	putimage(moonCake.x, moonCake.y, w, h, moon + 0, xx, yy,SRCAND);	putimage(moonCake.x, moonCake.y, w, h, moon + 1, xx, yy,SRCPAINT);}void initMoonCake(struct Moon moonCake[], int index){	moonCake[index].x = rand() % 6*200 + 200;	moonCake[index].y = rand() % 401 - 600;		moonCake[index].type = rand()%4; 	moonCake[index].speed = rand() % 15 + 1;}void outtextxy_result(int x, int y, int count[]){	settextstyle(25, 0, "FZZJ-XHFTJW");	setbkmode(TRANSPARENT);	const char* p[4] = { {"水果味月饼数:"},{"柠檬味月饼数:"},		{"绿豆味月饼数:"},{"豆沙味月饼数:"}};	for (int i = 0; i < 4; i++) 	{		char str[50] = "";		sprintf(str, "%s%d", p[i], count[i]);											outtextxy(x, y, str);		y += 30;	}}DWORD WINAPI playMusic(LPVOID lpvoid){	mciSendString("close img/爆炸.mp3", 0, 0, 0);	mciSendString("open img/爆炸.mp3", 0, 0, 0);	mciSendString("play img/爆炸.mp3 wait", 0, 0, 0);	mciSendString("close img/爆炸.mp3", 0, 0, 0);	return 0;}int Timer(int duration) {	static int start = 0;	int end = clock();	if (end - start > duration) 	{		start = end;		return 1;	}	return 0;}void keyDown(){	BeginBatchDraw();	//enum MoonType {Fruits,Lemon,Nung,Puree};	while (1) 	{		putimage(0, 0, &background);		for (int i = 0; i < MAX; i++)		{			drawMoonCake(moonCake[i]);			if (moonCake[i].y >= background.getheight())			{				initMoonCake(moonCake, i);			}		}			if (Timer(50)) 		{			for (int i = 0; i < MAX; i++)			{				moonCake[i].y += moonCake[i].speed;			}		}		if (_kbhit())		{			int userKey = _getch();			switch (userKey) 			{			case 'F':			case 'f':				userKey = 0;				break;			case 'L':			case 'l':				userKey = 1;				break;			case 'N':			case 'n':				userKey = 2;				break;			case 'P':			case 'p':				userKey = 3;				break;			}			//暂停			if (userKey == ' ')			{				//让他陷入输入的死循环,直到下次按下是空格				while (_getch() != ' ');			}			//退出			if (userKey == '\r')			{				break;			}			//玩游戏			for (int i = 0; i < MAX; i++)			{				if (userKey == moonCake[i].type)				{					initMoonCake(moonCake, i);					count[userKey]++;					CreateThread(NULL, NULL, playMusic, NULL, NULL, NULL);					break;			//每次只消除一个				}			}		}		outtextxy_result(1200, 600,count);		FlushBatchDraw();	}		EndBatchDraw();}int main(){	//srand(80);  含义用来确定范围的 80-正整数范围	srand((unsigned int)time(NULL));	//随机函数绑定时间		loadResource();	initgraph(background.getwidth(), background.getheight());				 //创建窗口	//初始化每一个字母的属性	for (int i = 0; i < MAX; i++)	{		initMoonCake(moonCake, i);	}	keyDown();	//系统推出时候需要保存	closegraph();						 //关闭窗口	return 0;}

大家快跟着源码去实现你的“月饼大作战”吧!

如果大家想好好学习C/C++的话,为了帮助大家,轻松,高效学习C语言/C++,给大家分享我收集的资源,从最零基础开始的,帮助大家在学习C语言的道路上披荆斩棘!

编程学习书籍分享:

编程学习视频分享:

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!

对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

标签: #c语言头文件conioh #c语言编码程序 #vs2019怎么写c程序