#include "pch.h"
#include <iostream>
#include <conio.h>
#define GAMEX 20
#define GAMEY 10
#define PLAYERSTARTX (GAMEX - 2) / 2
#define PLAYERSTARTY (GAMEY / 3) * 2
#define OVERX GAMEX - 2
#define OVERY GAMEY
#define LINEINDEX GAMEX - 2
#define ZEROINDEX GAMEX - 1
#define BASECHAR '*'
#define BULLETMAX 100
class Pos
{
public:
int X;
int Y;
public:
Pos() : X(0), Y(0)
{
}
Pos(int _X, int _Y) : X(_X), Y(_Y)
{
}
};
class ConsoleScreen
{
private:
char ArrPixel[GAMEY][GAMEX];
public:
void PrintScreen()
{
system("cls");
for (int y = 0; y < GAMEY; y++)
{
printf_s(ArrPixel[y]);
}
}
public:
void SetPixel(Pos _Pos, char _Pixel)
{
ArrPixel[_Pos.X][_Pos.Y] = _Pixel;
}
void SetPixel(int _X, int _Y, char _Pixel)
{
ArrPixel[_Y][_X] = _Pixel;
}
public:
void ScreenInit()
{
memset((void*)ArrPixel, BASECHAR, sizeof(char) * GAMEY * GAMEX);
for (int y = 0; y < GAMEY; y++)
{
ArrPixel[y][LINEINDEX] = '\n';
ArrPixel[y][ZEROINDEX] = 0;
}
}
};
class ScreenObj
{
private:
Pos m_ScreenPos;
bool IsOnOff;
ConsoleScreen* m_pScreen;
protected:
char RenderCh;
public:
int GetX() const
{
return m_ScreenPos.X;
}
int GetY() const
{
return m_ScreenPos.Y;
}
void SetRenderCh(char _ch)
{
RenderCh = _ch;
}
public:
void On() {
IsOnOff = true;
}
void Off() {
IsOnOff = false;
}
public:
void PosSet(Pos _Pos)
{
m_ScreenPos = _Pos;
}
void PosSet(int _X, int _Y)
{
m_ScreenPos.X = _X;
m_ScreenPos.Y = _Y;
}
void MoveLeft() { --m_ScreenPos.X; }
void MoveRight() { ++m_ScreenPos.X; }
void MoveUp() { --m_ScreenPos.Y; }
void MoveDown() { ++m_ScreenPos.Y; }
public:
void SetScreen(ConsoleScreen* _pScreen)
{
m_pScreen = _pScreen;
}
public:
void Render()
{
if (false == IsOnOff)
{
return;
}
if (-1 >= m_ScreenPos.X)
{
return;
}
if (-1 >= m_ScreenPos.Y)
{
return;
}
if (OVERX <= m_ScreenPos.X)
{
return;
}
if (OVERY <= m_ScreenPos.Y)
{
return;
}
m_pScreen->SetPixel(m_ScreenPos.X, m_ScreenPos.Y, RenderCh);
}
public:
ScreenObj() : m_ScreenPos(-1, -1), RenderCh(' '), IsOnOff(true)
{}
};
class Bullet : public ScreenObj
{
public:
void Update()
{
MoveUp();
}
public:
Bullet()
{
RenderCh = '^';
}
};
class FlyPlayer : public ScreenObj
{
private:
bool FireCheck;
int BulletCount = 0;
public:
bool IsFire()
{
bool Check = FireCheck;
FireCheck = false;
return Check;
}
int GetBulletCount()
{
return BulletCount;
}
public:
void Control()
{
FireCheck = false;
switch (_getch())
{
case 'a':
case 'A':
MoveLeft();
break;
case 'd':
case 'D':
MoveRight();
break;
case 'w':
case 'W':
MoveUp();
break;
case 's':
case 'S':
MoveDown();
break;
case 'r':
case 'R':
break;
case 'f':
case 'F':
FireCheck = true;
BulletCount += 1;
if (BulletCount == BULLETMAX)
{
BulletCount = 1;
}
break;
}
}
public:
FlyPlayer()
{
RenderCh = '@';
}
};
int main()
{
ConsoleScreen Screen = ConsoleScreen();
Screen.ScreenInit();
FlyPlayer NewPlayer = FlyPlayer();
NewPlayer.SetScreen(&Screen);
NewPlayer.SetRenderCh('@');
NewPlayer.PosSet(PLAYERSTARTX, PLAYERSTARTY);
Bullet OneBullet[BULLETMAX];
for (int i = 0; i < BULLETMAX; i++)
{
OneBullet[i].SetScreen(&Screen);
OneBullet[i].SetRenderCh('^');
OneBullet[i].Off();
}
while (true)
{
size_t Test = 0;
int BulletCount = NewPlayer.GetBulletCount();
for (size_t i = 0; i < 20000000; ++i)
{
Test = i;
}
NewPlayer.Render();
for (int i = 0; i < BULLETMAX; i++)
{
OneBullet[i].Render();
OneBullet[i].Update();
}
Screen.PrintScreen();
Screen.ScreenInit();
if (1 == _kbhit())
{
NewPlayer.Control();
}
if (true == NewPlayer.IsFire())
{
OneBullet[BulletCount].PosSet(NewPlayer.GetX(), NewPlayer.GetY());
OneBullet[BulletCount].On();
}
}
}
간단한 코드니 과제에 사용하시면 아마 F나옵니다 ㅠ
'게임 프로그래밍 > C++ 게임' 카테고리의 다른 글
자체엔진 2D포트폴리오 스타듀밸리 모작 (0) | 2020.03.02 |
---|---|
C++ 콘솔RPG게임[클래스사용, 간단..] (0) | 2019.01.23 |
C++ 콘솔슈팅게임[클래스 미사용, 간단..] (0) | 2019.01.23 |