用c语言实现小游戏--俄罗斯方块

基于我们已经实现了相对来说比较完整可以经行娱乐的贪吃蛇游戏,用C语言实现小游戏–贪吃蛇我们接下来,可以再实现一个比贪吃蛇更加复杂的游戏。俄罗斯方块。

这里写图片描述
同样先是完成界面显示的文件,对应的.h 和.c文件

#ifndef __UI_H__
#define __UI_H__

typedef struct pos
{
    int x;
    int y;
}pos;


enum Shapetype
{
    SHAPE1 = 0,
    SHAPE2,
    SHAPE3,
    SHAPE4,
    SHAPE5,
    SHAPE6
};
struct UI {
    // 边缘宽度
    int marginTop;
    int marginLeft;

    // 游戏区域的个数
    int gameWidth;
    int gameHeight;

    // 整个窗口大小宽度
    int windowWidth;
    int windowHeight;

    char *Block;    // 显示块
    int blockWidth;     // 每个块的宽度,注意,上面几个块的宽度要相等,否则就对不齐了
    int *_a;
};
struct Shape
{
    pos* postion;
    enum Shapetype shapetype;
};
// UI 初始化
struct UI * UIInitialize(int width, int height);
void UICleanBlock(const struct UI *pUI, struct Shape*shape);

int InsertScore(struct UI *pUI);
// 显示游戏向导
void UIDisplayWizard(const struct UI *pUI);
// 显示游戏整体,包括墙、右边的信息
void UIDisplayGameWindow(const struct UI *pUI, int score);

void _CoordinatePosAtXY(const struct UI *pUI, int x,int y);

void _ResetCursorPosition(const struct UI *pUI);

void UIDisplayBlock(const struct UI *pUI, struct Shape *shape);
// 清空x,y处的显示块
void UICleanBlockAtY(const struct UI *pUI, int y);
// 显示分数信息
void UIDisplayScore(const struct UI *pUI, int score);
// 销毁 UI 资源
void UIDeinitialize(struct UI *pUI);
int IsGameOver(const struct UI*pUI);
int IsRightHaveShape(const int *a, struct Shape *shape,int width);
int IsLeftHaveShape(const int *a, struct Shape*shape, int width);
#endif
#define _CRT_SECURE_NO_WARNINGS

#include "UI.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

// 移动光标到x,y处,注意,这里是相对整个屏幕的,而不是游戏盘的
static void _SetPos(int x, int y);
// 显示墙
static void _DisplayWall(const struct UI *pUI);
// 显示右侧信息
static void _DisplayDesc(const struct UI *pUI);
// 将游戏盘的x,y坐标转换为相对整个屏幕的x,y
void _CoordinatePosAtXY(const struct UI *pUI, int x, int y);
// 重置光标到屏幕下方,主要是为了显示的美观
void _ResetCursorPosition(const struct UI *pUI);
//数组整体下移
void GetDown(int i, int *a, int width);
//显示,清理图形
static void  _DisplayShape(const struct UI *pUI, struct Shape*shape);
static void  _ClearShape(const struct UI *pUI, struct Shape *shape);

//设置单块颜色
void SetColor(unsigned short ForeColor, unsigned short BackGroundColor)
{
    //forecolor 字体颜色backgroundcolor底色
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon, (ForeColor % 16) | (BackGroundColor % 16 * 16));
}
struct UI * UIInitialize(int width, int height)
{
    const int left = 2;
    const int top = 2;
    const int blockWidth = 2;   // @杨祥钰指出
    const int descWidth = 50;

    struct UI *pUI = (struct UI *)malloc(sizeof(struct UI));
    pUI->gameWidth = width;
    pUI->gameHeight = height;
    pUI->marginLeft = left;
    pUI->marginTop = top;
    pUI->windowWidth = left + (width + 2) * blockWidth + descWidth;
    pUI->windowHeight = top + height + 2 + 3;
    pUI->Block = "█";
    pUI->blockWidth = strlen(pUI->Block);
    pUI->_a = (int *)malloc(sizeof(int)*(pUI->gameHeight)*(pUI->gameWidth));
    memset(pUI->_a, 0, sizeof(int)*(pUI->gameHeight)*(pUI->gameWidth));
    char modeCommand[1024];
    sprintf(modeCommand, "mode con cols=%d lines=%d", pUI->windowWidth, pUI->windowHeight);
    system(modeCommand);

    return pUI;
} 

void UIDisplayWizard(const struct UI *pUI)
{
    int i;
    const char *messages[3] = {
        "欢迎来到俄罗斯方块小游戏",
        "用↑旋转, ←→移动,↓加速。",
        "加速将能得到更高的分数。"
    };

    i = 0;
    _SetPos(pUI->windowWidth / 2 - strlen(messages[i]) / 2, pUI->windowHeight / 2);
    printf("%s\n", messages[i]);
    _SetPos(pUI->windowWidth / 2 - strlen(messages[i]) / 2, pUI->windowHeight / 2 + 2);
    system("pause");
    system("cls");

    i = 1;
    _SetPos(pUI->windowWidth / 2 - strlen(messages[i]) / 2, pUI->windowHeight / 2);
    printf("%s\n", messages[i]);

    i = 2;
    _SetPos(pUI->windowWidth / 2 - strlen(messages[i]) / 2, 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值