SFML开发环境部署

  • 背景

    兴趣项目,学习做游戏

  • 需要的组件

    clion编辑器,也可以使用其他编辑器

    gcc编译环境,mingw64 https://sourceforge.net/projects/mingw-w64/files/

    sfml下载 https://www.sfml-dev.org/download/sfml/2.5.1/

    将sfml解压之后复制到mingw64文件夹,主要是为了头文件引入,肯定有更优雅的引入-to do

 

    

 

     在项目中使用cmake引入编译链接,例子  

    

     需要将sfml的bin下所有dll复制到cmake-build-debug中

     其含有视频,音频等等基础api,物理引擎应该也有,目前太菜不知道有什么内容。

 

  • 俄罗斯方块

    照着B站视频做的,感觉和多年前做的cocos2dx有点类似,和web应用不同,感觉更要动脑筋一点。基本上就是一个时序函数,然后中间编写游戏规则。

//
// Created by ctk-vm on 2022/4/24.
//

#ifndef MYPROJECT_TETRIS_H
#define MYPROJECT_TETRIS_H


#include <iostream>
#include <SFML/Graphics.hpp>

const int M = 20;
const int N = 10;
const int RECT_PIXEL = 18;
int field[M][N] = {0};

struct Point{
    int x,y;
} a[4],b[4];

int figures[7][4] = {
        1,3,5,7, // I
        2,4,5,7, // Z
        3,5,4,6, // S
        3,5,4,7, // T
        2,3,5,7, // L
        3,5,7,6, // J
        2,3,4,5  // O
};

bool check(){
    for(int i=0;i<4;i++){
        if(a[i].x < 0 || a[i].x >= N || a[i].y >= M){
            return false;
        }else if(field[a[i].y][a[i].x]){
            return false;
        }
    }
    return true;
};

int tetris_start(){
    srand(time(0));

    sf::RenderWindow window(sf::VideoMode(320,480),"hello sfml");
    sf::Texture t1,t2,t3;
    t1.loadFromFile("..\\images\\Tetris\\tiles.png");
    t2.loadFromFile("..\\images\\Tetris\\background.png");
    t3.loadFromFile("..\\images\\Tetris\\frame.png");

    sf::Sprite s(t1);
    sf::Sprite background(t2);
    sf::Sprite frame(t3);
    s.setTextureRect(sf::IntRect(0,0,RECT_PIXEL,RECT_PIXEL));

    int dx = 0;
    bool rotate = false;
    int colorNum = 1;
    float timer = 0;

    // init
    int initN = rand() % 7;;
    for(int i=0;i<4;i++){
        a[i].x = figures[initN][i] % 2;
        a[i].y = figures[initN][i] / 2;
    }

    sf::Clock clock;

    while(window.isOpen()){
        float delay = 0.3;
        float time = clock.getElapsedTime().asSeconds();
        clock.restart();
        timer += time;

        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                window.close();
            }
            if(event.type == sf::Event::KeyPressed){
                if(event.key.code == sf::Keyboard::Up){
                    rotate = true;
                }else if(event.key.code == sf::Keyboard::Left){
                    dx = -1;
                }else if(event.key.code == sf::Keyboard::Right){
                    dx = 1;
                }else if(event.key.code == sf::Keyboard::Down){
                    delay = 0.03;
                }
            }
        }

        //// MOVE ///
        for(int i=0;i<4;i++){
            b[i]=a[i];
            a[i].x+=dx;
        }
        if(!check()){
            for(int i=0;i<4;i++){
                a[i]=b[i];
            }
        }
        /// Rotate ///
        if(rotate){
            Point p = a[1];
            for(int i=0;i<4;i++){
                int x = a[i].y - p.y;
                int y = a[i].x - p.x;
                a[i].x = p.x - x;
                a[i].y = p.y + y;
            }
            if(!check()){
                for(int i=0;i<4;i++){
                    a[i]=b[i];
                }
            }
        }

        /// Tick ///
        if(timer > delay){
            for(int i=0;i<4;i++){
                b[i] = a[i];
                a[i].y += 1;
            }

            if(!check()){
                for(int i=0;i<4;i++){
                    field[b[i].y][b[i].x] = colorNum;
                }
                colorNum = 1 + rand() % 7;
                int n = rand() % 7;
                for(int i=0;i<4;i++){
                    a[i].x = figures[n][i] % 2;
                    a[i].y = figures[n][i] / 2;
                }
            }
            timer = 0;
        }
        /// check line ///
        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--;
            }
        }

        dx = 0;
        rotate = false;
        window.clear(sf::Color::White);
        window.draw(background);

        /// draw ///
        for(int i=0;i<M;i++){
            for(int j=0;j<N;j++){
                if(field[i][j] != 0){
                    s.setTextureRect(sf::IntRect(field[i][j]*RECT_PIXEL,0,RECT_PIXEL,RECT_PIXEL));
                    s.setPosition(j*RECT_PIXEL,i*RECT_PIXEL);
                    s.move(28,31); //offset
                    window.draw(s);
                }
            }
        }
        for(int i=0;i<4;i++){
            s.setTextureRect(sf::IntRect(colorNum*RECT_PIXEL,0,RECT_PIXEL,RECT_PIXEL));
            s.setPosition(a[i].x*RECT_PIXEL,a[i].y*RECT_PIXEL);
            s.move(28,31); //offset
            window.draw(s);
        }
        window.draw(frame);
        window.display();
    }
    return 0;
}

#endif //MYPROJECT_TETRIS_H

 

 

posted @ 2022-01-24 19:02  天目山电鳗  阅读(206)  评论(0编辑  收藏  举报