C语言小游戏-----字母塔

#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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiDelilA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值