135-1821-9792

如何用C语言实现俄罗斯方块

这篇文章主要介绍“如何用C语言实现俄罗斯方块”,在日常操作中,相信很多人在如何用C语言实现俄罗斯方块问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用C语言实现俄罗斯方块”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

专注于为中小企业提供成都网站设计、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业林甸免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

C语言实现俄罗斯方块的具体代码,用VC++6.0操作如下:

1、文件->新建->文件->左边选C/C++ Header File->右边文件名命名为“tetris.h”->路径假定为桌面文件夹:tetris->确定。然后将下面红色字体标记的“头文件”代码粘贴至其中,保存并退出(或者关闭工作空间)。

2、文件->新建->文件->左边选C/C++ Header File->右边文件名命名为“tetris.h”->路径假定为桌面文件夹:tetris->确定。新建“tetris.c”源文件,将下面“源代码”代码粘贴到其中,保存并退出(或者关闭工作空间)。

3、文件->新建->工程->左边选Win32 Application ->右边工程名称命名为:tetris->路径假定为桌面文件夹:tetris->确定->一个空工程->完成。接下来:工程->增加到工程->文件。这时候,将头文件和源代码添加进去,调试使用。

1.头文件

//1.自定义枚举类型,定义7种形态的游戏方块
typedef enum tetris_shape
{
 ZShape=0,
  SShape,
  LineShape,
  TShape,
  SquareShape,
  LShape,
  MirroredLShape
}shape;


//2.函数声明

//(1)操作方块函数
int maxX();//取得当前方块的最大x坐标
int minX();//取得当前方块的最小x坐标
void turn_left();//当前方块逆时针旋转90度
void turn_right();
int out_of_table();
void transform();
int leftable();
int rightable();
int downable();
void move_left();
void move_right();

//(2)操作游戏桌面的函数
int add_to_table();
void remove_full();

//(3)控制游戏函数
void new_game();
void run_game();
void next_shape();
int random(int seed);

//(4)绘图函数
void paint();
void draw_table();

//(5)其他功能函数
void key_down(WPARAM wParam);
void resize();
void initialize();
void finalize();

//(6)回调函数,用来处理Windows消息
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);

2.源代码

//1.文件包含
#include
#include
#include
#include"tetris.h"

//2.常量定义
#define APP_NAME "TETRIS"
#define APP_TITLE "Tetris Game"
#define GAMEOVER "GAME OVER"

#define SHAPE_COUNT 7
#define BLOCK_COUNT 4
#define MAX_SPEED 5

#define COLUMS 10
#define ROWS 20

#define RED RGB(255,0,0)
#define YELLOW RGB(255,255,0)
#define GRAY RGB(128,128,128)
#define BLACK RGB(0,0,0)
#define WHITE RGB(255,255,255)
#define STONE RGB(192,192,192)

#define CHARS_IN_LINE 14
#define SCORE "SCORE %4d"

3.全局变量定义

//(1)
char score_char[CHARS_IN_LINE]={0};

//(2)
char* press_enter="Press Enter key...";

//(3)帮助提示信息
char *help[]=
{
 "press space or up key to transform shape.",
  "Press left or right key to mover shape.",
  "Press down key to speed up.",
  "Press enter key to pause game.",
  "Enjoy it.:-)",
  0
};

//(4)枚举游戏的状态
enum game_state
{
 game_start,
  game_run,
  game_pause,
  game_over,
}state=game_start;

//(5)定义方块的颜色
COLORREF shape_color[]=
{
 RGB(255,0,0),
  RGB(0,255,0),
  RGB(0,0,255),
  RGB(255,255,0),
  RGB(0,255,255),
  RGB(255,0,255),
  RGB(255,255,255)
};

//(6)方块的7中类型
int shape_coordinate[SHAPE_COUNT][BLOCK_COUNT][2]=
{
 {{0,1},{0,0},{-1,0},{-1,1}},
 {{0,-1},{0,0},{1,0},{1,1}},
 {{0,-1},{0,0},{0,1},{0,2}},
 {{-1,0},{0,0},{1,0},{0,1}},
 {{0,0},{1,0},{0,1},{1,1}},
 {{-1,-1},{0,-1},{0,0},{0,1}},
 {{1,-1},{0,-1},{0,0},{0,1}}
};

//(7)得分
int score=0;

//(8)下一个方块
shape next=0;

//(9)当前方块
shape current=0;

//(10)当前方块的每一部分坐标
int current_coordinate[4][2]={0};

//(11)游戏桌面
int table[ROWS][COLUMS]={0};

//(12)当前方块的x坐标
int shapex=0;

//(13)当前方块的\y坐标
int shapey=0;

//(14)方块下移速度
int speed=0;

//(15)每一帧开始时间
clock_t start=0;

//(16)每一帧结束时间
clock_t finish=0;

//(17)windows绘图用变量
HWND gameWND;
HBITMAP memBM;
HBITMAP memBMOld;
HDC memDC;
RECT clientRC;
HBRUSH blackBrush;
HBRUSH stoneBrush;
HBRUSH shapeBrush[SHAPE_COUNT];
HPEN grayPen;
HFONT bigFont;
HFONT smallFont;

//4.主要处理函数
//(1)取最大坐标
int maxX()
{
 int i=0;
 int x=current_coordinate[i][0];
 int m=x;
 for(i=1;ix)
   {
    m=x;
   }
 }
 return m;
}

//(3)逆时针转动方块
void turn_left()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=current_coordinate[i][0];
  y=current_coordinate[i][1];
  current_coordinate[i][0]=y;
  current_coordinate[i][1]=-x;
 }
}

//(4)顺时针旋转方块
void turn_right()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=current_coordinate[i][0];
  y=current_coordinate[i][1];
  current_coordinate[i][0]=-y;
  current_coordinate[i][1]=x;
 }
}

//(5)检查方块是否越界
int out_of_table()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=shapex+current_coordinate[i][0];
  y=shapey+current_coordinate[i][1];
  if(x<0||x>(COLUMS-1)||y>(ROWS-1))
  {
   return 1;
  }
  if(table[y][x])
  {
   return 1;
  }
 }
 return 0;
}

//(6)旋转方块
void transform()
{
 if(current==SquareShape)
 {
  return ;
 }
 turn_right();
 if(out_of_table())
 {
  turn_left();
 }
}

//(7)判断方块是否向左移动
int leftable()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=shapex+current_coordinate[i][0];
  y=shapey+current_coordinate[i][1];
  if(x<=0||table[y][x-1]==1)
  {
   return 0;
  }
 }
 return 1;
}

//(8)判断方块是否向右移动
int rightable()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=shapex+current_coordinate[i][0];
  y=shapey+current_coordinate[i][1];
  if(x>=(COLUMS-1)||table[y][x+1]==1)
  {
   return 0;
  }
 }
 return 1;
}

//(9)判断方块是否向下移动
int downable()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=shapex+current_coordinate[i][0];
  y=shapey+current_coordinate[i][1];
  if(y>=(ROWS-1)||table[y+1][x]==1)
  {
   return 0;
  }
 }
 return 1;
}

//(10)向左移动当前方块
void move_left()
{
 if(leftable())
 {
  shapex--;
 }
}

//(11)向右移动当前方块
void move_right()
{
 if(rightable())
 {
  shapex++;
 }
}

//(12)向下移动当前方块
void move_down()
{
 if(downable())
 {
  shapey++;
 }
 else
 {
  if(add_to_table())
  {
   remove_full();
   next_shape();
  }
  else
  {
   state=game_over;
  }
 }
}

//(13)将当前方块固定到桌面上
int add_to_table()
{
 int i=0;
 int x,y;
 for(i=0;i<4;i++)
 {
  x=shapex+current_coordinate[i][0];
  y=shapey+current_coordinate[i][1];
  if(y<0||table[y][x]==1)
  {
   return 0;
  }
  table[y][x]=1;
 }
 return 1;
}

//(14)删除填满的行
void remove_full()
{
 int c=0;
 int i,j;
 for(i=ROWS-1;i>0;i--)
 {
  c=0;
  for(j=0;j(MAX_SPEED-speed)*100)
 {
  move_down();
  start=clock();
  InvalidateRect(gameWND,NULL,TRUE);
 }
}

//(17)操作当前方块
void next_shape()
{
 current=next;
 memcpy(current_coordinate,shape_coordinate[next],sizeof(int)*BLOCK_COUNT*2);
 shapex=(COLUMS-((maxX(current)-minX(current))))/2;
 shapey=0;
 next=random(SHAPE_COUNT);
}

//(18)取随机数
 int random(int seed)
 {
  if(seed==0)
  {
   return 0;
  }
  srand((unsigned)time(NULL));
  return (rand()%seed);
 }

 //(19)绘图
 void paint()
 {
  PAINTSTRUCT ps;
  HDC hdc;
  draw_table();
  hdc=BeginPaint(gameWND,&ps);
  BitBlt(hdc,clientRC.left,clientRC.top,clientRC.right,clientRC.bottom,memDC,0,0,SRCCOPY);
  EndPaint(gameWND,&ps);
 }

 //(20)绘制游戏桌面
 void draw_table()
 {
  HBRUSH hBrushOld;
  HPEN hPenOld;
  HFONT hFontOld;
  RECT rc;
  int x0,y0,w;
  int x,y,i,j;
  char* str;

  w=clientRC.bottom/(ROWS+2);
  x0=y0=w;

  FillRect(memDC,&clientRC,blackBrush);

  // 如果游戏是开始或结束状态
  if(state==game_start||state==game_over)
  {
   memcpy(&rc,&clientRC,sizeof(RECT));
   rc.bottom=rc.bottom/2;
   hFontOld=SelectObject(memDC,bigFont);
   SetBkColor(memDC,BLACK);

   //如果游戏是开始状态,用黄色字显示游戏开始画面
   if(state==game_start)
   {
    str=APP_TITLE;
    SetTextColor(memDC,YELLOW); 
   }
  //如果游戏是结束状态,用红色字显示GAME OVER  
   else
    {
     str=GAMEOVER;
     SetTextColor(memDC,RED);
    }

    DrawText(memDC,str,strlen(str),&rc,DT_SINGLELINE|DT_CENTER|DT_BOTTOM);
    SelectObject(memDC,hFontOld);
    hFontOld=SelectObject(memDC,smallFont);
    rc.top=rc.bottom;
    rc.bottom=rc.bottom*2;
    if(state==game_over)
    {
     SetTextColor(memDC,YELLOW);
     sprintf(score_char,SCORE,score);
     DrawText(memDC,score_char,strlen(score_char),&rc,DT_SINGLELINE|DT_CENTER|DT_TOP);
    }
    SetTextColor(memDC,STONE);
    DrawText(memDC,press_enter,strlen(press_enter),&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
    SelectObject(memDC,hFontOld);
    return;
   }

  //桌面上残留的方块
   hBrushOld=SelectObject(memDC,stoneBrush);
   for(i=0;i

到此,关于“如何用C语言实现俄罗斯方块”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站标题:如何用C语言实现俄罗斯方块
分享路径:http://kswsj.com/article/jciogc.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号