千家信息网

c++如何实现俄罗斯方块游戏

发表于:2024-11-25 作者:千家信息网编辑
千家信息网最后更新 2024年11月25日,这篇文章主要介绍"c++如何实现俄罗斯方块游戏",在日常操作中,相信很多人在c++如何实现俄罗斯方块游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"c++如何实现俄罗
千家信息网最后更新 2024年11月25日c++如何实现俄罗斯方块游戏

这篇文章主要介绍"c++如何实现俄罗斯方块游戏",在日常操作中,相信很多人在c++如何实现俄罗斯方块游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"c++如何实现俄罗斯方块游戏"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

俄罗斯方块c++

1.创建项目
2.总共需要创建两个文件,一个main.cpp,一个是elsfk2.h。本人使用的编译器是vs2019.

3.在项目的源文件夹下创建一个文件夹image

4.把下面两张图片重命名好放进刚创建的文件夹

elsfk.jpg

elsfk_block.jpg

5.把下列代码分别复制到对应文件中

-----------------------------------------------------------------------------------以下是main.cpp 文件的内容#include"elsfk2.h"int main() {        srand((int)time(0));        //创建游戏窗口        RenderWindow window(VideoMode(COL*18/*+100*/, ROW*18-36),"ELSFK");        //添加游戏背景        Texture t1,t2;        t1.loadFromFile("image/elsfk.jpg");        t2.loadFromFile("image/elsfk_block.jpg");        Sprite sprite_Bg(t1);        Sprite sprite_block(t2);        NewBlock();        Clock begin;        float time1 = 0, time2 = 0;        while (window.isOpen()) {                time2 = begin.getElapsedTime().asSeconds();                begin.restart();                time1 += time2;                //等待用户按下按键                keyEvent(&window);                if (time1 > delay) {                        time1 = 0;                        blockDrop();                                        }                delay = SPEED_NOM;                window.draw(sprite_Bg);                drawBlock(&sprite_block, &window);                window.display();                }}--------------------------------------------------------------------------------以下为elsfk2.cpp#pragma once#include                               //图像处理头文件//#include                        #includeusing namespace sf;#define ROW 22                                                        //行#define COL 10                                                        //列#define SPEED_NOM 0.5                                 //不加速运动的时间间隔#define SPEED_QIK 0.05                                        //加速运动的时间间隔int map[ROW][COL] = { 0 };                             //游戏区域大小int blocktype;                                                     //方块类型int Delete=0;                                                      //删除的行数float delay = SPEED_NOM;                         //时间间隔void Move_x(int);void Rotate();void ClearBlock();void NewBlock();void blockDrop();void keyEvent(RenderWindow*);void drawBlock(Sprite*, RenderWindow*);bool check();//存放7种方块的二维数组int a[7][4] = {        1,3,5,7,        2,4,5,7,        3,5,4,6,        3,5,4,7,        2,3,5,7,        3,5,7,6,        2,3,4,5,};//点的结构体struct point {        int x, y;};//当前方块point block[4];//方块的备份point bakblock[4];//处理按键void keyEvent(RenderWindow *w) {        Event e;        bool rotate = 0;        int x = 0;        while (w->pollEvent(e)) {                if (e.type == Event::Closed) {                        w->close();                }                if (e.type == Event::KeyPressed) {                        switch (e.key.code) {                        case Keyboard::Up:                                rotate = 1;                                break;                        case Keyboard::Left:                                x = -1;                                break;                        case Keyboard::Right:                                x = 1;                                break;                        default:                                break;                        }                }                if (Keyboard::isKeyPressed(Keyboard::Down)) {                        delay = SPEED_QIK;                }                if (x) {                        Move_x(x);                }                if (rotate) {                        Rotate();                }        }}//消除完成的行void ClearBlock() {        int k = ROW - 1;        for (int i = ROW - 1; i > 0; i--) {                int count = 0;                for (int j = 0; j < COL; j++) {                        if (map[i][j]) {                                count++;                        }                        map[k][j] = map[i][j];                }                if (count < COL) {                        k--;                }                        }}//检查移动合理性bool check() {        for (int i = 0; i < 4; i++){                if(       block[i].x<0||block[i].x>=COL||block[i].y>=ROW||                map[block[i].y][block[i].x]){                        return 0;                }        }        return 1;}//方块降落void blockDrop() {                for (int i = 0; i < 4; i++) {                bakblock[i] = block[i];                block[i].y++;        }        if (!check()) {                for (int j = 0; j < 4; j++){                        map[bakblock[j].y][bakblock[j].x] = blocktype;                }                NewBlock();                ClearBlock();        }}//左右移动void Move_x(int x) {        for (int  i = 0; i < 4; i++){                bakblock[i] = block[i];                block[i].x += x;        }        if (!check()) {                for (int i = 0; i < 4; i++) {                        block[i] = bakblock[i];                }        }}//旋转******************************    importantvoid Rotate() {        if (blocktype == 7) {                return;        }        point p = block[1];        for (int i = 0; i < 4; i++) {                bakblock[i] = block[i];                block[i].x = p.x - bakblock[i].y + p.y;                block[i].y = bakblock[i].x - p.x + p.y;        }        if (!check()) {                for (int i = 0; i < 4; i++) {                        block[i] = bakblock[i];                }        }}//生成方块void NewBlock() {        blocktype = 1 + rand() % 7;        for (int i = 0; i < 4; i++) {                block[i].x = a[blocktype-1][i] % 2+4;                block[i].y = a[blocktype-1][i] / 2;        }}//绘制方块void drawBlock(Sprite *b, RenderWindow *w) {        //完成的方块        for (int i = 0; i < ROW; i++) {                for (int j = 0; j < COL; j++) {                        if (map[i][j] != 0) {                                b->setTextureRect(IntRect(map[i][j]*18,0,18,18));                                b->setPosition(j * 18, i * 18);                                b->move(0, -36);                                w->draw(*b);                        }                }        }        for (int j = 0; j < 4; j++)        {                b->setTextureRect(IntRect(blocktype * 18, 0, 18, 18));                b->setPosition(block[j].x * 18, block[j].y * 18);                b->move(0, -36);                w->draw(*b);        }}

到此,关于"c++如何实现俄罗斯方块游戏"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0