게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c++갤러그 소스분석중인데..
게시물ID : programmer_17535짧은주소 복사하기
작성자 : 토왕
추천 : 0
조회수 : 3644회
댓글수 : 4개
등록시간 : 2016/06/08 16:45:46
옵션
  • 본인삭제금지
#include <iostream> // 80,23
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <cstdio>
#include "Player.h"
#include "Enemy.h"
#include "Bullet.h"
#include "Game.h"

#define SCREENX 80 // 화면 넓이
#define SCREENY 24 // 화면 높이
#define delay(n) Sleep(n) // n/1000초만큼 시간 지연
#define ENEMYNUM 35 // 적기, 적총알 수
using namespace std;

void main()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); // 핸들값 얻어오기
Game *game;
Player *player;
CBullet *pbullet;
CBullet *pDelBullet;
Enemy *enemy[ENEMYNUM];
CBullet *ebullet[ENEMYNUM];
clock_t startTime, endTime;
int count; // 프레임 카운트
int i;
int selectMenu;
startTime = clock();
// 메뉴 선택 변수
bool cur_key = false; // 총알 발사 체크 변수
bool old_key = false;
bool check = 1; // 플레이어 총알 삭제 체크 변수
CBullet *bullet_head = NULL; // 플레이어 총알 linked list 헤트
CBullet *bullet_tail = NULL; // 플레이어 총알 linked list 테일

for(;;)
{
SetConsoleTextAttribute(hStdOut, FOREGROUND_INTENSITY);
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t//////////////////////////"<<endl;
cout<<"\t\t//      난 이 도        //"<<endl;
cout<<"\t\t//////////////////////////"<<endl;
cout<<"\t\t//                      //"<<endl;
cout<<"\t\t//      1. Easy         //"<<endl;
cout<<"\t\t//                      //"<<endl;
cout<<"\t\t//      2. Nomal        //"<<endl;
cout<<"\t\t//                      //"<<endl;
cout<<"\t\t//      3. Hard         //"<<endl;
cout<<"\t\t//                      //"<<endl;
cout<<"\t\t//      4. Exit         //"<<endl;
cout<<"\t\t//////////////////////////"<<endl;
cout<<"\t\t선택 하세요 : ";
cin>>selectMenu;

//////////////////////////////////////////////////////////////////////////////////
//
// 게임 초기화
game = new Game(selectMenu);
player = new Player(1,"<<A>>", SCREENX/2, SCREENY);
game->SetCursorType(NOCURSOR); // 화면 에서 커서 안보이게함
game->ScreenClear(); // 화면 지우기
count = 0;
for(i =0; i< ENEMYNUM; i++)
{
enemy[i] =NULL; // 적 객체 초기화
ebullet[i] = NULL; // 적 총알 객체 초기화
}

if(selectMenu == 4) // 메뉴중 4번 이 선택되었을시 
exit(0); // 종료


//////////////////////////////////////////////////////////////////////////////////
// 게임루프 시작
for(;;)
{
//////////////////////////////////////////////////////////////////////////////////
// 키 입력에 따른 처리
game->KeyPress(); // 키입력
if(!(player->GetExist())) // 플레이어가 죽지 않았을경우
{
if( (game->GetLeftMove() == 1) && (player->GetX() > 0))
player->MoveXY(-2,0); // 왼쪽 화살표를 눌렀을 경우 왼쪽으로 이동
else if( (game->GetRightMove() == 1) && (player->GetX() < 74))
player->MoveXY(2,0); // 오른쪽 화살표를 눌렀을 경우 오른쪽으로 이동
if( (game->GetUpMove() == 1) && (player->GetY() > 0))
player->MoveXY(0,-1); // 윗쪽 화살표를 눌렀을 경우 위로 이동
else if( (game->GetDownMove() == 1) && (player->GetY() < 23))
player->MoveXY(0,1); // 아랫쪽 화살표를 눌렀을 경우 아래로 이동

if(game->GetFlag() == 0) // esc를 눌렀을경우 게임루프 종료
break;

pbullet = new CBullet();
if(game->GetShotBullet() == 1) // space를 눌렀을경우 총알 발사
{
cur_key = true;
// cur_key가 true이고 old_key가 false일때만 총알 발사
// cur_key는 space가 눌린경우 true를 대입 받는다
// space가 눌리지 않은 경우는 false값을 대입 받는다
// old_key는 매 루프 마다 cur_key의 값을 대입 받는다
// space가 계속 눌리고 있다면 cur_key와 old_key는 모두 true가 되어
// 총알이 발사되지 않는다
// space를 누르고 있지 않다가 누른경우
// cur_key는 true가 되고 old_key는 false가 되어 총알이 발사 된다
if( cur_key && !old_key )
{
pbullet->CreateBullet("A", player->GetX()+2, player->GetY());
// 총알을 생성한다
game->AddNode(&pbullet, &bullet_head, &bullet_tail);
// 생성된 총알을 linked list에 추가한다
}
}
else
cur_key = false;
old_key = cur_key;
}
//////////////////////////////////////////////////////////////////////////////////
// 적기 생성

if(count % 30 == 20) // 적기 모션 0,1
for(i = 0; i<game->GetEnemyNum(); i++)
{
if(enemy[i] == NULL) // 적 배열이 초기화 되어있을 경우 
{
enemy[i] = new Enemy(-(i*10), i *2, 0); 
break; // 0번 모션으로 움직이는 적 생성
}
if(enemy[game->GetEnemyNum() + i] == NULL)
{
enemy[game->GetEnemyNum() + i] = new Enemy(80 + (10 * i), 1 + i * 2, 1);
break; // 1번 모션으로 움직이는 적 생성
}
}
if(count % 30 == 10) //적기 모션 2,3
for(i = 0; i<game->GetEnemyNum(); i++)
{
if(enemy[game->GetEnemyNum() * 2 + i] == NULL)
{
enemy[(game->GetEnemyNum() * 2) + i] = new Enemy(30,0, 2);
break; // 2번 모션으로 움직이는 적 생성
}
if(enemy[game->GetEnemyNum() * 3 + i] == NULL)
{
enemy[(game->GetEnemyNum() * 3) + i] = new Enemy(50, 0, 3);
break; // 3번 모션으로 움직이는 적 생성
}
}
if(count % 30 == 0) //적기 모션 4,5
for(i = 0; i<game->GetEnemyNum(); i++)
{
if(enemy[game->GetEnemyNum() * 4 + i] == NULL)
{
enemy[(game->GetEnemyNum() * 4) + i] = new Enemy(75, (i * 2) + 1, 4);
break; // 4번 모션으로 움직이는 적 생성
}
if(enemy[game->GetEnemyNum() *5 + i] == NULL)
{
enemy[(game->GetEnemyNum() * 5) + i] = new Enemy(0, (i * 2), 5);
break; // 5번 모션으로 움직이는 적 생성
}
}
if(count % 50 == 10) //적기 모션 6
for(i = 0; i<game->GetEnemyNum(); i++)
{
if(enemy[game->GetEnemyNum() *6 + i] == NULL)
{
enemy[(game->GetEnemyNum() * 6) + i] = new Enemy(20 + 20*i, 0, 6);
break; // 6번 모션으로 움직이는 적 생성
}
}
if (count % 50 == 10) //적기 모션 7
for (i = 0; i<game->GetEnemyNum(); i++)
{
if (enemy[game->GetEnemyNum() * 7+ i] == NULL)
{
enemy[(game->GetEnemyNum() * 7) + i] = new Enemy(25+ 25* i, 0, 7);
break; // 7번 모션으로 움직이는 적 생성
}
}
//////////////////////////////////////////////////////////////////////////////////
// 적 총알 생성

int shootCount = 0; // 루프 10번당 발사될 적 총알 수
static int shoot = 0; // 발사될 총알 번호 저장
int rnd; // 렌덤값 저장
switch(selectMenu)
{
case 1 :
shootCount = 1;
break;
case 2 :
shootCount = 3;
break;
case 3 :
shootCount = 5;
break;
}
while( shootCount && (count % 10 == 4))
{
rnd = rand() % ENEMYNUM; // 0보다 크고 적 숫자보다 작은 임의의정수
if(enemy[rnd] != NULL && ebullet[shoot] == NULL)
{ // 임의의 정수의 적이 있고 shoot카운터에 해당하는
// 적 총알이 생성되지 않았을경우
ebullet[shoot] = new CBullet(); // 총알 할당
ebullet[shoot]->CreateBullet("v", enemy[rnd]->GetX()+2, enemy[rnd]->GetY());
//선택된 적의위치에 총알 생성
shoot++;
shootCount--;
}
if(shoot == ENEMYNUM) // 총알 카운터가 적수를 넘어갈때
shoot = 0; // 0 대입 
}
//////////////////////////////////////////////////////////////////////////////////
// 화면 출력
for(i = 0; i < ENEMYNUM; i++)
{
if(enemy[i] != NULL)
{
enemy[i]->Move();
if(enemy[i]->GetY() >= 0 && enemy[i]->GetY()+1 <= SCREENY+1 && enemy[i]->GetX() >= 0 && enemy[i]->GetX()+5 <= SCREENX)
game->ScreenPrint(hStdOut, PINK,enemy[i]->GetX(), enemy[i]->GetY(), enemy[i]->GetType());
else
{
delete enemy[i];
enemy[i] =NULL;
}
}
if(ebullet[i] != NULL)
{
ebullet[i]->MoveBullet(0,1);
if(ebullet[i]->GetY() >= 0 && ebullet[i]->GetY()+1 <= SCREENY+1 && ebullet[i]->GetX() >= 0 && ebullet[i]->GetX()+5 <= SCREENX)
game->ScreenPrint(hStdOut, WHITE,ebullet[i]->GetX(), ebullet[i]->GetY(), ebullet[i]->GetType());
else
{
delete ebullet[i];
ebullet[i] =NULL;
}
}
}
pbullet = bullet_head;
while(pbullet)
{
game->ScreenPrint(hStdOut, WHITE,pbullet->GetX(), pbullet->GetY(), pbullet->GetType());
pbullet->MoveBullet(0, -1);
// 플레이어 총알, 적
for(i = 0; i < ENEMYNUM; i++)
{
if(enemy[i] != NULL && game->Crash(pbullet,enemy[i]))
{
player->AddScore(100);
pDelBullet = pbullet;
check = 0;
delete enemy[i];
enemy[i] = NULL;
break;
}
}
if(pbullet->CheckRange() == 0)
{
pDelBullet = pbullet;
check = 0;
}
pbullet = pbullet->pNext;
if(check == 0)
{
game->DelNode(&pDelBullet, &bullet_head, &bullet_tail);
check = 1;
}
}
if(player->GetExist())
game->ScreenPrint(hStdOut, RED,player->GetX(), player->GetY(), game->Explosion());
else
game->ScreenPrint(hStdOut, SKYBLUE,player->GetX(), player->GetY(), player->GetType());
game->ScreenPrint(hStdOut, GREEN,0,0," 점수 : ");
game->ScreenPrint(hStdOut, GREEN,9,0,player->GetScore());
game->ScreenPrint(hStdOut, GREEN,12,0," Life : ");
game->ScreenPrint(hStdOut, GREEN,21,0,player->GetLife());
game->ScreenPrint(hStdOut, GREEN, 0, 1, " 경과시간 : ");
endTime = clock();
game->ScreenPrint(hStdOut, GREEN, 11, 1, ((double)(endTime - startTime)) / CLOCKS_PER_SEC);
//////////////////////////////////////////////////////////////////////////////////
// 충돌 처리

for(i = 0; i < ENEMYNUM; i++)
{
// 플레이어, 적총알
if( ebullet[i] != NULL && game->Crash(player, ebullet[i]) && player->GetExist() == 0)
{
player->SetExist(1);
player->AddLife(-1);
delete ebullet[i];
ebullet[i] = NULL;
break;
}
// 플레이어, 적 
if(enemy[i] != NULL && game->Crash(player, enemy[i])&& player->GetExist() == 0)
{
player->SetExist(1);
player->AddLife(-1);
delete enemy[i];
enemy[i] = NULL;
break;
}
}

delay(30); //시간 지연
count++;
game->ScreenClear(); // 화면 지우기
if(game->GetExpCount() == 35)
if(player->GetLife() <= 0)
break;
else
{
player->SetExist(0);
player->SetPos(SCREENX/2, SCREENY);
game->setExpCount(0);
}
}
game->ScreenPrint(hStdOut, RED,SCREENX/2-5, SCREENY/2, "Game Over!");
getch();
game->ScreenClear();
game->SetCursorType(NORMALCURSOR);

for(i = 0; i < ENEMYNUM; i++)
{
// 플레이어, 적총알
if( ebullet[i] != NULL)
{
delete ebullet[i];
ebullet[i] = NULL;
}
}
pbullet = bullet_head;
while(pbullet)
{
// 플레이어 총알, 적
pDelBullet = pbullet;
pbullet = pbullet->pNext;
game->DelNode(&pDelBullet, &bullet_head, &bullet_tail);
}
delete game;
delete player;
}


메인 cpp소스인데 count가  프레임 카운트라는데  왜 만든건지 모르겠습니다 특히  적기 를 만들 때 if 조건문에 왜들어가는지 아무리 봐도 이해가 안가네요 ;;  그리고  모션 7은  왜  난이도 1에서는 나오는데 2~3에서는 안나올까요 ? ㅠㅠ
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호