偶然看到大神使用 SFML 制作游戏,简单学习了一下这个库的使用并且仿照YouTube上大神的思路做了一个俄罗斯方块,目前只实现了出现方块、消除方块的功能,随着慢慢学习一点点继续修改吧;
资源:
源码:
/*****************************************************************************
* Copyright (C) 2020 LL 596214569@qq.com.
*
* This file is part of SFML test project.
*
* @file SFML.cpp
* @brief
* Details.
*
* @author LL
* @email 596214569@qq.com
* @version 1.0.0.1
* @date 596214569@163.com
*
*----------------------------------------------------------------------------
* Remark : Description
*----------------------------------------------------------------------------
* Change History :
* <Date> | <Version> | <Author> | <Description>
*----------------------------------------------------------------------------
* 2020/01/05 | 1.0.0.1 | LL | Create file
*----------------------------------------------------------------------------
*
*****************************************************************************/
// SFML.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
using namespace sf;
#include <time.h>
#include <WINDOWS.H>#define RectNum 4 // 每个俄罗斯方块的元素个数
const int M = 20;// 行
const int N = 10;// 列/// 整个游戏区
int field[M][N] = { 0 };/// 图形每个方块的坐标点
struct point
{int x;int y;point() { x = 0; y = 0; };
};/// 图形
/// * * * *
/// * ** ** ** * * **
/// * * ** * * ** *
/// * * * *
int figures[7][4] =
{1,3,5,7,// I2,4,5,7,// Z3,5,4,6,// 反向Z3,5,4,7,// T2,3,5,7,// L3,5,7,6,// J2,3,4,5,// 口
};// 检测当前图形是否合法
bool Check(point p[4])
{for (int i = 0; i < RectNum; i++){if (p[i].x < 0 || p[i].x >= N || p[i].y >= M){return false;}else if( field[p[i].y][p[i].x] ){return false;}}return true;
}// 随机选中图形
void SetFigures(point p[4])
{int n = rand() % 7;for (int i = 0; i < RectNum; i++){p[i].x = figures[n][i] % 2;p[i].y = figures[n][i] / 2;}
}int main()
{RenderWindow window(VideoMode(280, 560), "Game");Texture t;if (!t.loadFromFile("images/titles.png")){MessageBox(NULL, TEXT("Pictures not found!"), TEXT("MSG"), MB_OK);return -1;}Sprite s(t);s.setTextureRect(IntRect(0,0,28,28));// 精灵// 初始化相关变量Clock clock;float delay = 0.3f;float time1 = 0;int nDeltaX = 0;bool bRotate = false;int nColorNum = 1;point figureNow[4], figureTemp[4];SetFigures(figureNow);while ( window.isOpen() ){nDeltaX = 0;bRotate = false;delay = 0.3f;srand((unsigned)time(NULL));float time = clock.getElapsedTime().asSeconds();clock.restart();time1 += time;Event evt;while (window.pollEvent(evt)){if (evt.type == Event::Closed){window.close();}if (evt.type == Event::KeyPressed){if (evt.key.code == Keyboard::Up){bRotate = true;}else if (evt.key.code == Keyboard::Left){nDeltaX = -1;}else if (evt.key.code == Keyboard::Right){nDeltaX = 1;}else if (evt.key.code == Keyboard::Down){delay = 0.5;}}}for (int i = 0; i < RectNum; i++){figureTemp[i] = figureNow[i];figureNow[i].x += nDeltaX;}if (!Check(figureNow)){for (int i = 0; i < RectNum; i++){figureNow[i] = figureTemp[i];}}// 旋转if (bRotate){point p = figureNow[1];for (int i = 0; i < RectNum; i++){int x = figureNow[i].y - p.y;int y = figureNow[i].x - p.x;figureNow[i].x = p.x - x;figureNow[i].y = p.y + y;}if (!Check(figureNow)){for (int i = 0; i < RectNum; i++){figureNow[i] = figureTemp[i];}}}if (time1 > delay){for (int i = 0; i < RectNum; i++){figureTemp[i] = figureNow[i];figureNow[i].y += 1;}if (!Check(figureNow)){for (int i = 0; i < RectNum; i++){field[figureTemp[i].y][figureTemp[i].x] = nColorNum;}nColorNum = 1 + rand() % 6;SetFigures(figureNow);}time1 = 0;}// 消除已满的行int k = M - 1;for (int i = M - 1; i > 0; i--){int count = 0;for (int j = 0; j < N; j++){if (field[i][j]){count++;}field[k][j] = field[i][j];}if (count < N){k--;}}// 图形绘制window.clear(Color::White);for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (field[i][j] == 0){continue;}s.setTextureRect(IntRect(field[i][j] * 28, 0, 28, 28));s.setPosition(static_cast<float>(j * 28), static_cast<float>(i * 28));window.draw(s);}}for (int i = 0; i < RectNum; i++){s.setTextureRect(IntRect(nColorNum * 28, 0, 28, 28));s.setPosition(static_cast<float>(figureNow[i].x * 28), static_cast<float>(figureNow[i].y * 28));window.draw(s);}window.display();}return 0;
}
运行效果;