用cocos2d-x(初级)实现篮球投篮小游戏,控制篮球投篮,一共三关(第二第三关使用box2d物理引擎)

这篇博客介绍了如何使用cocos2d-x开发一个包含三关的篮球投篮游戏。作者详细讲解了在不同关卡中如何控制小球运动,第一关通过简单物理公式,第二、三关利用box2d物理引擎处理碰撞和运动。项目源代码可供下载,适合cocos2d-x初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现的功能:(分关说明)
主界面:
1.点击三个不同的按钮实现界面跳转。
第一二三关:
1.用简单的物理公式在update控制小球的运动,实时刷新小球的坐标。
2.使用box2d物理引擎来制作游戏场景,控制小球的运动以及碰撞。
3.点击退出按钮返回主界面。

游戏截图:
(嘿嘿,找了个巨配的背景)
在这里插入图片描述然后就是点击第一关第二关第三关进入不同的关卡

第一关是没有使用box2d物理引擎的,大致的做法就是直接更改update里面小球的position。运用了一丢丢物理公式。

拖拽鼠标左键的时候可以控制小球的发射方向以及力度。
在这里插入图片描述在这里插入图片描述第一关没有使用物理引擎,所以小球之前、小球与物体的碰撞都需要自己写。

#include "DemoLayer.h"
#include "math.h"
#include "Box2DDemoLayer.h"
#include "HelloWorldScene.h"

#define MASS                0.4
#define Gravity             ccp(0,-9.8*100*MASS)
#define Height              500
#define Width               640
#define RadiusOfBall        36
#define LossRateOfCollide   0.8

CCScene* DemoLayer::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	DemoLayer *layer = DemoLayer::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

bool DemoLayer::init()
{
	if (!CCLayer::init())
	{
		return false;
	}
	//    
	size = CCDirector::sharedDirector()->getVisibleSize();
	left = (size.width - Width) / 2;
	right = (size.width + Width) / 2;
	bottom = (size.height - Height) / 2;
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	this->setTouchEnabled(true);

	CCLabelTTF* pLabel = CCLabelTTF::create("The first level(don't use Box2D)", "Arial", 24);

	// position the label on the center of the screen
	pLabel->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - pLabel->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(pLabel, 1);

	//按钮的代码
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"blackBtnbackground.png",//正常状态的图片,系统自带的
		"blackBtnbackground.png",//被点击的图片
		this,
		menu_selector(DemoLayer::menuCloseCallback));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2 - 10,
		origin.y + pCloseItem->getContentSize().height / 2 + 10));

	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu);
	//这里的退出按钮由于找不到合适的素材,我就用文本代替了,然后按钮的图片是用一张黑色的图片放在按钮上,所以看不出来。
	CCLabelTTF* pLabel1 = CCLabelTTF::create("Exit", "Arial", 30);
	pLabel1->setPosition(ccp(origin.x + visibleSize.width - 35, origin.y + pLabel1->getContentSize().height - 15));
	this->addChild(pLabel1, 1);

	//用纹理缓存(资源池)加载纹理  这里的纹理就是一个点
	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("texball.png");

	//创建批处理渲染方式的精灵节点
	CCSpriteBatchNode *mgr = CCSpriteBatchNode::createWithTexture(texture);
	//添加到层中
	this->addChild(mgr);
	background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 640, 500));

	//创建背景
	background->setAnchorPoint(ccp(0, 0));
	background->setPosition(ccp(left, bottom));
	mgr->addChild(background);

	//开始箭头位置
	startArea = CCSprite::createWithTexture(texture, CCRectMake(647, 124, 100, 100));
	startArea->setPosition(ccp(left + 80, bottom + 150));
	mgr->addChild(startArea);

	//箭头
	arrow = CCSprite::createWithTexture(texture, CCRectMake(0, 500, 690, 120));
	arrow->setPosition(startArea->getPosition());
	arrow->setScale(0.1);
	mgr->addChild(arrow);

	//小球
	ball = CCSprite::createWithTexture(texture, CCRectMake(654, 0, 72, 72));
	ball->setPosition(startArea->getPosition());
	mgr->addChild(ball);

	//篮板
	basketry = CCSprite::createWithTexture(texture, CCRectMake(656, 86, 114, 24));
	basketry->setPosition(ccp(left + 550, bottom + 300));
	mgr->addChild(basketry);

	//篮筐
	target = CCSprite::createWithTexture(texture, CCRectMake(648, 272, 16, 160));
	target->setPosition(ccp(left + 600, bottom + 350));
	mgr->addChild(target);

	//铁三角
	trangle = CCSprite::createWithTexture(texture, CCRectMake(760, 0, 90, 90));
	trangle->setPosition(ccp(left + Width - 45, bottom + 45));
	mgr->addChild(trangle);


	isShoot = false;
	//加速度为Gravity
	acceleration = Gravity;
	//篮筐左点和右点
	basketryLeftPos = ccp(left + 493, bottom + 300);//篮筐左
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值