当前位置: 首页 > news >正文

广州最新封闭封控区域/网站产品怎么优化

广州最新封闭封控区域,网站产品怎么优化,手机免费制作ppt的软件下载,宁波外贸公司电话名单本文实例为大家分享了C语言俄罗斯方块的具体代码,供大家参考,具体内容如下本代码运行环境是Windows下的VS2013首先创建tetris.cpp然后依次创建view.h以及view.cpp、model.h以及model.cpp。代码如下:view.h#pragma once#include void ShowBack…

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

本代码运行环境是Windows下的VS2013

首先创建tetris.cpp

然后依次创建view.h以及view.cpp、model.h以及model.cpp。

代码如下:

view.h

#pragma once

#include

void ShowBackground();

void ShowBrick();

void ShowGame();

void OnLeft();

void OnRight();

void OnUp();

void OnDown();

view.cpp

#include

#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

#include

#include

#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

#include

#include

#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;

}

更多关于俄罗斯方块的文章,请点击查看专题:《俄罗斯方块》

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

http://www.jmfq.cn/news/4798207.html

相关文章:

  • 贵州网站制作品牌公司/全国各城市感染高峰进度查询
  • 小程序就是做网站/百度指数1000搜索量有多少
  • 可以做热图的工具网站/seo平台代理
  • 智能网站优化 cms 加盟/全球搜是什么公司
  • 网站策划 ppt/深圳市企业网站seo营销工具
  • 烟台做网站哪家好/小红书软文案例
  • 哪些网站可以做图片链接/文章推广平台
  • 中国中国建设银行网站首页/企业培训课程名称大全
  • 沈阳网站建设公司/企业网站建设案例
  • 呼伦贝尔网站建设平台/技能培训班
  • 上海网站建设收费/设计本网站
  • wordpress采集工具/seo页面内容优化
  • 如何k掉别人的网站/网站建设开发价格
  • 网站建设重要新/哪些网站可以免费发广告
  • 西部数码网站管理助手错误/著名的网络营销案例
  • 企业网站建设的思路/今日国际新闻头条15条简短
  • 亚洲7号卫星电视/上海seo网站策划
  • 山西推广型网站制作/天津做网站的公司
  • 团购手机网站怎么做/做谷歌推广比较好的公司
  • 南昌网站制作/品牌广告视频
  • 网站转化怎么做/seo优化关键词排名优化
  • canvas案例网站/响应式模版移动优化
  • 做网站之前的工作/营销百度app下载手机版
  • 做网站背景图片要多大/竞价托管运营哪家好
  • 做网站不需要编程的软件/提升seo排名
  • w7系统那个网站做的好/搜索引擎排名2022
  • 合肥做网站哪家公司好/百度爱企查电话人工服务总部
  • 90设计网是干嘛的/长沙seo招聘
  • 如何在阿里巴巴上做网站/html网页制作步骤
  • 深圳网站建设开发/怎么做一个网站页面