欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

本代碼運行環(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; //不能移動
 }
 }
 }
 }
 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;
}

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

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

相關(guān)文章

  • VC6.0如何創(chuàng)建以及調(diào)用動態(tài)鏈接庫實例詳解

    VC6.0如何創(chuàng)建以及調(diào)用動態(tài)鏈接庫實例詳解

    作為客戶與后臺的中介,為了更好的調(diào)節(jié)兩方的關(guān)系,我明智滴選擇了webservice以及動態(tài)鏈接庫。在與客戶c++使動態(tài)鏈接庫方式,而與后臺java,使用webservice來交流溝通
    2013-01-01
  • Qt多線程實現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能

    Qt多線程實現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能

    這篇文章主要為大家詳細(xì)介紹了Qt多線程實現(xiàn)網(wǎng)絡(luò)發(fā)送文件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C++對string進(jìn)行大小寫轉(zhuǎn)換操作方法

    C++對string進(jìn)行大小寫轉(zhuǎn)換操作方法

    這篇文章主要介紹了C++對string進(jìn)行大小寫轉(zhuǎn)換操作方法,本文通過兩種方法結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Linux C/C++實現(xiàn)DNS客戶端請求域名IP的示例代碼

    Linux C/C++實現(xiàn)DNS客戶端請求域名IP的示例代碼

    DNS全稱:Domain Name System,域名解析系統(tǒng),是互聯(lián)網(wǎng)的一項服務(wù),本文主要介紹了C/C++如何實現(xiàn)DNS客戶端請求域名IP,感興趣的可以了解下
    2024-03-03
  • C語言進(jìn)階:指針的進(jìn)階(2)

    C語言進(jìn)階:指針的進(jìn)階(2)

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下
    2021-09-09
  • 深入理解C++的多態(tài)性

    深入理解C++的多態(tài)性

    本篇文章是對C++的多態(tài)性進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言實現(xiàn)雙向鏈表

    C語言實現(xiàn)雙向鏈表

    本文給大家分享的是一段使用C語言實現(xiàn)雙向鏈表的代碼,完全是根據(jù)自己的理解和認(rèn)識來編寫的,希望大家能夠喜歡,文章的最后附上了一個網(wǎng)友寫的對于雙向鏈表刪除節(jié)點、插入節(jié)點、雙向輸出等操作的代碼,也非常不錯,推薦給大家
    2015-03-03
  • C++11中bind綁定器和function函數(shù)對象介紹

    C++11中bind綁定器和function函數(shù)對象介紹

    這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對象介紹,綁定器,函數(shù)對象和lambda表達(dá)式只能使用在一條語句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-07-07
  • 詳解C++異常處理三個重要組成部分

    詳解C++異常處理三個重要組成部分

    這篇文章主要為大家介紹了C++異常處理的三個重要組成部分示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • C++簡單集合類的實現(xiàn)方法

    C++簡單集合類的實現(xiàn)方法

    如何使用C++實現(xiàn)一個簡單的集合類,這篇文章主要介紹了C++簡單集合類的實現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評論