返回首页
  主页 > 编程 > c语言 >

C++实现游戏人物行走跑动攻击效果

2019-07-26 13:00 阅读:107 来源:智宇SEO自媒体

之前智宇用C语言写过控制游戏人物上下左右四个方向的行走。今天用C++游戏引擎实现行走加攻击的效果

class Hero
	: public Sprite
{
	FramesPtr	run_frames_up;          /* 走路序列帧上 */
	FramesPtr	run_frames_left;        /* 走路序列帧左 */
	FramesPtr	run_frames_right;       /* 走路序列帧右 */
	FramesPtr	run_frames_down;        /* 走路序列帧下 */
	FramesPtr	stand_frames1;          /* 站立左 */
	FramesPtr	stand_frames2;          /* 站立右 */
	FramesPtr	stand_frames3;          /* 站立上 */
	FramesPtr	stand_frames4;          /* 站立下 */
	FramesPtr	attack_frames1;         /* 攻击序列帧1 */
	FramesPtr	attack_frames2;         /* 攻击序列帧2 */
	FramesPtr	attack_frames3;         /* 攻击序列帧2 */
	FramesPtr	attack_frames4;         /* 攻击序列帧2 */
	int		facing;                 /* 方向 */
	bool		running;                /* 是否正在跑步 */
	Direction	running_direction;      /* 跑步方向 */
	bool		attackStatus;           /* 是否在攻击 */


public:
	Hero()
	{
		ImagePtr	image		= new Image( L"res/gy.png" );
		ImagePtr	image1		= new Image( L"res/gy_melee.png" );
		Size		source_size	= image->GetSize();
		Size		source_size1	= image1->GetSize();
		Size		frame_size	= { source_size.x / 4, source_size.y / 4 };
		Size		frame_size1	= { source_size1.x / 4, source_size1.y / 4 };

		run_frames_up = new Frames;
		for ( int i = 0; i < 4; ++i )
		{
			Point		pos	= { (i) * frame_size.x, (3) * frame_size.y };
			ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
			run_frames_up->Add( frame );
		}

		run_frames_left = new Frames;
		for ( int i = 0; i < 4; ++i )
		{
			Point		pos	= { (i) * frame_size.x, (1) * frame_size.y };
			ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
			run_frames_left->Add( frame );
		}

		run_frames_right = new Frames;
		for ( int i = 0; i < 4; ++i )
		{
			Point		pos	= { (i) * frame_size.x, (2) * frame_size.y };
			ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
			run_frames_right->Add( frame );
		}

		run_frames_down = new Frames;
		for ( int i = 0; i < 4; ++i )
		{
			Point		pos	= { (i) * frame_size.x, (0) * frame_size.y };
			ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
			run_frames_down->Add( frame );
		}


		/* 英雄站立 左 和 下 */
		{ stand_frames1 = new Frames;
		  Point		pos	= { 0, 0 };
		  ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
		  stand_frames1->Add( frame ); }
		/* 英雄站立 右 */
		{ stand_frames2 = new Frames;
		  Point		pos	= { 0, 2 * frame_size1.y };
		  ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
		  stand_frames2->Add( frame ); }

		/* 英雄站立 上 */
		{ stand_frames3 = new Frames;
		  Point		pos	= { 0, 3 * frame_size1.y };
		  ImagePtr	frame	= new Image( L"res/gy.png", Rect { pos, frame_size } );
		  stand_frames3->Add( frame ); }

		/* 英雄攻击一左 */
		attack_frames1 = new Frames;
		for ( int i = 0; i < 4; i++ )
		{
			Point		pos	= { (i) * frame_size1.x, (1) * frame_size1.y };
			ImagePtr	frame	= new Image( L"res/gy_melee.png", Rect { pos, frame_size1 } );
			attack_frames1->Add( frame );
		}
		/*下 */
		attack_frames2 = new Frames;
		for ( int i = 0; i < 4; i++ )
		{
			Point		pos	= { (i) * frame_size1.x, (0) * frame_size1.y };
			ImagePtr	frame	= new Image( L"res/gy_melee.png", Rect { pos, frame_size1 } );
			attack_frames2->Add( frame );
		}
		/* 右 */
		attack_frames3 = new Frames;
		for ( int i = 0; i < 4; i++ )
		{
			Point		pos	= { (i) * frame_size1.x, (2) * frame_size1.y };
			ImagePtr	frame	= new Image( L"res/gy_melee.png", Rect { pos, frame_size1 } );
			attack_frames3->Add( frame );
		}

		/*上 */
		attack_frames4 = new Frames;
		for ( int i = 0; i < 4; i++ )
		{
			Point		pos	= { (i) * frame_size1.x, (3) * frame_size1.y };
			ImagePtr	frame	= new Image( L"res/gy_melee.png", Rect { pos, frame_size1 } );
			attack_frames4->Add( frame );
		}
		/* 执行动画 */
		AddAction(
			Tween::Animation( stand_frames1 )
			.SetDuration( 2000 )
			.SetLoops( -1 )
			);

		/* 添加按键监听 */
		AddListener( Event::KeyDown, MakeClosure( this, &Hero::OnKeyDown ) );
		AddListener( Event::KeyUp, MakeClosure( this, &Hero::OnKeyUp ) );

		/* 默认方向为 Left */
		facing		= 1; /* 左边 */
		running		= false;
		attackStatus	= false;
		/* 设置锚点 */
		SetAnchor( 0.5f, 0.5f );
	}


	void OnKeyDown( Event const & e )
	{
		if ( e.key.code == KeyCode::A )
			Run( Direction::Left );
		else if ( e.key.code == KeyCode::D )
			Run( Direction::Right );
		else if ( e.key.code == KeyCode::W )
			Run( Direction::Up );
		else if ( e.key.code == KeyCode::S )
			Run( Direction::Down );
		else if ( e.key.code == KeyCode::K )
			attack();  /* 攻击序列帧1 */
	}


	void OnKeyUp( Event const & e )
	{
		switch ( e.key.code )
		{
		case KeyCode::A:
		case KeyCode::D:
		case KeyCode::S:
		case KeyCode::W:
			StopRun();
			break;
		case KeyCode::K:

			/* Stopattack(); */
			break;
		}
	}


	void attack()
	{
		/* 执行动画 */
		if ( facing == 1 )
		{
			AddAction(
				Tween::Animation( stand_frames1 )
				.SetDuration( 2000 )
				.SetLoops( -1 )
				);
		}

		if ( facing == 2 )
		{
			AddAction(
				Tween::Animation( stand_frames2 )
				.SetDuration( 2000 )
				.SetLoops( -1 )
				);
		}
		if ( facing == 3 )
		{
			AddAction(
				Tween::Animation( stand_frames3 )
				.SetDuration( 2000 )
				.SetLoops( -1 )
				);
		}

		if ( facing == 4 )
		{
			AddAction(
				Tween::Animation( stand_frames1 )
				.SetDuration( 2000 )
				.SetLoops( -1 )
				);
		}
		if ( !attackStatus )
		{
			attackStatus = true;
			if ( facing == 1 )
			{
				AddAction(
					Tween::Animation( attack_frames1 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}else if ( facing == 2 )
			{
				AddAction(
					Tween::Animation( attack_frames3 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}else if ( facing == 3 )
			{
				AddAction(
					Tween::Animation( attack_frames4 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}else if ( facing == 4 )
			{
				AddAction(
					Tween::Animation( attack_frames2 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}
		}
		attackStatus = false;
	}


	void Run( Direction d )
	{
		if ( !running )
		{
			running = true;
			StopAllActions();

			/* 执行跑步动画 */
			running_direction = d;
			if ( running_direction == Direction::Left )
			{
				AddAction(
					Tween::Animation( run_frames_left )
					.SetDuration( 1000 )
					.SetLoops( -1 )
					);

				facing = 1;
			}else if ( running_direction == Direction::Right )
			{
				AddAction(
					Tween::Animation( run_frames_right )
					.SetDuration( 1000 )
					.SetLoops( -1 )
					);


				facing = 2;
			}else if ( running_direction == Direction::Up )
			{
				AddAction(
					Tween::Animation( run_frames_up )
					.SetDuration( 1000 )
					.SetLoops( -1 )
					);
				facing = 3;
			}else if ( running_direction == Direction::Down )
			{
				AddAction(
					Tween::Animation( run_frames_down )
					.SetDuration( 1000 )
					.SetLoops( -1 )
					);
				facing = 4;
			}
		}
	}


	void StopRun()
	{
		if ( running )
		{
			running = false;
			StopAllActions();

			/* 执行站立动画 */
			if ( facing == 1 )
			{
				AddAction(
					Tween::Animation( stand_frames1 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}else if ( facing == 2 )
			{
				AddAction(
					Tween::Animation( stand_frames2 )
					.SetDuration( 1000 )
					.SetLoops( 0 )
					);
			}else if ( facing == 3 )
			{
			}else if ( facing == 4 )
			{
			}
		}
	}


	void OnUpdate( Duration dt )
	{
		if ( running )
		{
			/* 计算移动距离 */

			const float	moving_per_sec	= 150;
			const float	distance	= moving_per_sec * dt.Seconds();

			switch ( running_direction )
			{
			case Direction::Up:
				Move( 0, -distance );
				break;
			case Direction::Down:
				Move( 0, distance );
				break;
			case Direction::Left:
				Move( -distance, 0 );
				break;
			case Direction::Right:
				Move( distance, 0 );
				break;
			}
		}
	}
};



返回首页
利博娱乐计划 利博娱乐官网 利博娱乐APP 利博娱乐走势图 利博娱乐下载 利博娱乐官方 利博娱乐网站 利博娱乐怎么玩 利博娱乐官方 利博娱乐靠谱吗 利博娱乐代理 利博娱乐骗局 利博娱乐投注 利博娱乐倍率 利博娱乐开奖 利博娱乐漏洞