中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言實現俄羅斯方塊

發布時間:2020-10-23 19:07:54 來源:腳本之家 閱讀:166 作者:未北、 欄目:編程語言

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

本代碼運行環境是Windows下的VS2013
首先創建tetris.cpp
然后依次創建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
 {
 //固定方塊至背景,并且產生新方塊
 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 <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; //是長條還是方塊,系數為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 <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;
}

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

分宜县| 辉县市| 灵璧县| 莲花县| 新郑市| 博客| 房山区| 广元市| 海阳市| 嘉义县| 濮阳市| 共和县| 海兴县| 临武县| 蓬安县| 吴忠市| 屯留县| 罗山县| 灌阳县| 玉林市| 寿光市| 探索| 循化| 昌图县| 厦门市| 阜宁县| 闽侯县| 二连浩特市| 河津市| 凭祥市| 徐州市| 民勤县| 沂源县| 海淀区| 葵青区| 湘乡市| 望奎县| 云梦县| 庆阳市| 白沙| 新河县|