map[x][y]
* 1
A 2
C 3
N 4
*/
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
int px=5, py=5; // player1의 초기위치
int p2x=10,p2y=10; // player2의 초기위치
int player1score=0, player2score=0;
int map[100][100] = {0};
void view_player1(){
gotoxy(px, py);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
printf("*");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
void view_player2(){
gotoxy(p2x, p2y);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
printf("A");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
void info(){
gotoxy(5,115); //23,90
printf("player1 위치 x : %2d y : %2d",px,py);
gotoxy(6 ,115);
printf("player2 위치 x : %2d y : %2d",p2x,p2y);
gotoxy(10,100);
printf("player1 score : %2d", player1score);
gotoxy(12,100);
printf("player2 score : %2d", player2score);
gotoxy(15,100);
printf("게임 설명");
gotoxy(17,100);
printf(" 플레이어1은 방향키로 움직인다(플레이어1은 *)");
gotoxy(19,100);
printf("플레이어2는 asdw로 움직인다(플레이어2는 A)");
gotoxy(21,100);
printf("플레이어1이 A블럭을 먹으면 스코어는 1 증가하고 또한 플레이어1은 N블럭을 먹어도 스코어는 1 증가한다");
gotoxy(23,100);
printf(" 플레이어2가 *을 먹으면 스코어는 1 증가하고 또한 플레이어2는 C블럭을 먹어도 스코어는 1 증가한다.");
}
void view_map(){
int i;
for (i=0; i<=91; i+=2)
{
gotoxy(41,i);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
printf("x");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(0,i);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
printf("x");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
for (i=0; i<=41; i++)
{
gotoxy(i,91);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
printf("x");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(i,0);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
printf("x");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
}
void gotoxy(int x,int y){
COORD pos= {y,x};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int main(){
system("mode con cols=200 lines=50");
char c;
view_map(); info();
while(1){
if(_kbhit()){
c = _getch();
// together use
if((c !='a' && c !='w' && c !='s' && c != 'd')) //플레이어1이 움직일때
{
//gotoxy(px, py); map[px][py] = 0; printf(" ");
if(c==72&&px>1) px--;
else if(c==75&&py>1) py--;
else if(c==77&&py<90) py++;
else if(c==80&&px<40) px++;
view_player1();
//////////////////////////////////
gotoxy(p2x, p2y); map[p2x][p2y] = 0; // printf(" ");
if(map[px][py]==2||map[px][py]==4 ){ //p1이 p2를 먹으면
player1score++;
map[px][py]=0;
gotoxy(10,100); printf("player1 score : %2d", player1score);
if(player1score>=10&& player1score%10==0)//10번 마다 랜덤 위치에 아이템 생성하기
{
int x = rand()%40+1;
int y = rand()%90+1;
map[x][y]=4;
gotoxy(x,y);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
printf("N");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
}
map[px][py]=1; //p1의 위치 체크
}
//player2가 움직일때
else if(c=='w' || c=='W'||c=='d' || c=='D'||c=='a' || c=='A'||c=='s' || c=='S'){
if((c=='w' || c=='W')&& p2x>1) p2x--;
else if((c=='d' || c=='D')&& p2y<90) p2y++;
else if((c=='a' || c=='A')&& p2y>1) p2y--;
else if((c=='s' || c=='S')&&p2x<40) p2x++;
view_player2();
if(map[p2x][p2y]==1||map[p2x][p2y]==3){//p2가 p1을 먹으면
player2score++;
gotoxy(12,100);
printf("player2 score : %2d", player2score);
if(player2score>=10&& player2score%10==0)
{
int pcx = rand()%40+1;
int pcy = rand()%90+1;
map[pcx][pcy]=3;
gotoxy(pcx,pcy);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
printf("c");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
}
map[p2x][p2y] = 2;
}
info();
if(player1score>100 || player2score>100) break;
}
}
//게임 종료
//system("cls");
Sleep(500);
gotoxy(25,25);
if(player1score>player2score)
printf("player1 ");
else
printf("player2 ");
Sleep(500);
printf("is ");
Sleep(500);
printf("winner!!!");
Sleep(500);
gotoxy(100,0);
return 0;
}



