raylib:flappy_bird_게임_장애물_만들기
flappy bird 게임 장애물 만들기
문서의 이전 판입니다!
준비
- 이전 기사는 한글 출력하기이다.
- 스타팅 파일은 Flappy Bird에 장애물 만들기 스타팅 파일이다.
장애물 클래스 만들기
1. 개념
플래피 버드에서는 파이프 장애물이 존재한다. 이를 네모난 박스로 구조화하면 다음과 같다.
위와 아래의 장애물 좌표를 구하려면 위의 장애물 높이와, 아래 장애물 높이, 그리고 중간 빈 공간의 높이가 필요하다.
위의 장애물 높이와 중간 빈 공간의 높이를 정해 놓으면 아래 장애물 높이는 전체 화면 높이에서 빼면 될 것이다.
굳이 서식으로 해보자면
아래장애물 높이 = 전체 화면 높이 - (위 장애물 높이 + 중간 빈 공간의 높이)
이다.
2. obstacle.h
- obstacle.h
#pragma once #include "raylib.h" enum class ObstacleType { top = 0, bottom, both }; typedef struct Box { int x; int y; int bWidth; int bHeight; } Box ; class Obstacle { public: void Init(); void Update(); void Draw(); int GetPipePosition(); private: Box upPipe; Box downPipe; int upPipeHeight; int width; int middleHeight; int speed = 2; ObstacleType type = ObstacleType::top; };
장애물은 네모난 박스로 그릴 것이므로 int형의 박스 구조체를 만들었다. 그리고 장애물의 종류를 설정했다. 위에 달린 장애물, 아래에 달린 장애물 위와 아래 모두에 달린 장애물의 세 가지 종류를 만들었다.
3. obstacle.cpp
- obstacle.cpp
#include "obstacle.h" void Obstacle::Init() { int typeValue = GetRandomValue(0, 2); // 장애물 타입 0은 top, 1은 bottom, 2는 both type = ObstacleType(typeValue); // 장애물 타입을 랜덤하게 설정함 width = GetRandomValue(100, 150); // 파이프 너비는 위와 아래 파이프가 동일 하다 upPipeHeight = GetRandomValue(250, 400); // 위 파이프의 높이 middleHeight = GetRandomValue(300, 400); // 위와 아래 파이프 사이의 공간 upPipe = {GetScreenWidth(), 0, width, upPipeHeight}; downPipe = {GetScreenWidth(), upPipeHeight + middleHeight, width, GetScreenHeight() - (upPipeHeight + middleHeight)}; } void Obstacle::Update() { upPipe.x -= speed; downPipe.x -= speed; } // 파이프가 화면 왼쪽끝을 넘어가면 지우기 위해 왼쪽 끝의 값을 가져옴 int Obstacle::GetPipePosition() { return upPipe.x + upPipe.bWidth; } void Obstacle::Draw() { if (type == ObstacleType::top) { DrawRectangle(upPipe.x, upPipe.y, upPipe.bWidth, upPipe.bHeight, DARKGREEN); }else if (type == ObstacleType::bottom) { DrawRectangle(downPipe.x, downPipe.y, downPipe.bWidth, downPipe.bHeight, DARKGREEN); }else { DrawRectangle(upPipe.x, upPipe.y, upPipe.bWidth, upPipe.bHeight, DARKGREEN); DrawRectangle(downPipe.x, downPipe.y, downPipe.bWidth, downPipe.bHeight, DARKGREEN); } }
가. Init() 함수
장애물은 랜덤하게 위, 아래, 양쪽을 나오게 했다. 따라서 GetRandomValue()함수를 이용하여 랜덤 값을 처음에 만들어 준다.
각 장애물(파이프)의 높이도 랜덤하게 만들 것이므로 랜덤 하게 변수를 설정하게 했다. 이후에 위와 아래 파이프의 상자를 그리기 위하여 4개의 값을 요소로 하는 구조체에 넣어주게 하였다.
나. Update() 함수
왼쪽으로만 움직이게 할 것이므로 x 값만 바꿔주면 된다.
다. GetPipePosition() 함수
raylib/flappy_bird_게임_장애물_만들기.1697108259.txt.gz · 마지막으로 수정됨: 2023/10/12 19:57 (바깥 편집)
로그인