c++实现俄罗斯方块游戏代码

c++如何俄罗斯方块,刚学c++想在朋友面前炫一把,下面是c++实现俄罗斯方块游戏源代码。

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

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

在这里插入图片描述

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

image.png

elsfk.jpg

elsfk_block.jpg

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

-----------------------------------------------------------------------------------
以下是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<SFML/Graphics.hpp>               //图像处理头文件
//#include<SFML/Audio.hpp>           
#include<time.h>
using 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];
 
        }
    }
 
}
  
//旋转******************************    important
void 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);
    }
 
}


郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • vs2015打包安装程序,vs2015程序打包,VS2022实现VC++打包生成安装文件图文详细历程
  • vc++6.0的快捷键,vc 快捷键
  • vc++6.0的快捷键,vc 快捷键,VC6.0常用快捷键大全
  • 绘制圆角矩形的方法,c++ 画矩形,C#画圆角矩形的方法
  • 懒汉式和饿汉式代码,单列模式懒汉和饿汉,C++单例模式的懒汉模式和饿汉模式详解
  • 好用的C++编译器,c++编译软件哪个好
  • semaphore c#,c++ semaphore
  • semaphore c#,c++ semaphore,C++中Semaphore内核对象用法实例
  • dev-c++使用教程,dev c++安装教程
  • dev-c++使用教程,dev c++安装教程,Dev C++ 安装及使用方法(图文教程)
  • C里面指针常量和常量指针的区别,c++指针常量和常量指针
  • C里面指针常量和常量指针的区别,c++指针常量和常量指针,简单总结C++中指针常量与常量指针的区别
  • com组件初始化失败,c#开发com组件,C++中COM组件初始化方法实例分析
  • c++静态成员变量使用,c++静态成员函数和静态成员变量
  • c++静态成员变量使用,c++静态成员函数和静态成员变量,详解c++ 静态成员变量
  • 留言与评论(共有 条评论)
       
    验证码: