用C++实现简单的小游戏

这篇具有很好参考价值的文章主要介绍了用C++实现简单的小游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

采用面向对象的编程思想

在头文件中引入acllic图形库,实现c++控制图片以及生成可视化窗口

所需工具:

acllib图形库下载地址:acl图形库下载地址

 win32位项目的创建:通过visual studio创建win32项目

三张图片:tom.bmp,jerry.bmp,heart.bmp

游戏规则很简单,如下:

1.猫和老鼠游戏,其中包含可以加分的红心(heart)。

2.用户通过上下左右键控制老鼠jerry的移动,每当老鼠jerry与猫tom碰撞时,用户分数会下降,当老鼠jerry与红心heart碰撞时,用户分数增加。

3.tom猫的个数会随着时间增加,上限为5只,生成位置随机。

游戏没有设置获胜或者失败条件。

游戏效果图:

工程结构图:

用C++实现简单的小游戏

注:

setup()函数为acllib库里的启动函数,相当于一般c++程序中的主函数。

游戏的重点在于图片碰撞的检测算法,要考虑两张图片的位置x,y与两张图片的宽高width,height之间的关系,用if语句进行判断,若猫鼠碰撞则减分,若鼠心碰撞则加分。

要考虑猫主动碰撞鼠、鼠主动碰撞猫以及鼠主动碰撞红心3种情况。

游戏的第二重点在于猫的自动生成与运行轨迹。代码中采用计时器实现tom猫的随机生成与移动,生成位置的随机性使用rand()函数实现 。用结构体数组和循环实现五只tom猫。   开头的srand((unsigned)time(NULL));语句让rand()函数与时间无关。

 

源码:

#include "acllib.h"
#include <time.h>
#include <string>
using namespace std;
typedef struct rect
{
	int x;
	int y;
	int width;
	int  height;
}rect;
ACL_Image tomimg, jerryimg,heartimg;//变量
ACL_Color red = RED;
void timerEvent(int id);
void keyEvent(int key, int e);
int winWidth = 700, winHeight = 480;
int moveid = 0, movetime = 60;
int creatid = 1, creattime = 6000;
const int maxnum = 5;
const char* tomm = "tom.bmp";
const char* jerryy = "jerry.bmp";
const char* heartt = "heart.bmp";
void paint();
int score=0;//分数
int collision(rect r1, rect r2);

struct sprite {
	char const* name;
	int x, y;
	int distx, disty;
	int width, height;
};
struct sprite tom[maxnum], jerry , heart[maxnum];
int num = 0;


int Setup()
{
	srand((unsigned)time(NULL));
	initWindow("happy game", DEFAULT, DEFAULT, winWidth, winHeight);
	loadImage(tomm, &tomimg);
	loadImage(jerryy, &jerryimg);//
	loadImage(heartt, &heartimg);//

	tom[0].width = 100;
	tom[0].height = 100;
	tom[0].x = rand() % (winWidth - tom[0].width);
	tom[0].y = rand() % (winHeight - tom[0].height);
	tom[0].distx = tom[0].disty = 3;

	//tom[num] = (struct sprite*)malloc(sizeof(struct sprite));//?


	jerry.x = 40;
	jerry.y = 50;


	registerTimerEvent(timerEvent);
	startTimer(moveid, movetime);
	startTimer(creatid, creattime);
	//paint();

	registerKeyboardEvent(keyEvent);
	//paint();
	return 0;
}//windows编程   消息处理机制
void timerEvent(int id)
{
	
	if (id == 0)
	{
		for (int i = 0; i < maxnum; i++)
		{
			tom[i].x += tom[i].distx;
			tom[i].y += tom[i].disty;
			if (tom[i].x > (winWidth - tom[i].width) || tom[i].x <= 0) tom[i].distx = tom[i].distx * -1;
			if (tom[i].y > (winHeight - tom[i].height) || tom[i].y <= 0) tom[i].disty = tom[i].disty * -1;
		}
		for (int i = 0; i < num; ++i)
		{
			rect r1, r2;
			r1.x = jerry.x;
			r1.y = jerry.y;
			r1.width = 80;
			r1.height = 70;

			r2.x = tom[i].x;
			r2.y = tom[i].y;
			r2.width = tom[i].width;
			r2.height = tom[i].height;
			int c = collision(r1, r2);
			if (c == 1) score--;

		}//判断于tom是否主动碰撞jerry

	}
	if (id == 1)
	{
		
		 if (num < maxnum-1)
		 {
			 num++;
			 tom[num].width = 100;
			 tom[num].height = 100;
			 tom[num].x = rand() % (winWidth - tom[num].width);
			 tom[num].y = rand() % (winHeight - tom[num].height);
			 tom[num].distx = tom[num].disty = 3;
			 
			 


		 }
		 heart[num].width = 40;
		 heart[num].height = 40;
		 heart[num].x = rand() % (winWidth - tom[num].width);
		 heart[num].y = rand() % (winHeight - tom[num].height);
		 heart[num].distx = heart[num].disty = 0;
		

	}
	paint();

}

void paint()
{
	beginPaint();
	//putImage(&img,0,0);//把图片数据绘制到指定位置
	clearDevice();
	setTextSize(40);
	setTextColor(BLACK);
	paintText(1, 1, "分数:");
	char score1[20];
	sprintf_s(score1, "%d", score);
	paintText(99, 1, score1);
	setTextSize(20);
	setTextColor(BLUE);
	paintText(490, 450, "使用键盘控制jerry");
	putImageScale(&jerryimg, jerry.x, jerry.y, 80, 70);
	for (int i = 0; i <= num; i++)
	{
		putImageScale(&tomimg, tom[i].x, tom[i].y, tom[i].width, tom[i].height);
		putImageScale(&heartimg, heart[i].x, heart[i].y, heart[i].width, heart[i].height);
	}
	

	endPaint();
}


void keyEvent(int key, int e)
{
	if (e != KEY_DOWN) return;
	switch (key)
	{
	case VK_UP:
		jerry.y = jerry.y - 11;
		break;
	case VK_DOWN:
		jerry.y = jerry.y + 11;
		break;
	case VK_LEFT:
		jerry.x = jerry.x - 11;
		break;
	case VK_RIGHT:
		jerry.x = jerry.x + 11;
		break;

	}
	for (int i = 0; i < num; ++i) 
	{
		rect r1, r2;
		r1.x = jerry.x;
		r1.y = jerry.y;
		r1.width =  80;
		r1.height = 70;
		
			r2.x = tom[i].x;
			r2.y = tom[i].y;
			r2.width = tom[i].width;
			r2.height = tom[i].height;
			int c = collision(r1, r2);
			if (c ==1) score--;

	}//判断于tom是否碰撞
	for (int i = 1; i <=num; ++i)
	{
		rect r1, r2;
		r1.x = jerry.x;
		r1.y = jerry.y;
		r1.width = 80;
		r1.height = 70;

		r2.x = heart[i].x;
		r2.y = heart[i].y;
		r2.width = heart[i].width;
		r2.height = heart[i].height;
		int c = collision(r1, r2);
		if (c == 1) { score = score + 5; heart[i].width = 0; heart[i].height = 0; heart[i].x = winWidth + 10000;  }
	}//判断是否与heart碰撞
	paint();
}
int collision(rect r1, rect r2)
{
	int c = 1;
if (r1.x<r2.x && r1.x + r1.width>r2.x) {
	if (r1.y > r2.y && r1.y < r2.y + r2.height) return c;
	if (r1.y<r2.y && r1.y + r1.height>r2.y)return c;
	else {
		if (r1.x > r2.x && r2.x + r2.width > r1.x) 
		{
			if (r1.y > r2.y && r1.y < r2.y + r2.height) return c;
			if (r1.y<  r2.y && r1.y + r1.height > r2.y)return c;

		}
		c = 0; return c;



	}

}

}

至此,游戏编写完成,但存在碰撞检测偶尔不敏感,jerry可以移动出可视化窗口等问题。

 文章来源地址https://www.toymoban.com/news/detail-452602.html

到了这里,关于用C++实现简单的小游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • c语言及数据结构实现简单贪吃蛇小游戏

    目录 一·贪吃蛇简单介绍: 二·贪吃蛇的实现的开始准备: 2.1:欢迎界面的实现: 2.2地图的绘制: 2.3.1初始化蛇: 2.3.2初始化食物:  三·贪吃蛇的运行操作: 3.1辅助信息的打印: 3.2蛇的下一步移动操作: 3.2.1判断玩家按键情况: 3.2.2下一步遇到食物: 3.2.3下一步不是食物:

    2024年04月27日
    浏览(46)
  • 【Python游戏】超简单~Python实现植物大战僵尸小游戏,可以用于做毕业设计哟 | 附源码

    hello,大家好呀~ 今天给打击整一个植物大战僵尸 无广告版本 哈哈 现在的小游戏很多都是有广告,多少有点难受 今天给大家直接安排 遇到不懂的问题也可以私信小编或者↓ ↓ ↓ 源码. 点击蓝色字体领取哟~(备注:苏) 有很多的资源可以白嫖的哈,不定时会更新一下Pytho

    2024年02月05日
    浏览(35)
  • 通关大一编程实践,用C++基础和Qt实现FlappyBird小游戏

    目录 内容简介 项目要求 项目实现 素材导入 核心思路 思路的转变:从main到mainwindow 如何让游戏动起来 如何设计一个物体类 如何从键盘输入操作 如何绘制图片  如何初始化 项目源码 该项目实现了基于Qt的FlappyBird动画游戏开发,我会从素材导入开始带大家熟悉Qt开发的全过程

    2023年04月09日
    浏览(26)
  • python超简单小游戏代码,python简单小游戏代码

    大家好,小编来为大家解答以下问题,python超简单小游戏代码,python简单小游戏代码,今天让我们一起来看看吧! 大家好,我是辣条。 今天给大家带来30个py小游戏,一定要收藏! 目录 有手就行 1、吃金币 2、打乒乓 3、滑雪 4、并夕夕版飞机大战 5、打地鼠 简简单单 6、小恐

    2024年03月14日
    浏览(46)
  • python简单小游戏代码教程,python编程小游戏简单的

    大家好,小编来为大家解答以下问题,一些简单好玩的python编程游戏,python编写的入门简单小游戏,今天让我们一起来看看吧! 哈喽铁子们 表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对? 比方说,可以通过打游戏来学编

    2024年04月23日
    浏览(31)
  • python简单小游戏代码200行,python简单的小游戏代码

    大家好,小编为大家解答python简单小游戏代码200行的问题。很多人还不知道python简单的小游戏代码,现在让我们一起来看看吧! 贪吃蛇游戏是有史以来最受欢迎的街机游戏之一。在这个游戏中,玩家的主要目标是在不撞墙或不撞墙的情况下抓住最大数量的水果python基础知识点

    2024年02月02日
    浏览(50)
  • python简单小游戏代码10行,python超简单小游戏代码

    大家好,小编为大家解答python编写的入门简单小游戏代码大全的问题。很多人还不知道python编写的入门简单小游戏代码,现在让我们一起来看看吧! 玩法:上下控制起跳躲避 玩法:三个相连就能消除 玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速的。 玩法:童

    2024年02月08日
    浏览(27)
  • python简单小游戏代码10行,简单的python小游戏代码

    本篇文章给大家谈谈python简单小游戏代码200行,以及python简单小游戏代码20行,希望对各位有所帮助,不要忘了收藏本站喔。 大家好,小编来为大家解答以下问题,python编程一个最简单游戏代码,python编程游戏代码大全,今天让我们一起来看看吧! 大家好,我是辣条。 今天

    2024年01月22日
    浏览(34)
  • python简单小游戏代码100行,简单的python小游戏代码

    大家好,给大家分享一下python简单小游戏代码100行,很多人还不知道这一点。下面详细解释一下。现在让我们来看看! Source code download: 本文相关源码 大家小时候都玩过贪吃蛇吧?小编小时候可喜欢拿爸妈的手机玩了,厉害着呢!今天,小编就来用100行代码实现一个简易版的

    2024年04月14日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包