티스토리 뷰

* 인프런에 있는 "홍정모의 게임 만들기 연습 문제 패키지" 강의를 바탕으로 작성된 글입니다.

 

1.움직이는 마우스 커서 위치에 회전하는 별 그려보기

namespace shyplants
{
	class MouseExample : public Game2D
	{
	private:
		float time = 0.0f;

	public:
		void update() override
		{
			const vec2 mouse_pos = getCursorPos();
			
			translate(mouse_pos);                      // 실행순서 3. 마우스 위치로 제자리 회전중인 별이 이동한다.
			rotate(time * 60.0f);                      // 실행순서 2. 제자리에서 별이 회전한다.
			drawFilledStar(Colors::gold, 0.2f, 0.1f);  // 실행순서 1. 원점에 별을 그린다.

			time += getTimeStep();
		}
	};
}

 

2. 파란 원을 하나 그리고 마우스가 그 원에 들어갔을 경우 빨간색으로 변경하기

namespace shyplants
{
	class MouseExample : public Game2D
	{
	private:
		float time = 0.0f;
		float radius = 0.3f;   // 원의 반지름

	public:
		void update() override
		{
			const vec2 mouse_pos = getCursorPos();                                     
			if (mouse_pos.x*mouse_pos.x + mouse_pos.y*mouse_pos.y <= radius * radius)  
			{
				drawFilledCircle(Colors::red, radius);       // 마우스 좌표(x,y)가 원 안에 위치할 때 빨간색 원 그리기
			}
			else
			{
				drawFilledCircle(Colors::blue, radius);      // 마우스 좌표(x,y)가 원 안에 위치하지 않을시 파랑색 원 그리기
			}
			
			time += getTimeStep();
		}
	};
}

3. 화면 가운데 물체를 커서방향으로 회전하기 & 마우스 버튼을 누르면 커서 방향으로 총알을 발사하기

namespace shyplants
{
	class MyBullet
	{

	public:
		vec2 center = vec2(0.0f, 0.0f);
		vec2 velocity = vec2(0.0f, 0.0f);

		void draw()
		{
			beginTransformation();
			translate(center);
			drawFilledRegularConvexPolygon(Colors::yellow, 0.02f, 8);
			drawWiredRegularConvexPolygon(Colors::gray, 0.02f, 8);
			endTransformation();
		}

		void update(const float& dt)
		{
			center += velocity * dt;
		}
	};

	class MouseExample : public Game2D
	{
	private:
		float time = 0.0f;
		float radius = 0.1f;   
		float angle, rad;           
		std::list<MyBullet*> bulletList;
		
	public:
		bool OOB(vec2& pos)
		{
			return pos.y < -1.0f || pos.y > 1.0f || pos.x < -1.0f || pos.x > 1.0f;
		}

		void update() override
		{
			const vec2 mouse_pos = getCursorPos(true);

			beginTransformation();
			{
				if (mouse_pos.x != 0.0f) {
					rad = atan2f(mouse_pos.y, mouse_pos.x);
					angle = 180.0f / 3.141592f * rad;
					rotate(angle);
				}
				translate(vec2(0.1f, 0));
				drawFilledBox(Colors::blue, 0.2f, 0.05f);
			}
			endTransformation();
			drawFilledCircle(Colors::blue, radius);

			if (isMouseButtonPressedAndReleased(GLFW_MOUSE_BUTTON_1))   // 왼쪽 마우스버튼 클릭시 마우스 좌표 각도에 따른 총알 발사
			{
				if (mouse_pos.x != 0.0f)
				{
					bulletList.push_back(new MyBullet);
					bulletList.back()->center = vec2(0, 0);
					bulletList.back()->center.x += (radius + 0.1f) * cos(rad);
					bulletList.back()->center.y += (radius + 0.1f) * sin(rad);
					bulletList.back()->velocity = vec2(2.0f*cos(rad), 2.0f*sin(rad));
				}
			}

			// 총알 리스트를 순회하며 업데이트, 드로우 함수 호출
			for (auto iter = bulletList.begin(); iter != bulletList.end(); iter++)
			{
				(*iter)->update(getTimeStep());
				(*iter)->draw();
			}

			// 총알 리스트를 순회하며 화면 밖에 나간 총알 메모리 해제 (메모리 누수 방지)
			for (auto iter = bulletList.begin(); iter != bulletList.end();)
			{
				if (OOB((*iter)->center)) {
					delete *iter;
					iter = bulletList.erase(iter);
				}
				else {
					iter++;
				}
			}

			time += getTimeStep();
		}
	};
}

댓글