最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)俄羅斯方塊

 更新時(shí)間:2019年11月25日 11:00:27   作者:未北、  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言俄羅斯方塊的具體代碼,供大家參考,具體內(nèi)容如下

本代碼運(yùn)行環(huán)境是Windows下的VS2013
首先創(chuàng)建tetris.cpp
然后依次創(chuàng)建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
 {
 //固定方塊至背景,并且產(chǎn)生新方塊
 CombineBgBrick();
 GetNewBrick();
 //判斷游戲是否結(jié)束,并給出對應(yīng)提示
 }
}

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; //是長條還是方塊,系數(shù)為16
int g_nRotate = 0; //朝向,系數(shù)為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; //不能移動(dòng)
 }
 }
 }
 }
 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; //不能移動(dòng)
 }
 }
 }
 }
 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;
}

更多關(guān)于俄羅斯方塊的文章,請點(diǎn)擊查看專題:《俄羅斯方塊》

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言水仙花數(shù)的實(shí)現(xiàn)

    C語言水仙花數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了C語言水仙花數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C語言示例講解for循環(huán)的用法

    C語言示例講解for循環(huán)的用法

    初學(xué)C語言,常常遇到for循環(huán)中嵌套個(gè)for循環(huán),初學(xué)者對于這種形式總是一知半解,這次我就整理了常見的for循環(huán)嵌套for循環(huán)的題目,我們一起爭取一舉拿下這類題。學(xué)廢他們,以后再見到就不怕啦!每天都要學(xué)一點(diǎn)呀。加油,奮斗的我們
    2022-06-06
  • C語言實(shí)現(xiàn)折半查找法(二分法)

    C語言實(shí)現(xiàn)折半查找法(二分法)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)折半查找法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 深入探究C++中的容器適配器與仿函數(shù)技術(shù)

    深入探究C++中的容器適配器與仿函數(shù)技術(shù)

    C++中的容器適配器和仿函數(shù)是實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)與算法的重要技術(shù),容器適配器可以將一個(gè)容器轉(zhuǎn)換為另一個(gè)形式,仿函數(shù)則可以自定義數(shù)據(jù)類型的比較、排序、計(jì)算等行為,提高程序的靈活性和可重用性
    2023-04-04
  • C++遞歸算法實(shí)例代碼

    C++遞歸算法實(shí)例代碼

    這篇文章主要介紹了C++遞歸算法實(shí)例代碼,還是比較不錯(cuò)的,運(yùn)用了遞歸算法解決相關(guān)問題,這里分享給大家,需要的朋友可以參考下。
    2017-11-11
  • Qt設(shè)置窗體(QWidget)透明度的方法總結(jié)

    Qt設(shè)置窗體(QWidget)透明度的方法總結(jié)

    在Qt開發(fā)中,有的時(shí)候需要為窗體設(shè)置透明度。這篇文章主要為大家介紹幾個(gè)Qt中窗體設(shè)置透明度的方法,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-11-11
  • 探究C++中指針與數(shù)組運(yùn)算符優(yōu)先級

    探究C++中指針與數(shù)組運(yùn)算符優(yōu)先級

    C++中與指針和數(shù)組相關(guān)的運(yùn)算符優(yōu)先級,通過實(shí)際代碼示例解釋了運(yùn)算符的左結(jié)合與右結(jié)合方式,以及如何使用圓括號()來改變默認(rèn)的結(jié)合順序,文章還提供了一個(gè)優(yōu)先級表,列出了運(yùn)算符的優(yōu)先級和結(jié)合性,幫助讀者更好地理解復(fù)雜表達(dá)式中運(yùn)算符的調(diào)用順序
    2024-10-10
  • C語言計(jì)算大數(shù)階乘的方法

    C語言計(jì)算大數(shù)階乘的方法

    這篇文章主要為大家詳細(xì)介紹了C語言計(jì)算大數(shù)階乘的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程

    MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程

    這篇文章主要介紹了MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程,包括內(nèi)存表的創(chuàng)建以及使用限制等等,需要的朋友可以參考下
    2015-12-12
  • C++中std::for_each的使用

    C++中std::for_each的使用

    std::for_each是C++標(biāo)準(zhǔn)庫中的一個(gè)算法,用于遍歷容器并對每個(gè)元素執(zhí)行指定的操作,本文就來介紹一下C++中std::for_each的使用,感興趣的可以了解一下
    2025-04-04

最新評論

广平县| 南郑县| 三都| 荥经县| 丰原市| 东丽区| 巴林右旗| 聊城市| 嘉兴市| 陆河县| 梁河县| 龙山县| 永嘉县| 岳普湖县| 开原市| 临潭县| 枣阳市| 安化县| 蒙山县| 游戏| 苗栗县| 龙川县| 台北市| 千阳县| 梅河口市| 大庆市| 富宁县| 安平县| 蕉岭县| 龙海市| 道孚县| 临海市| 威宁| 五大连池市| 开化县| 贵港市| 皮山县| 乌拉特前旗| 香港 | 信阳市| 即墨市|