...
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#include "gotoxy.h"
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define MOB 3
#define COL GetStdHandle(STD_OUTPUT_HANDLE)
#define BLACK SetConsoleTextAttribute(COL, 0x0000);
#define DARK_BLUE SetConsoleTextAttribute(COL, 0x0001);
#define GREEN SetConsoleTextAttribute(COL, 0x0002);
#define BLUE_GREEN SetConsoleTextAttribute(COL, 0x0003);
#define BLOOD SetConsoleTextAttribute(COL, 0x0004);
#define PURPLE SetConsoleTextAttribute(COL, 0x0005);
#define DARK_YELLOW SetConsoleTextAttribute(COL, 0x0006);
#define WHITE SetConsoleTextAttribute(COL, 0x0007);
#define GRAY SetConsoleTextAttribute(COL, 0x0008);
#define BLUE SetConsoleTextAttribute(COL, 0x0009);
#define LIGHT_GREEN SetConsoleTextAttribute(COL, 0x000a);
#define LIGHT_BLUE SetConsoleTextAttribute(COL, 0x000b);
#define RED SetConsoleTextAttribute(COL, 0x000c);
#define PLUM SetConsoleTextAttribute(COL, 0x000d);
#define YELLOW SetConsoleTextAttribute(COL, 0x000e);
#define LIGHT_WHITE SetConsoleTextAttribute(COL, 0x000f);
struct playerxy
{
int x,y;
};
struct mobxy
{
int x[MOB],y[MOB];
int trackx[MOB],tracky[MOB];
};
struct playerxy playerxy1 = {1*2,1};
struct mobxy mobxy1 = {{1*2,18*2,18*2},{18,18,1},{0},{0}};
int map[20][20]=
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
char player[3] = "웃", mob[MOB][3] = {"웃","웃","웃"};
void mob_moving(int a, int move);
void moving();
void maping();
void mob_tracking(int a);
int main()
{
int i;
float degree_in_fraction[MOB],degree[MOB];
srand(time(NULL));
maping();
gotoxy(playerxy1.x,playerxy1.y);
WHITE printf("%s", player);
for(i=0;i<4;i++)
{
gotoxy(mobxy1.x[i],mobxy1.y[i]);
RED printf("%s", mob[i]);
}
while(1)
{
if(kbhit())
moving();
for(i=0;i<MOB;i++)
{
mobxy1.trackx[i] = playerxy1.x - mobxy1.x[i];
mobxy1.tracky[i] = playerxy1.y - mobxy1.y[i];
mobxy1.tracky[i] = -mobxy1.tracky[i];
//degree_in_fraction[i] = (float)mobxy1.tracky[i] / mobxy1.trackx[i];
//degree[i] = atan(degree_in_fraction[i]);
/*gotoxy(45,5+i);
if(i==0)
GREEN
else if(i==1)
BLUE*/
//printf("%5.1f", degree[i] * (180.0/3.141592));
mob_tracking(i);
}
//mob_tracking(degree[i] * (180.0/3.141592))
if(kbhit())
moving();
}
}
void moving()
{
int hit = getch();
switch(hit)
{
case LEFT :
if(map[playerxy1.y][playerxy1.x/2-1] != 1)
{
gotoxy(playerxy1.x,playerxy1.y);
printf(" ");
playerxy1.x-=2;
gotoxy(playerxy1.x,playerxy1.y);
LIGHT_WHITE printf("%s", player);
}
break;
case RIGHT :
if(map[playerxy1.y][playerxy1.x/2+1] != 1)
{
gotoxy(playerxy1.x,playerxy1.y);
printf(" ");
playerxy1.x+=2;
gotoxy(playerxy1.x,playerxy1.y);
LIGHT_WHITE printf("%s", player);
}
break;
case UP :
if(map[playerxy1.y-1][playerxy1.x/2] != 1)
{
gotoxy(playerxy1.x,playerxy1.y);
printf(" ");
playerxy1.y--;
gotoxy(playerxy1.x,playerxy1.y);
LIGHT_WHITE printf("%s", player);
}
break;
case DOWN :
if(map[playerxy1.y+1][playerxy1.x/2] != 1)
{
gotoxy(playerxy1.x,playerxy1.y);
printf(" ");
playerxy1.y++;
gotoxy(playerxy1.x,playerxy1.y);
LIGHT_WHITE printf("%s", player);
}
}
}
void mob_moving(int a, int move)
{
//int hit;
Sleep(100);
if(kbhit())
moving();
Sleep(100);
if(kbhit())
moving();
Sleep(100);
if(kbhit())
moving();
switch(move)
{
case LEFT :
if(map[mobxy1.y[a]][mobxy1.x[a]/2-1] != 1)
{
gotoxy(mobxy1.x[a],mobxy1.y[a]);
printf(" ");
mobxy1.x[a]-=2;
gotoxy(mobxy1.x[a],mobxy1.y[a]);
RED printf("%s", mob[a]);
}
break;
case RIGHT :
if(map[mobxy1.y[a]][mobxy1.x[a]/2+1] != 1)
{
gotoxy(mobxy1.x[a],mobxy1.y[a]);
printf(" ");
mobxy1.x[a]+=2;
gotoxy(mobxy1.x[a],mobxy1.y[a]);
RED printf("%s", mob[a]);
}
break;
case UP :
if(map[mobxy1.y[a]-1][mobxy1.x[a]/2] != 1)
{
gotoxy(mobxy1.x[a],mobxy1.y[a]);
printf(" ");
mobxy1.y[a]--;
gotoxy(mobxy1.x[a],mobxy1.y[a]);
RED printf("%s", mob[a]);
}
break;
case DOWN :
if(map[mobxy1.y[a]+1][mobxy1.x[a]/2] != 1)
{
gotoxy(mobxy1.x[a],mobxy1.y[a]);
printf(" ");
mobxy1.y[a]++;
gotoxy(mobxy1.x[a],mobxy1.y[a]);
RED printf("%s", mob[a]);
}
break;
}
}
void mob_tracking(int a)
{
int mv;
if(kbhit())
moving();
if(mobxy1.trackx[a] < 0 && mobxy1.tracky[a] < 0)
{
if(map[mobxy1.y[a]][mobxy1.x[a]/2] )
mv = rand()%2;
mv == 0 ? mv=LEFT : mv=DOWN;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] > 0 && mobxy1.tracky[a] > 0)
{
mv = rand()%2;
mv == 0 ? mv=UP : mv=RIGHT;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] < 0 && mobxy1.tracky[a] > 0)
{
mv = rand()%2;
mv == 0 ? mv=LEFT : mv=UP;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] > 0 && mobxy1.tracky[a] < 0)
{
mv = rand()%2;
mv == 0 ? mv=DOWN : mv=RIGHT;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] == 0 && mobxy1.tracky[a] < 0)
{
mv = DOWN;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] == 0 && mobxy1.tracky[a] > 0)
{
mv = UP;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] > 0 && mobxy1.tracky[a] == 0)
{
mv = RIGHT;
mob_moving(a,mv);
}
if(mobxy1.trackx[a] < 0 && mobxy1.tracky[a] == 0)
{
mv = LEFT;
mob_moving(a,mv);
}
}
void maping()
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
switch(map[i][j])
{
case 0 :
printf(" "); break;
case 1 :
printf("■"); break;
}
}
putchar('\n');
}
}
인용한 부분에 있어 만일 누락된 출처가 있다면 반드시 알려주시면 감사하겠습니다
이 글이 좋으셨다면 구독 & 좋아요
여러분의 구독과 좋아요는
저자에게 큰 힘이 됩니다.