大一实训----C语言编写俄罗斯方块游戏

本文介绍了作者在大一实训期间使用C语言编写俄罗斯方块游戏的过程,包括代码编写、运行效果展示及实训笔记。实训笔记涵盖开发环境安装、程序开发流程、windows.h的使用、需求分析、总体设计、函数和结构体的运用,以及如何在CodeBlocks中播放音乐。游戏具备开始动画、游戏界面、游戏逻辑和结束动画等功能,并讨论了使用PlaySound和mciSendString播放音乐的细节。
摘要由CSDN通过智能技术生成

一、代码

main.c

#include <stdio.h>
#include <stdlib.h>
#include "game.h"

int main()
{
   
   gameInit();
    return 0;
}

mywindows.h

#ifndef MYWINDOWS_H_INCLUDED
#define MYWINDOWS_H_INCLUDED

/*系统调用模块*/
#include <windows.h>
//函数声明
//初始化句柄
void initHandle();

//设置颜色
void setColor(int color);

//设置光标位置
void setPos(int x,int y);

//隐藏光标
void hideCursor();

#endif // MYWINDOWS_H_INCLUDED

mywindows.c

#include "mywindows.h"

HANDLE handle;

void initHandle()
{
   
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    hideCursor();

}

void setColor(int color)
{
   
    SetConsoleTextAttribute(handle,color);
}

void setPos(int x,int y)
{
   
    COORD coord = {
   x*2,y};
    SetConsoleCursorPosition(handle,coord);

}

void hideCursor()
{
   
    CONSOLE_CURSOR_INFO info;
    info.bVisible = FALSE;          //设置光标是否可见
    info.dwSize = 1;                //光标的宽度(1~100)
    SetConsoleCursorInfo(handle,&info);
}

game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

/*游戏逻辑模块*/

#include <stdio.h>
#include <time.h>

typedef struct{
   
    int x;
    int y;
    int shape;
    int status;
    int color;
}BLOCK;

//绘制游戏池边框
void windowPrint(int x, int y);

//游戏初始化
void gameInit();

//打印操作说明
void printInfo();

//打印分数等级
void printGradeLevel(int num);

//游戏计时
void gameTime(clock_t star_time);

//打印方块
void printBlock(int x ,int y ,int shape ,int status ,int color);

//删除方块
void deleteBlock(int x, int y, int shape ,int status);

//拷贝方块
void copyBlock();

//产生游戏第一个方块
void startBlock();

//产生下一个方块
void nextBlock();

//方块下移
//返回值:标志方块是否到游戏池底部
int downBlock();

//方块左移
int leftBlock();

//方块右移
int rightBlock();

//方块变形
void changeBlock();

//碰撞检测
int crash(int x, int y, int shape, int status);

//保存方块
void save();

//刷新游戏池
void undateGame();

//暂停
void pause();

//落地
void bottomBlock();

//消行检测
void lineClear();

//消行下移
void lineDown(int line);

//游戏结束动画
void printOver();

//重新开始提示
void printFinish();

//重新开始游戏
void againGame();

//打印开始图案
void printStart(int x, int y);

//清除开始动画
void deleteStart(int x, int y);

//动画效果->定时(边界控制)
void printAnimation();

//读取最高记录
void readFile();

//保存最高记录
void writeFile();


#endif // GAME_H_INCLUDED

game.c

#include "game.h"
#include "data.h"
#include "mywindows.h"
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

int grade = 0;         //分数
int level = 1;         //等级
int max=0;             //最高记录

BLOCK cur_block;       //当前方块
BLOCK next_block;      //下一个方块

void windowPrint(int x, int y)
{
   
     int i,j;
     for(i = 0; i < 25; i++){
   
        for( j = 0; j < 37; j++){
   
            if(windowShape[i][j] == 1){
   
                setPos(x+j,y+i);//打印位置,坐标位置和数组内容相反
                setColor(0x0f);
                printf("■");
            }else if(windowShape[i][j] == 2){
   
                setPos(x+j,y+i);
                setColor(0x04);
                printf("■");
            }else if(windowShape[i][j] == 3){
   
                setPos(x+j,y+i);
                setColor(0x0e);
                printf("■");
            }else if(windowShape[i][j] == 4){
   
                setPos(x+j,y+i);
                setColor(0x06);
                printf("■");
            }
        }
     }
}

void printInfo()
{
   
    setColor(0x0f);

    setPos(31,8);
    printf("操作规则:");
    setPos(33,9);
    printf("变形:↑");
    setPos(33,11);
    printf("下移:↓");
    setPos(33,13);
    printf("左移:←");
    setPos(33,15);
    printf("右移:→");
    setPos(33,17);
    printf("直接下落:Enter");
    setPos(33,19);
    printf("暂停:space");
    setPos(33,21);
    printf("退出:ESC");
    setPos(33,23);
    printf("音乐:开O/关C");
}

void printGradeLevel(int num)
{
   
    switch(num){
   
    case 0:break;
    case 1:grade += 10;break;
    case 2:grade += 20;break;
    case 3:grade += 30;break;
    case 4:grade += 40;break;
    }

    setColor(0x0f);
    if(grade < 20){
   
        level = 1;
        setPos(10,9);
        printf("速度:0.45s");
    }
    else if(grade <= 40 && grade >= 20){
   
        level = 2;
        setPos(10,9);
        printf("速度:0.30s");
    }
    else if(grade <= 60 && grade > 40){
   
        level = 3;
        setPos(10,9);
        printf("速度:0.20s");
    }
    else if(grade <= 100 && grade > 60){
   
        level = 4;
        setPos(10,9);
        printf("速度:0.15s");
    }

    setColor(0x0f);
    setPos(10,6);
    printf("分数:%d", grade);

    setPos(10,7);
    printf("等级:%d", level);

    setPos(10,8);
    printf("最高分:%d", max);

    setPos(11,16);
    setColor(0x04);
    printf("■");

    setPos(10,10);
    setColor(0x0e);
    printf("■■■");
    setPos(9,11);
    setColor(0x0e);
    printf("■■■■■");
    setPos(9,12);
    setColor(0x0e);
    printf("■■■■■");
    setColor(0x0e);
    setPos(9,13);
    printf("■■■■■");
    setPos(8,14);
    setColor(0x0e);
    printf("■■■■■■■");

}

void gameTime(clock_t star_time)
{
   
    setColor(0x06);
    setPos(42,12);
    printf("本次游戏已运行 %ld s",(clock()-star_time)/CLOCKS_PER_SEC);
}

void printBlock(int x ,int y ,int shape ,int status ,int color)
{
   
    int i,j;
    for( i = 0; i < 4; i++){
   
        for( j = 0; j < 4; j++){
   
            if(block[shape][status][i][j]  == 1){
   
                setColor(color);
                setPos(x+j ,y+i);
                printf("■");
            }
        }
    }
}

void deleteBlock(int x, int y, int shape ,int status)
{
   
    int i,j;
    for(i = 0; i < 4; i++){
   
        for(j = 0; j < 4; j++){
   
            if(block[shape][status][i][j] == 1){
   
                setPos(x+j, y+i);
                printf("  ");
            }
        }
    }
}

void startBlock()
{
   
    //第一个方块:形状/形态/颜色->随机 位置(x,y)固定
    //初始化cur_block
    //设置随机数种子(时间永远不一样)
    srand((unsigned)time(NULL));
    cur_block.x = 22;
    cur_block.y = 1;
    cur_block.shape = rand()%7;
    cur_block.status = rand()%4;
    cur_block.color = rand()%0x10;   //0x00~0x0f
    if(cur_block.color == 0x00){
   
        cur_block.color = 0x01;
    }
    else if(cur_block.color == 0x0f){
   
        cur_block.color = 0x06;
    }
     else if(cur_block.color == 0x07){
   
        cur_block.color = 0x0d;
    }
     else if(cur_block.color == 0x08){
   
        cur_block.color = 0x09;
    }
    printBlock(cur_block.x, cur_block.y, cur_block.shape, cur_block.status, cur_block.color);
}

void nextBlock()
{
   
    //形状、形态、颜色->随机,位置->固定
    //初始化next_block值
    deleteBlock(next_block<
  • 33
    点赞
  • 212
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值