C语言实现俄罗斯方块

本文实例为大家分享了C语言俄罗斯方块的具体代码,供大家参考,具体内容如下

本代码运行环境是Windows下的VS2013
首先创建tetris.cpp
然后依次创建view.h以及view.cpp、model.h以及model.cpp。

代码如下:

view.h

#pragma once

#include <stdio.h>
void ShowBackground();
void ShowBrick();
void ShowGame();
void OnLeft();
void OnRight();
void OnUp();
void OnDown();

view.cpp

#include <stdlib.h>
#include "view.h"
#include "model.h"
void OnLeft()
{//如果能够左移,则左移
 if (IsCanMove(g_nRow, g_nCol - 1))
 {
  g_nCol--;
  ShowGame();
 }
}

void OnRight()
{
 if (IsCanMove(g_nRow, g_nCol + 1))
 {
  g_nCol++;
  ShowGame();
 }
}

void OnUp()
{
 if (IsCanRotate())
 {
  Rotate();
  ShowGame();
 }
}

void OnDown()
{
 if (IsCanMove(g_nRow+1, g_nCol))
 {
  g_nRow++;
  ShowGame();
 }
 else
 {
  //固定方块至背景,并且产生新方块
  CombineBgBrick();
  GetNewBrick();
  //判断游戏是否结束,并给出对应提示
 }
}

void ShowGame()
{
 system("cls");
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
}
void ShowBrick()
{
 for (size_t i = 0; i < 4; i++)
 {
  for (size_t j = 0; j < 4; j++)
  {
   if (g_chBrick[i][j] == 1)
   {
    printf("■");
   }
  }
 printf("\r\n");
 }
}

void ShowBackground()
{
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
  for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
  {
   if (g_chBackground[nRow][nCol] == 1)
   {
    printf("■");
   }
   else
   {
    printf("□");
   }
  }
  printf("\r\n");
 }
}

model.cpp

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "model.h"

char g_chBackground[GAME_ROWS][GAME_COLS];
char g_chBrick[4][4];
int g_nShape = 0; //是长条还是方块,系数为16
int g_nRotate = 0; //朝向,系数为4
int g_nRow = 0;
int g_nCol = 0;
char g_chBrickPool[][4] = {
// 长条
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

// T形
1, 1, 1, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,

//L形状
1, 0, 0, 0,
1, 0, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,

1, 1, 1, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 0, 1, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};

int IsCanRotate()
{
 char chNextShape[4][4] = { 0 };
 int nNextRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + nNextRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   chNextShape[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
  }
 }
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   if (chNextShape[nRow][nCol] == 1)
   {
    if (g_chBackground[nRow + g_nRow][nCol + g_nCol] == 1)
    {
     return 0; //不能移动
    }
   }
  }
 }
 return 1;
}

void Rotate()
{
 g_nRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate*4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   g_chBrick[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
  }
 }
}

int IsCanMove(int nToRow, int nToCol)
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   if (g_chBrick[nRow][nCol] == 1)
   {
    if (g_chBackground[nRow + nToRow][nCol + nToCol] == 1)
    {
     return 0; //不能移动
    }
   }
  }
 }
 return 1;
}

void GetNewBrick()
{
 srand((unsigned)time(NULL));
 g_nRow = 0;
 g_nCol = GAME_COLS / 2 - 1;
 int nShapeCount = sizeof(g_chBrickPool) / sizeof(g_chBrickPool[0]) /16;
 g_nShape = rand() % nShapeCount;
 g_nRotate = rand() % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   g_chBrick[nRow][nCol] = g_chBrickPool[nRow+nPoolRows][nCol];
  }
 }
}

void DetachBgBrick()
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   if (g_chBrick[nRow][nCol] == 1)
   {
    g_chBackground[nRow + g_nRow][nCol + g_nCol] = 0;
   }
  }
 }
}

void CombineBgBrick()
{//组合块
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
  for (size_t nCol = 0; nCol < 4; nCol++)
  {
   if (g_chBrick[nRow][nCol] == 1)
   {
    g_chBackground[nRow+g_nRow][nCol+g_nCol] = 1;
   }
  }
 }
}

void InitBackground()
{//初始化背景
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
  for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
  {
   if (nRow == GAME_ROWS - 1
   || nCol == 0
   || nCol == GAME_COLS - 1)
   {
    g_chBackground[nRow][nCol] = 1;
   }
   else
   {
    g_chBackground[nRow][nCol] = 0;
   }
  }
 }
}

model.h

#pragma once

#define GAME_ROWS 20
#define GAME_COLS 12

extern char g_chBackground[GAME_ROWS][GAME_COLS];
extern char g_chBrick[4][4];
extern int g_nRow;
extern int g_nCol;

void InitBackground();
void GetNewBrick();
void CombineBgBrick();
void DetachBgBrick();
int IsCanMove(int nToRow, int nToCol);
void Rotate();
int IsCanRotate();

tetris.cpp

#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "model.h"
#include "view.h"

int main(int argc, char* argv[])
{
 InitBackground();
 GetNewBrick();
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
 char chInput = 0;
 clock_t clkStart = clock();
 clock_t clkEnd = clock();
 while (1)
 {
  clkEnd = clock();
  if (clkEnd - clkStart > 1000)
  {
   clkStart = clkEnd;
   OnDown();
  }
  if (_kbhit() != 0)
  {
   chInput = _getch();
  }
  switch (chInput)
  {
  case 'a':
   OnLeft();
   break;
  case 'w':
   OnUp();
   break;
  case 's':
   OnDown();
   break;
  case 'd':
   OnRight();
   break;
  default:
   break;
  }
  chInput = 0;
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C语言源码实现俄罗斯方块

    介绍 俄罗斯方块(Tetris, 俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名.俄罗斯方块的基本规则是移动.旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分.由于上手简单.老少皆宜,从而家喻户晓,风靡世界. 源码 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #includ

  • C语言实现俄罗斯方块源代码

    本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下 GitHub:[C语言]实现俄罗斯方块源代码 Head.h #ifndef _HEAD_H_ #define _HEAD_H_ #include<graphics.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> #include<string.h> #def

  • VC++ 6.0 C语言实现俄罗斯方块详细教程

    今天把我之前写的大作业分享一下吧,并教你们如何实现,希望你们看了前面的教程也能自己写一个. 1.要先下载一个 graphics.h 的头文件来绘图. 2.初始化窗口:initgraph(x, y);这是先创建一个窗口的函数,以左上角为(0,0),向右为x轴,向下为y轴,其中x表示长x个单位,y表示宽y个单位. 3.关闭图像窗口:closegraph();结束时用来关闭用的. 4.按任意键继续:getch();这个就和getchar();差不多,为了防止以运行完就关了,这样能停顿一下,他的头文件是

  • C语言Turbo C下实现俄罗斯方块

    本文实例为大家分享了C语言俄罗斯方块的具体代码,供大家参考,具体内容如下 #include <stdio.h> #include <dos.h> #include <conio.h> #include <graphics.h> #include <stdlib.h> #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif #define MINBOXSIZE

  • 基于VC 6.0使用C语言实现俄罗斯方块

    本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下 裸写的俄罗斯方块的代码,有意见或者想征用,直接评论留言即可. 效果如下: 代码: /***************************************************************/ /*俄罗斯方块的实现 * 基于VC 6.0 编译链接即可运行 * 已实现的功能: * 1.初步的规划及背景图案的显示 * 2.四种方块实现左右移动.下键加速.上键变形(两种变形)功能 * 3.下落方块碰壁及触

  • C语言实现俄罗斯方块小游戏

    C语言实现俄罗斯方块小游戏的制作代码,具体内容如下 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define TTY_PATH "/dev/tty" #define STTY_ON "stty raw -echo -F" #define STTY_OFF "stty -raw echo -F" int map[21][14]; char

  • C语言实现俄罗斯方块课程设计

    本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下 该课程设计用VC++6.0操作如下: 1.文件->新建->文件->左边选C/C++ Header File->右边文件名命名为"tetris.h"->路径假定为桌面文件夹:tetris->确定.然后将下面红色字体标记的"头文件"代码粘贴至其中,保存并退出(或者关闭工作空间). 2.文件->新建->文件->左边选C/C++ Header

  • C语言实现俄罗斯方块的六种模式详程建议收藏

    --------写在前面-------- 第一次做标题党,大家轻喷哈.这个游戏是博主在大一c语言实训时独立完成的,所有内容均为原创.小游戏耗时5天完成,除了常见的单人模式外,增加了作弊模式,双人模式,计时赛等玩法,真滴很好玩哦.虽然现在看起来很简陋,但对于当时的我来说实属不易,从页面设计到游戏背景音乐的选取再到关键算法的编写,每一步都凝汇了自己的努力,通宵鏖战的画面依然历历在目.现在分享出来,一方面是希望可以帮助到大家,另一方面也想纪念美好的大一时光.源码地址放在文末了,大家自取. ------

  • C语言实现俄罗斯方块

    本文实例为大家分享了C语言俄罗斯方块的具体代码,供大家参考,具体内容如下 本代码运行环境是Windows下的VS2013 首先创建tetris.cpp 然后依次创建view.h以及view.cpp.model.h以及model.cpp. 代码如下: view.h #pragma once #include <stdio.h> void ShowBackground(); void ShowBrick(); void ShowGame(); void OnLeft(); void OnRight

  • C语言代码实现俄罗斯方块

    这里为大家敲写一段怎样用C语言实现俄罗斯方块: 首先推荐大家使用CodeBlocks这个软件,方便添加不同的工程. 代码中有很多注释便于理解! 下面是效果图和全部的代码以及注释,大家可以观看并自己新增内容! 1.首先是main.c文件: #include <stdio.h> #include <stdlib.h> #include "game.h" int main() { gameInit(); return 0; } 2.然后是mywindows.h文件:

  • C语言俄罗斯方块游戏课程设计

    本文实例为大家分享了C语言实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 1.设计流程 2.相关程序 #include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<time.h> #include<dos.h> #include<bios.h> #define LEFT 0x4b00 /*键盘码*/ #define RIGHT 0x4d00 #define

随机推荐