• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++制作俄罗斯方块

C++制作俄罗斯方块

作者:maxuewei2 字体:[增加 减小] 来源:互联网 时间:2017-05-28

maxuewei2 通过本文主要向大家介绍了c++俄罗斯方块代码,c++俄罗斯方块,c++俄罗斯方块源代码,c++编写俄罗斯方块,c++语言制作时钟等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

缘起:

  在玩Codeblocks自带的俄罗斯方块时觉得不错,然而有时间限制。所以想自己再写一个。

程序效果:

主要内容:

  程序中有一个board数组,其中有要显示的部分,也有不显示的部分,不显示的部分都存储1。

  如下图:

  shape采用4*4数组(shape)保存。如:

    0 0 0 0
    0 1 0 0
    1 1 1 0
    0 0 0 0

  另外用变量row和column保存shape数组左上角在board中的位置。

  每次下落或左右移动,先对row和column做出改变,然后检测当前row和column下,shape是否重合了为1的格子,如果有重合,就说明shape出界了或者到达下落最低点,则要恢复row和column值。另外,如果是下落,还要将shape放在board上,并产生新的shape。

  旋转时,先对shape数组进行旋转操作,然后检测重合,如果有重合,则反向旋转回来。

代码:

#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
/*-----------------宏定义--------------------------------------------------------*/
#define WIDTH 180
#define HEIGHT 400
#define LONG_SLEEP 300
#define BKCOLOR RGB(238,238,238)//背景色
/*-----------------变量----------------------------------------------------------*/
static int shapes[7][4][4];//存储7个形状
static int high_score[4]= {0,0,0,0};//前三个元素存储最高分,最后一个元素存储此次得分
static int **shape;//当前形状
static int **board;
static int M=15;//显示的列数
static int N=30;//显示的行数
static int MM=M+8;//board的列数
static int NN=N+4;//board的行数
static int LEFT=4;//显示的最左一列
static int RIGHT=LEFT+M-1;//显示的最右一列
static int TOP=0;//显示的最上一列
static int BOTTOM=N-1;//显示的最下一列
static int score=0;
static int row=0;//形状所在行
static int column=MM/2;//形状坐在列
static bool is_pause=false;
static HBRUSH grey_brush =CreateSolidBrush (RGB(210,210,210));
static HBRUSH white_brush =CreateSolidBrush (RGB(130,130,130));
static HBRUSH bk_brush =CreateSolidBrush (BKCOLOR);
static HPEN hPen = CreatePen(PS_SOLID,1,RGB(147,155,166));
static int lattices_top=40;//上面留白
static int lattices_left=20;//左侧留白
static int width=WIDTH/M;//每个格子的宽度
static int height=(HEIGHT-lattices_top)/N;//每个格子的高度
/*-----------------函数-----------------------------------------------------------*/
void add_score() ;
bool check_is_lose() ;
void clear_up() ;//消除没有空格子的行
void* down_thread_function(void * args) ;//形状下落进程要执行的函数
void exit_game(HWND hwnd) ;
void give_new_shape() ;//随机生成一个新形状
int handle_key(HWND hwnd,WPARAM wParam) ;
int init_down_thread(HWND hwnd) ;//初始化形状下落进程
int init_game(HWND hwnd) ;//初始化游戏程序
void init_play() ;//初始化游戏数据
bool is_legel() ;//检测形状在当前位置是否合法(即是否重合了非空的格子)
int load_scores(int* a) ;//读取游戏最高分数据
int load_shape() ;//从文件中加载7个形状
void lose_game(HWND hwnd) ;
int move_down(HWND hwnd) ;//形状下落
int move_lr(HWND hwnd,int lr) ;//形状左右移动
void paint_lattice(HDC hdc,int x,int y,int color) ;//显示一个格子
void paint_UI(HDC hdc) ;//画界面
void reset_rc() ;
void rerotate_matrix(int mn) ;//顺时针旋转一个行列数为mn的方阵
void rotate_matrix(int mn) ;//逆时针旋转一个行列数为mn的方阵
int rotate_shape(HWND hwnd) ;//旋转当前形状并更新界面
bool save_score(HWND hwnd) ;//保存最高分数据
void shape_to_ground() ;//当前形状落地之后,更新board
bool sort_scores(int* a) ;//对最高分和此次得分排序,若创造新纪录则返回true
void update_UI(HWND hwnd) ;//更新界面,仅更新Rect区域(形状所在的那几行)内
void update_UI_all(HWND hwnd) ;//更新界面,更新整个界面
int write_scores(int* a) ;//写最高分数据




/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("Tris");

int WINAPI WinMain (HINSTANCE hThisInstance,
          HINSTANCE hPrevInstance,
          LPSTR lpszArgument,
          int nCmdShow) {
  HWND hwnd;        /* This is the handle for our window */
  MSG messages;      /* Here messages to the application are saved */
  WNDCLASSEX wincl;    /* Data structure for the windowclass */

  /* The Window structure */
  wincl.hInstance = hThisInstance;
  wincl.lpszClassName = szClassName;
  wincl.lpfnWndProc = WindowProcedure;   /* This function is called by windows */
  wincl.style = CS_DBLCLKS;         /* Catch double-clicks */
  wincl.cbSize = sizeof (WNDCLASSEX);

  /* Use default icon and mouse-pointer */
  wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  wincl.lpszMenuName = NULL;         /* No menu */
  wincl.cbClsExtra = 0;           /* No extra bytes after the window class */
  wincl.cbWndExtra = 0;           /* structure or the window instance */
  /* Use Windows's default colour as the background of the window */
  wincl.hbrBackground =bk_brush;
  /* Register the window class, and if it fails quit the program */
  if (!RegisterClassEx (&wincl))
    return 0;

  /* The class is registered, let's create the program*/
  hwnd = CreateWindowEx (
        0,          /* Extended possibilites for variation */
        szClassName,     /* Classname */
        _T("Tris"),    /* Title Text */
        WS_OVERLAPPEDWINDOW, /* default window */
        CW_USEDEFAULT,    /* Windows decides the position */
        CW_USEDEFAULT,    /* where the window ends up on the screen */
        WIDTH+200,         /* The programs width */
        HEIGHT+70,         /* and height in pixels */
        HWND_DESKTOP,    /* The window is a child-window to desktop */
        NULL,        /* No menu */
        hThisInstance,    /* Program Instance handler */
        NULL         /* No Window Creation data */
      );

  /* Make the window visible on the screen */
  ShowWindow (hwnd, nCmdShow);

  /* Run the message loop. It will run until GetMessage() returns 0 */
  while (GetMessage (&messages, NULL, 0, 0)) {
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
  }

  /* The program return-value is 0 - The value that PostQuitMessage() gave */
  return messages.wParam;
}
//从文件中加载7个形状
int load_shape() {
  FILE* f=fopen("shapes.txt","rb");
  if(f==NULL) {
    return -1;
  }
  for(int i=0; i<7; i++) {
    for(int j=0; j<4; j++) {
      for(int k=0; k<4; k++) {
        if(fscanf(f,"%d",&shapes[i][j][k])!=1) {
          return -1;
        }
      }
    }
  }
  fclose(f);
  return 0;
}
//随机生成一个新形状
void give_new_shape() {
  int shape_num=rand()%7;
  for(int i=0; i<4; i++) {
    for(int j=0; j<4; j++) {
      shape[i][j]=shapes[shape_num][i][j];
    }
  }
}
void add_score() {
  score+=100;
}
//消除没有空格子的行
void clear_up() {
  for(int i=row; i<=row+3; i++) {
    if(i>BOTTOM)continue;
    bool there_is_blank=false;
    for(int j=LEFT; j<=RIGHT; j++) {
      if(board[i][j]==0) {
        there_is_blank=true;
        break;
      }
    }
    if(!there_is_blank) {
      add_score();
      for(int r=i; r>=1; r--) {
        for(int c=LEFT; c<=RIGHT; c++) {
          board[r][c]=board[r-1][c];
        }
      }
    }
  }
}
//检测形状在当前位置是否合法(即是否重合了非空的格子)
bool is_legel() {
  for(int i=0; i<4; i++) {
    for(int j=0; j<4; j++) {
      if(shape[i][j]==1&&board[row+i][column+j]==1) {
        return false;
      }
    }
  }
  return true;
}




 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++制作俄罗斯方块

相关文章

  • 2017-05-28C++实现Linux下弹出U盘的方法
  • 2017-05-28C++编程中用put输出单个字符和cin输入流的用法
  • 2017-05-28关于STL中list容器的一些总结
  • 2017-05-28C++动态规划之背包问题解决方法
  • 2017-05-28c语言快速排序算法示例代码分享
  • 2017-05-28C语言查找数组里数字重复次数的方法
  • 2017-05-28C语言 以数据块的形式读写文件详解及实现代码
  • 2017-05-28VC实现获取当前正在运行的进程
  • 2017-05-28C语言中实现“17进制”转“10进制”实例代码
  • 2017-05-28从汇编看c++中extern关键字的使用

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 《Objective-C高级编程》干货三部曲(一):引用计数篇
    • C语言fopen函数的用法,C语言打开文件详解
    • C++编程中指针的声明与基本使用讲解
    • C语言基础知识点解析(extern,static,typedef,const)
    • C++实现CreatThread函数主线程与工作线程交互的方法
    • 支持C++,python,java等语言的跨平台的农历库(天文历算法)
    • C语言以数据块的形式读写文件实例代码
    • 使用C语言的fork()函数在Linux中创建进程的实例讲解
    • 详解C++编程中断言static_assert的使用
    • 浅谈stringstream 的.str()正确用法和清空操作

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有