#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
#include <time.h>
#define HEIGHT 10
#define WIDTH 28
#define FRONT 10 //前空
#define UP 1 //上空
#define BIAS FRONT - 'A' + 2
int sums[WIDTH - 2] = { 0 };
void HideCursor(int x); //隐藏光标
void GotoXY(int x, int y); //定位
void SetColor(int color); //设置颜色
void DrawRectangle(int x, int y, int w, int h);
void Init(); //初始化
void Start(); //游戏开始
void End(); //游戏结束
int main()
{
while (1)
{
Init();
Start();
End();
}
return 0;
}
void HideCursor(int x)
{
CONSOLE_CURSOR_INFO cursor_info = { 1,x };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void GotoXY(int x, int y)
{
COORD pos;
pos.X = x - 1;
pos.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void SetColor(int color)
{
HANDLE consolehwnd;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(consolehwnd, &csbiInfo);
color = color + (csbiInfo.wAttributes & 0xf0);
SetConsoleTextAttribute(consolehwnd, color);
}
void DrawRectangle(int x, int y, int w, int h)
{
int i;
for (i = x; i <= x + w; i = i + 2)
{
GotoXY(i, y);
printf("■");
GotoXY(i, y + h);
printf("■");
}
for (i = y; i < y + h; i = i + 1)
{
GotoXY(x, i);
printf("■");
GotoXY(x + w, i);
printf("■");
}
}
void Init()
{
int i = 0;
SetColor(3);
system("clc");
system("mode con cols=80 lines=22");
HideCursor(0);
DrawRectangle(FRONT, UP, WIDTH, HEIGHT);
GotoXY(FRONT + 2, UP + HEIGHT - 1);
for (i = 0; i < WIDTH - 2; i++) printf("%c", 'A' + i % 26);
for (i = 0; i < WIDTH - 2; i++)sums[i] = 0;
}
void Start()
{
int i, cx;
char c, ch = 0;
SetColor(2);
while (1)
{
srand(clock()); //产生随机数种子
cx = rand() % 26 + FRONT + 2;
c = rand() % 26 + 'A';
GotoXY(cx, UP + 1);
printf("%c", c);
while (ch != 's' && ch != 'S')
{
ch = _getch();
switch (ch) {
case 'a':case 'A':GotoXY(cx, UP + 1); printf(" "); cx = cx == BIAS + 'A' ? BIAS + 'Z' : cx - 1; break;
case 'd':case 'D':GotoXY(cx, UP + 1); printf(" "); cx = cx == BIAS + 'Z' ? BIAS + 'A' : cx + 1; break;
default:break;
}
GotoXY(cx, UP + 1);
printf("%c", c);
}
ch = 0;
for (i = UP + 1; i < UP + HEIGHT - sums[cx - FRONT - 2] - 1; i = i + 1)
{
GotoXY(cx, i);
printf("%c", c);
Sleep(100);
GotoXY(cx, i);
printf(" ");
}
if (cx == BIAS + c)
{
GotoXY(cx, UP + HEIGHT - sums[c - 'A']++ - 2);
printf("%c", c);
}
if (sums[c - 'A'] == HEIGHT - 3)
break;
}
}
void End()
{
char ch;
DrawRectangle(FRONT, UP, WIDTH, HEIGHT);
GotoXY(10, 5);
SetColor(4);
printf("\t\t\t\t\t游戏结束");
GotoXY(10, 10);
printf("\t\t\t\t\t按“Esc”键重新开始........");
while (1)
{
if (ch = _getch() == 0x1b)
break;
}
}
|