1) 프로젝트 생성하기
Qt Creator를 실행합니다.
- File > New File or Project로 이동합니다.
- Application > Qt Widgets Application을 선택하고 Next를 클릭합니다.
- 프로젝트 이름을 예를 들어 RockPaperScissors로 설정하고 저장할 경로를 지정합니다.
- Build System은 qmake를 선택합니다. 다른 선택지들은 기본 설정으로 합니다.
- Kit 선택 화면에서는 Desktop Qt 6.5.3 MinGW 64-bit으로 두고 Next를 클릭합니다.(윈도우 환경에 따라 32bit, 64bit 선택)
- 마지막으로 Finish를 클릭하여 프로젝트를 생성합니다.
2) UI 디자인 - 간단한 GUI 만들기
Projects -> RockPaperScissors -> Forms -> mainwindow.ui
mainwindow.ui 파일을 열고 디자인 모드로 이동합니다.
다음과 같이 위젯을 배치합니다:
- Label: "Choose your move:"
- 3 PushButtons: "Rock", "Paper", "Scissors"
- Label: "Result"
- PushButton: "Play Again"
각 버튼의 이름을 수정합니다:
- "Rock" 버튼의 objectName: btnRock
- "Paper" 버튼의 objectName: btnPaper
- "Scissors" 버튼의 objectName: btnScissors
- "Play Again" 버튼의 objectName: btnPlayAgain
- 결과를 표시할 Label의 objectName: lblResult
- 수정 후 Ctrl+s를 눌러 저장해준다.
3) C++ 코드 작성 - 가위바위보 게임 로직
mainwindow.h 파일에 **슬롯(Slot)**을 정의합니다:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void playGame(const QString &playerChoice);
void on_btnRock_clicked();
void on_btnPaper_clicked();
void on_btnScissors_clicked();
void on_btnPlayAgain_clicked();
private:
Ui::MainWindow *ui;
QString computerChoice();
QString determineWinner(const QString &player, const QString &computer);
};
#endif // MAINWINDOW_H
mainwindow.cpp 파일에 로직 구현:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cstdlib> // For rand() function
#include <ctime> // For seeding random number generator
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
std::srand(std::time(nullptr)); // Seed the random number generator
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::computerChoice()
{
int choice = std::rand() % 3; // 0: Rock, 1: Paper, 2: Scissors
if (choice == 0) return "Rock";
else if (choice == 1) return "Paper";
else return "Scissors";
}
QString MainWindow::determineWinner(const QString &player, const QString &computer)
{
if (player == computer) return "It's a tie!";
if ((player == "Rock" && computer == "Scissors") ||
(player == "Paper" && computer == "Rock") ||
(player == "Scissors" && computer == "Paper"))
return "You win!";
else
return "You lose!";
}
void MainWindow::playGame(const QString &playerChoice)
{
QString compChoice = computerChoice();
QString result = determineWinner(playerChoice, compChoice);
ui->lblResult->setText("Computer chose: " + compChoice + "\n" + result);
}
void MainWindow::on_btnRock_clicked()
{
playGame("Rock");
}
void MainWindow::on_btnPaper_clicked()
{
playGame("Paper");
}
void MainWindow::on_btnScissors_clicked()
{
playGame("Scissors");
}
void MainWindow::on_btnPlayAgain_clicked()
{
ui->lblResult->setText("Choose your move:");
}
4) 프로그램 빌드 및 실행
- Ctrl + R을 눌러 프로그램을 빌드하고 실행합니다.
- GUI가 나타나면 가위, 바위, 보 버튼을 눌러 게임을 플레이할 수 있습니다.
5) EXE 파일로 빌드하기
1.릴리즈 모드로 전환
- Qt Creator에서 좌측 Project 메뉴 > Build&Run > Build > Build Settings로 이동합니다.
- Release 모드를 선택합니다.
- General의 Shadow build를 체크해제합니다.
2.빌드 실행
- Ctrl + B를 눌러 프로그램을 빌드합니다.
- 빌드가 완료되면 D:\qt\RockPaperScissors\release 경로의 release 폴더에 EXE 파일이 생성됩니다.
- 프로젝트 경로는 처음 프로젝트 만들 때, Create in으로 입력했던 경로에 있다.
6) EXE 파일 배포 준비
1. 환경변수 path에 'C:\Qt\6.5.3\mingw_64\bin' 추가 후 저장한다. -> cmd 창 또는 powershell 창을 끄고 다시 켠다. (windeplyqt.exe를 사용하기 위함)
2. release 폴더에 생성된 EXE 파일을 확인합니다.
3. DLL 의존성 복사
CMD(명령 프롬프트)에서 다음 명령어를 실행합니다:
- D:\qt\RockPaperScissors\release 경로로 이동 후 아래의 명령어를 입력한다.
windeployqt --release RockPaperScissors.exe
windeployqt.exe RockPaperScissors.exe
이 명령어를 실행하면 EXE 파일과 함께 필요한 DLL 파일들이 자동으로 복사됩니다.
7) 알집 파일로 압축해서 배포하기
- D:\qt\RockPaperScissors\release 파일 전체를 .7z로 압축하여 배포한다.
- RockPaperScissors.exe 실행을 위해선 dll, 하위 폴더 모두 필요하다.
- 다른 경로에서 알집을 풀고 RockPaperScissors.exe를 실행해본다.
8) 마무리
이제 간단한 가위바위보 게임이 완성되었습니다! 🎉
Qt Creator를 사용해 EXE 파일로 빌드하고, DLL 파일과 함께 배포할 수 있습니다.
release 폴더 전체를 압축해 친구들과 공유해보세요! 🎮
'임베디드 관련 카테고리 > Qt' 카테고리의 다른 글
Rock Paper Scissors Installer: 버전별 관리와 설치 프로그램 생성 가이드 (Windows CMD) (1) | 2024.10.28 |
---|---|
GitHub Pages와 Qt Installer Framework를 이용해 온라인 설치 프로그램 만들기 (1) | 2024.10.25 |
Qt Creator로 버튼 클릭 예제 만들기 (0) | 2024.10.23 |
Qt Installer Framework를 사용한 설치 프로그램 제작 튜토리얼 (윈도우 10 기반) (0) | 2024.10.23 |
Qt에서 클래스 이름 앞에 'Q'가 붙는 이유와 주요 클래스의 역할 (0) | 2024.10.22 |
댓글