基于easyx的俄罗斯方块
时间: 2023-11-17 12:07:27 浏览: 103
基于C++和EasyX 写的俄罗斯方块小游戏源码+项目说明文件.zip
好的,下面是一个基于easyx的俄罗斯方块游戏的示例代码,供您参考:
```c++
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
#define BLOCK_SIZE 20
int block[7][4][4][4] = {
{
{
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 0, 2, 0},
{0, 2, 2, 2},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 2, 0, 0},
{0, 2, 2, 0},
{0, 2, 0, 0},
},
{
{0, 0, 0, 0},
{0, 2, 2, 2},
{0, 2, 0, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 2, 0},
{0, 2, 2, 0},
{0, 0, 2, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 3, 3, 0},
{0, 0, 3, 3},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 3, 0},
{0, 3, 3, 0},
{0, 3, 0, 0},
},
{
{0, 0, 0, 0},
{0, 3, 3, 0},
{0, 0, 3, 3},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 3, 0},
{0, 3, 3, 0},
{0, 3, 0, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 4, 0, 0},
{0, 4, 4, 4},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 4, 0},
{0, 0, 4, 4},
{0, 0, 4, 0},
},
{
{0, 0, 0, 0},
{0, 4, 4, 4},
{0, 0, 4, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 4, 0},
{0, 4, 4, 0},
{0, 0, 4, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 5, 5, 0},
{0, 5, 0, 0},
{0, 5, 0, 0},
},
{
{0, 0, 0, 0},
{0, 5, 0, 0},
{0, 5, 5, 5},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 5, 0},
{0, 0, 5, 0},
{0, 5, 5, 0},
},
{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 5, 5, 5},
{0, 0, 5, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 6, 0, 0},
{0, 6, 6, 6},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 6, 0, 0},
{0, 6, 6, 0},
{0, 6, 0, 0},
},
{
{0, 0, 0, 0},
{0, 6, 6, 6},
{0, 0, 6, 0},
{0, 0, 0, 0},
},
{
{0, 0, 0, 0},
{0, 0, 6, 0},
{0, 6, 6, 0},
{0, 0, 6, 0},
},
},
{
{
{0, 0, 0, 0},
{0, 7, 7, 0},
{0, 0, 7, 0},
{0, 0, 7, 0},
},
{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 7, 7, 7},
{0, 0, 0, 7},
},
{
{0, 0, 0, 0},
{0, 7, 0, 0},
{0, 7, 0, 0},
{0, 7, 7, 0},
},
{
{0, 0, 0, 0},
{0, 7, 0, 0},
{0, 7, 7, 7},
{0, 0, 0, 0},
},
},
};
int map[WIDTH][HEIGHT] = {0}; // 地图数组
int cur[4][4]; // 当前方块数组
int cur_x, cur_y; // 当前方块的左上角坐标
int cur_type, cur_state; // 当前方块的类型和状态
int next_type; // 下一个方块的类型
int score; // 得分
int speed = 200; // 方块下落速度
DWORD last_time; // 上一次方块下落的时间
bool game_over = false; // 游戏是否结束
// 初始化当前方块
void init_cur()
{
cur_type = next_type;
next_type = rand() % 7;
cur_state = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cur[i][j] = block[cur_type][cur_state][i][j];
}
}
cur_x = WIDTH / 2 - 2;
cur_y = 0;
}
// 绘制一个方块
void draw_block(int x, int y, int type)
{
setfillcolor(type + 1);
solidrectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE);
setcolor(BLACK);
rectangle(x * BLOCK_SIZE, y * BLOCK_SIZE, (x + 1) * BLOCK_SIZE, (y + 1) * BLOCK_SIZE);
}
// 绘制地图
void draw_map()
{
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < HEIGHT; j++)
{
if (map[i][j] != 0)
{
draw_block(i, j, map[i][j] - 1);
}
}
}
}
// 绘制当前方块
void draw_cur()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0)
{
draw_block(cur_x + j, cur_y + i, cur_type);
}
}
}
}
// 判断当前方块是否可以下落
bool can_move_down()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0)
{
if (cur_y + i >= HEIGHT - 1 || map[cur_x + j][cur_y + i + 1] != 0)
{
return false;
}
}
}
}
return true;
}
// 将当前方块放入地图中
void put_cur_to_map()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0)
{
map[cur_x + j][cur_y + i] = cur_type + 1;
}
}
}
}
// 消除满行
void clear_full_rows()
{
for (int i = HEIGHT - 1; i >= 0; i--)
{
bool full = true;
for (int j = 0; j < WIDTH; j++)
{
if (map[j][i] == 0)
{
full = false;
break;
}
}
if (full)
{
score += 10;
for (int k = i; k >= 1; k--)
{
for (int j = 0; j < WIDTH; j++)
{
map[j][k] = map[j][k - 1];
}
}
i++;
}
}
}
int main()
{
// 初始化图形界面
initgraph(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE, SHOWCONSOLE);
setbkcolor(WHITE);
cleardevice();
settextcolor(BLACK);
settextstyle(20, 0, _T("宋体"));
// 初始化随机数种子
srand((unsigned)time(NULL));
// 开始游戏循环
last_time = GetTickCount();
while (!game_over)
{
// 绘制地图和当前方块
cleardevice();
draw_map();
draw_cur();
// 处理用户输入
if (_kbhit())
{
int key = _getch();
if (key == 'a' || key == 'A')
{
bool can_move_left = true;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0)
{
if (cur_x + j <= 0 || map[cur_x + j - 1][cur_y + i] != 0)
{
can_move_left = false;
break;
}
}
}
if (!can_move_left)
{
break;
}
}
if (can_move_left)
{
cur_x--;
}
}
else if (key == 'd' || key == 'D')
{
bool can_move_right = true;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0)
{
if (cur_x + j >= WIDTH - 1 || map[cur_x + j + 1][cur_y + i] != 0)
{
can_move_right = false;
break;
}
}
}
if (!can_move_right)
{
break;
}
}
if (can_move_right)
{
cur_x++;
}
}
else if (key == 'w' || key == 'W')
{
int next_state = (cur_state + 1) % 4;
int next_type = block[cur_type][next_state];
bool can_rotate = true;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (next_type[i][j] != 0)
{
if (cur_x + j < 0 || cur_x + j >= WIDTH || cur_y + i >= HEIGHT || map[cur_x + j][cur_y + i] != 0)
{
can_rotate = false;
break;
}
}
}
if (!can_rotate)
{
break;
}
}
if (can_rotate)
{
cur_state = next_state;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cur[i][j] = next_type[i][j];
}
}
}
}
else if (key == 's' || key == 'S')
{
speed = 50;
}
}
else
{
speed = 200;
}
// 判断当前方块是否可以下落
if (can_move_down())
{
// 计算时间差,控制方块下落速度
DWORD current_time = GetTickCount();
if (current_time - last_time >= speed)
{
last_time = current_time;
cur_y++;
}
}
else
{
// 将当前方块放入地图中
put_cur_to_map();
// 消除满行
clear_full_rows();
// 初始化下一个方块
init_cur();
// 判断是否游戏结束
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (cur[i][j] != 0 && map[cur_x + j][cur_y + i] != 0)
{
game_over = true;
break;
}
}
if (game_over)
{
break;
}
}
}
// 绘制分数和下一个方块
TCHAR str_score[16], str_next[16];
_stprintf_s(str_score, _T("得分:%d"), score);
_stprintf_s(str_next, _T("下一个:"));
outtextxy(WIDTH * BLOCK_SIZE + 40, 50, str_score);
outtextxy(WIDTH * BLOCK_SIZE + 40, 150, str_next);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (block[next_type][0][i][j] != 0)
{
draw_block(WIDTH + 3 + j, 6 + i, next_type);
}
}
}
// 延时
Sleep(10);
}
// 游戏结束
cleardevice();
settextstyle(30, 0, _T("宋体"));
outtextxy(WIDTH * BLOCK_SIZE / 2 - 100, HEIGHT * BLOCK_SIZE / 2 - 30, _T("游戏结束"));
_stprintf_s(str_score, _T("得分:%d"), score);
outtextxy(WIDTH * BLOCK_SIZE / 2 - 100, HEIGHT * BLOCK_SIZE / 2 + 10, str_score);
_getch();
closegraph();
return 0;
}
```
该示例代码基于 easyx 图形库实现了一个简单的俄罗斯方块游戏,包括方块的下落、移动、旋转、消除满行、得分统计和游戏结束等功能。您可以根据自己的需要进行修改和拓展。
阅读全文