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
    评论
以下是一个简单的基于控制台的五子棋C语言小游戏示例: ```c #include <stdio.h> #include <stdlib.h> #define ROWS 15 #define COLS 15 // 棋盘 char board[ROWS][COLS]; // 初始化棋盘 void init_board() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = '+'; } } } // 显示棋盘 void print_board() { printf(" "); for (int i = 0; i < COLS; i++) { printf("%c ", 'A' + i); } printf("\n"); for (int i = 0; i < ROWS; i++) { printf("%2d", i + 1); for (int j = 0; j < COLS; j++) { printf("%c ", board[i][j]); } printf("\n"); } } // 判断是否胜利 int is_win(char c) { // 判断行 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS - 4; j++) { if (board[i][j] == c && board[i][j+1] == c && board[i][j+2] == c && board[i][j+3] == c && board[i][j+4] == c) { return 1; } } } // 判断列 for (int i = 0; i < ROWS - 4; i++) { for (int j = 0; j < COLS; j++) { if (board[i][j] == c && board[i+1][j] == c && board[i+2][j] == c && board[i+3][j] == c && board[i+4][j] == c) { return 1; } } } // 判断主对角线 for (int i = 0; i < ROWS - 4; i++) { for (int j = 0; j < COLS - 4; j++) { if (board[i][j] == c && board[i+1][j+1] == c && board[i+2][j+2] == c && board[i+3][j+3] == c && board[i+4][j+4] == c) { return 1; } } } // 判断副对角线 for (int i = 0; i < ROWS - 4; i++) { for (int j = 4; j < COLS; j++) { if (board[i][j] == c && board[i+1][j-1] == c && board[i+2][j-2] == c && board[i+3][j-3] == c && board[i+4][j-4] == c) { return 1; } } } return 0; } int main() { int x, y; char player = 'O'; init_board(); print_board(); while (1) { printf("Player %c's turn.\n", player); printf("Enter the row and column (e.g., 1A): "); scanf("%d%c", &x, &y); x--; // 行号从0开始 y -= 'A'; // 列号从0开始 if (x < 0 || x >= ROWS || y < 0 || y >= COLS || board[x][y] != '+') { printf("Invalid input. Please try again.\n"); continue; } board[x][y] = player; print_board(); if (is_win(player)) { printf("Player %c wins!\n", player); break; } if (player == 'O') { player = 'X'; } else { player = 'O'; } } return 0; } ``` 在上面的代码中,我们首先定义了棋盘的大小(15x15),并使用`init_board`函数初始化棋盘为初始状态(所有位置都是'+')。然后,我们使用`print_board`函数打印当前的棋盘状态。 在每个玩家的回合中,我们使用`scanf`函数输入玩家的行和列。需要注意的是,行号从1开始,但是我们的数组是从0开始编号的,因此需要将输入的行号减去1。列号是一个字母,但是我们的数组是用数字表示的,因此需要将输入的列号减去'A'。如果输入的行或列不在合法的范围内,或已经有棋子了,我们会提示玩家重新输入。 在每个玩家下完棋子后,我们使用`is_win`函数检查是否有五个棋子连成一线。如果是,我们输出胜利者并退出游戏。如果没有胜者,则交换玩家,并继续下一回合。 需要注意的是,此代码示例是基于控制台的,因此无法实现图形化的界面。同时,由于没有实现人机对战和网络对战等功能,只能用于双人对战。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XiDelilA

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

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

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

打赏作者

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

抵扣说明:

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

余额充值