/*RULE
처음에 맵, 플레이어, 수명(3개)이 표시된다
1. 사과가 떨어진다
2. 사과를 방향키의 좌우로 피한다
3. 만약 사과에 맞으면 수명이 1개씩 줄어든다
4. 수명이 아예 없어지면 게임이 끝나고 재시작과 끝내는 방법이 적힌다
5. S를 누르면 다시시작 F를 누르면 끝
6. 중간에 그만하고 싶으면 F를 눌러 끝낸다
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
#include <conio.h> //_getch가 포함되어있는 헤더
#define LEFT 75
#define RIGHT 77
#include <process.h>
#include <Windows.h>
enum
{
BLACK, /* 0 : 검은색 */
DARK_BLUE, /* 1 : 어두운 파랑 */
DARK_GREEN, /* 2 : 어두운 초록 */
DARK_SKY_BLUE, /* 3 : 어두운 하늘 */
DARK_RED, /* 4 : 어두운 빨강 */
DARK_VOILET, /* 5 : 어두운 보라 */
DARK_YELLOW, /* 6 : 어두운 노랑 */
GRAY, /* 7 : 회색 */
DARK_GRAY, /* 8 : 어두운 회색 */
BLUE, /* 9 : 파랑 */
GREEN, /* 10 : 초록 */
SKY_BLUE, /* 11 : 하늘 */
RED, /* 12 : 빨강 */
VIOLET, /* 13 : 보라 */
YELLOW, /* 14 : 노랑 */
WHITE, /* 15 : 하얀색 */
};
int px=7,py=14;
char str[10]={};
int bx,by;
char arr[50][50]={};
void reset(){
for(int i=0;i<=14;i++)
for(int j=0;j<=28;j++)
arr[i][j]=' ';
}
void move(int x, int y)
{
COORD pos= {x*2,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void map(int x, int y)
{
move(x,y);
printf("#");
}
void apple_rand()
{
srand(time(NULL));
bx=rand()%15;
by=rand()%1;
}
void stage()
{
for(int i = 0;i<=14;i++){
move(i,14); printf(" ");
}
move(px,py);
printf("* ");
move(16,0);
printf("%s",str);
move(16,1);
printf("position = %2d",px);
}
unsigned _stdcall Thread1(void* arg)
{
while(1){
apple_rand();
move(bx,by);
printf("@");
for(int i=1;i<=14;i++)
{
move(bx,by);
printf(" ");
by++;
move(bx,by);
printf("@");
Sleep(100);
if(by==14)
{
move(bx,by);
printf(" ");
}
}
Sleep(500);
}
}
int main(void)
{
int a=0;
system("mode con:cols=80 lines=40");
printf("press 's' to start");
char c;
for (;;){
if (_kbhit()){ //키보드 입력 확인 (true / false)
c = _getch();
if( c=='s'){
system("cls");
a=0;
_beginthreadex(NULL, 0, Thread1, 0, 0, NULL);
px=7;
py=14;
strcpy(str,"♥♥♥");
if(px==bx && py==by)
{
a++;
}
if(a==1)
strcpy(str,"♡♥♥");
else if(a==2)
strcpy(str,"♡♡♥");
else if(a==3)
{
strcpy(str,"♡♡♡");
Sleep(100);
system("cls");
printf("You died");
printf("\npress 's' to restart \npress 'f' to finish");
}
stage();
break;
}
}
}
for(int i=0; i<=15; i++){
map(15,i);
map(i,15);
}
move(0,16);
printf("press 'f' to finish");
for (;;)
{
if (_kbhit()) //키보드 입력 확인 (true / false)
{
c = _getch();
if(c=='f'){
system("cls");
return 0;
}
if (c == -32){ //방향키가 입력되면?
c = _getch();
strcpy(str,"♥♥♥");
switch (c){
case LEFT: px--; break;
case RIGHT: px++; break;
}
if(px==15) px--;
else if(px==-1) px++;
stage(c);
}
}
}
}
/*RULE
처음에 맵, 플레이어, 수명(3개)이 표시된다
1. 사과가 떨어진다
2. 사과를 방향키의 좌우로 피한다
3. 만약 사과에 맞으면 수명이 1개씩 줄어든다
4. 수명이 아예 없어지면 게임이 끝나고 재시작과 끝내는 방법이 적힌다
5. S를 누르면 다시시작 F를 누르면 끝
6. 중간에 그만하고 싶으면 F를 눌러 끝낸다
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
#include <conio.h> //_getch가 포함되어있는 헤더
#include <process.h>
#include <Windows.h>
#define LEFT 75
#define RIGHT 77
CRITICAL_SECTION cs;
enum
{
BLACK, /* 0 : 검은색 */
DARK_BLUE, /* 1 : 어두운 파랑 */
DARK_GREEN, /* 2 : 어두운 초록 */
DARK_SKY_BLUE, /* 3 : 어두운 하늘 */
DARK_RED, /* 4 : 어두운 빨강 */
DARK_VOILET, /* 5 : 어두운 보라 */
DARK_YELLOW, /* 6 : 어두운 노랑 */
GRAY, /* 7 : 회색 */
DARK_GRAY, /* 8 : 어두운 회색 */
BLUE, /* 9 : 파랑 */
GREEN, /* 10 : 초록 */
SKY_BLUE, /* 11 : 하늘 */
RED, /* 12 : 빨강 */
VIOLET, /* 13 : 보라 */
YELLOW, /* 14 : 노랑 */
WHITE, /* 15 : 하얀색 */
};
int px=7,py=14;
char str[10]={};
int bx,by;
void reset(){
//게임을 시작, 재시작시 초기 셋팅
px=7; py=14;
}
void move(int x, int y)
{
COORD pos= {x*2,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void map(int x, int y)
{
move(x,y);
printf("#");
}
void apple_rand()
{
srand(time(NULL));
bx=rand()%15;
by=rand()%1;
}
void stage()
{
for(int i = 0;i<=14;i++){
move(i,14); printf(" ");
}
move(px,py);
printf("* ");
move(16,0);
printf("%s",str);
move(16,1);
printf("position = %3d",px);
}
// unsigned _stdcall Thread1(void* arg)
void Thread1()
{
while(1){
apple_rand();
EnterCriticalSection(&cs);
move(bx,by);
printf("@");
LeaveCriticalSection(&cs);
for(int i=1;i<=14;i++)
{
EnterCriticalSection(&cs);
move(bx,by);
printf(" ");
by++;
move(bx,by);
printf("@");
LeaveCriticalSection(&cs);
Sleep(100);
if(by==14)
{
move(bx,by);
printf(" ");
}
}
Sleep(500);
}
}
int main(void)
{
InitializeCriticalSection(&cs);
HANDLE thread1;
int a=0;
system("mode con:cols=80 lines=40");
printf("press 's' to start");
char c;
for (;;){
if (_kbhit()){ //키보드 입력 확인 (true / false)
c = _getch();
if( c=='s'){
system("cls");
px=7;
py=14;
a=0;
strcpy(str," ♥♥♥ ");
thread1 = _beginthreadex(NULL, 0,Thread1, 0, 0, NULL);
break;
}
}
}
for(int i=0; i<=15; i++){
map(15,i);
map(i,15);
}
/////////////////////game start///////////////////////////////////////////
move(0,16);
printf("press 'f' to finish");
stage();
for (;;)
{
if (_kbhit()) //키보드 입력 확인 (true / false)
{
c = _getch();
if(c=='f'){
system("cls");
return 0;
}
if (c == -32){ //방향키가 입력되면?
c = _getch();
switch (c){
case LEFT: px--; break;
case RIGHT: px++; break;
}
if(px==15) px--;
else if(px==-1) px++;
move(17,6); printf(" ");
if(px==bx && py==by) //crash!!
{
a++;
move(17,6); printf("crash!");
}
if(a==1)
{
strcpy(str," ♡♥♥ ");
move(16,0);
printf("%s",str);
}
else if(a==2)
{
strcpy(str," ♡♡♥ ");
move(16,0);
printf("%s",str);
}
else if(a==3)
{
strcpy(str," ♡♡♡ ");
move(16,0);
printf("%s",str);
Sleep(100);
system("cls");
printf("You died");
printf("\npress 's' to restart \npress 'f' to finish\n\n\n\n\n\n\n\n\n");
}
stage();
}
}
}WaitForSingleObject(thread1, INFINITE);
DeleteCriticalSection(&cs);
// game over
}