160行flappy bird

/*
山寨版 flappy bird
使用C风格直接调用cocos2dx
如果改成C++风格会更清晰
*/

import rcc.h

mac c_bot_y 80
rbuf<void*> g_list
int g_score

main
{
	cc.set_design_size(480,800)
	cc.set_display_state(false)
	cc.add_cache('flappy')
	cc.add_animations('frame.plist')
	cc.jump_scene(&game)
}

game(void* parent)
{
	g_list.clear
	g_score=0
	
	void* bg=get_bg
	cc.add_child(parent,bg)

	void* score=cc.bmf_create('Number24.fnt','0')
	cc.set_pos(score,240,400+240)
	cc.set_scale(score,2)
	cc.add_child(parent,score,5)
	
	void* bot=cc.sprite_create('ground.png')
	cc.set_pos(bot,280,c_bot_y)
	cc.add_child(parent,bot,2)
	cc.run_ac(bot,cca.repeat(cca.sequence(
		cca.move_to(0.5,195,c_bot_y),
		cca.move_to(0,280,c_bot_y)
	)))
	
	void* bird=cc.sprite_create('bird1.png')
	cc.set_pos(bird,120,400)
	cc.add_child(parent,bird,3)
	cc.run_ac(bird,bird_ac)
	
	cc.run_ac(parent,cca.schedule(0.1,lambda(void* pthis){
		if hit(bird)
			over(parent,bird,bg,bot)
		check(score,bird)
	}))
	cc.run_ac(bg,cca.schedule(1.5,lambda(void* pthis){
		y=180.0+cc.rand(160)
		add_hose(parent,'hold1.png',y)
		add_hose(parent,'hold2.png',y+432.0+185.0)
	}))
	cc.touch_call(bg,lambda(xpoint pt){
		bird_touch(bird)
	})
}

add_hose(void* parent,rstr& name,double y)
{
	hose=cc.sprite_create(name)
	g_list.push(hose)
	cc.set_pos(hose,480+35,y)
	cc.run_ac(hose,cca.sequence(
		cca.move_to(3.24,-35,y),
		cca.call_func(&hose_remove)
	))
	cc.add_child(parent,hose,1)
}

over(void* parent,void* bird,void* bg,void* bot)
{
	cc.play_effect('hit')
	label=cc.sprite_create('over.png')
	cc.set_pos(label,240,400)
	cc.add_child(parent,label,5)
	cc.touch_call(label,lambda(xpoint pt){
		cc.jump_scene(&game)
	})
	for i in g_list
		cc.stop_ac(g_list[i])
	cc.stop_ac(bird)
	cc.stop_ac(bg)
	cc.stop_ac(bot)
	cc.stop_ac(parent)
}

check(void* score,void* bird)
{
	for i in g_list
		if cc.get_tag(g_list[i])==1
			continue
		if cc.get_pos(bird).x<cc.get_pos(g_list[i]).x
			continue
		cc.set_tag(g_list[i],1)
		g_score++
		cc.play_effect('point')
		cc.bmf_set(score,g_score.torstr)
		if i+1>=g_list.count
			continue
		diff=cc.get_pos(g_list[i+1]).x-cc.get_pos(g_list[i]).x
		if diff.abs>20.0
			continue
		cc.set_tag(g_list[i+1],1)
}

bool hit(void* bird)
{
	for i in g_list
		if cc.rect_hit(g_list[i],bird)
			return true
	return false
}

void* get_bg
{
	bg=cc.sprite_create('bg.png')
	cc.set_pos(bg,240,400)
	return bg
}

hose_remove(void* hose)
{
	for i in g_list
		if g_list[i]==hose
			g_list.erase(i)
			break
	cc.remove_from_parent(hose)
}

bird_touch(void* bird)
{
	cc.play_effect('wing')
	pos=cc.get_pos(bird)
	rise=cca.spawn(
		cca.ease_out(cca.move_by(0.3,0.0,100.0),1.5),
		cca.rotate_to(0.2,-20.0)
	)
	fall=cca.spawn(
		cca.ease_in(cca.move_to(pos.y/1000.0,pos.x,200.0),1.5),
		cca.sequence(
			cca.delay(pos.y/3600.0),
			cca.rotate_to(0.15,60.0)
	))
	action=cca.spawn(
		bird_ac,
		cca.sequence(rise,cca.delay(0.1),fall)
	)
	cc.stop_ac(bird)
	cc.run_ac(bird,action)
}

void* bird_ac
{
	return cca.repeat_times(cc.animate_create(cc.get_animation('fly')),20)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值