俄罗斯方块最终
俄罗斯方块
| 这个作业属于哪个课程 | 2020面向对象程序设计张栋班 |
|---|---|
| 这个作业要求在哪里 | 我罗斯方块 |
| 这个作业的目标 | 代码的 git 仓库链接。 运行截图/运行视频。 代码要点 收获与心得。 依然存在的问题。 |
| 项目地址 | https://github.com/qhl1/Tetris |
| 其他参考文献 | |
| 小组成员 | 学号 |
| 邱翰林 | 031903121 |
| 卢梦晗 | 031903145 |
| 阿亚库仔·卡哈尔曼 | 031903137 |
运行视频
https://www.bilibili.com/video/bv1ti4y1x7s3
代码要点
我们小组用SFML进行游戏的渲染
设定总的方块和地图
int blocks[7][4] = {
1,3,5,7,//1
2,4,5,7,//Z1
3,5,4,6,//Z2
3,5,4,7,//T
2,3,5,7,//L
3,5,7,6,//J
2,3,4,5,//田
};
const int ROW_COUNT = 20;
const int COL_COUNT = 10;
//游戏区的表示
//table[i][j]==0 表示第i行第j列,是空白的,否则表示有方块
//table[i][j]==1 表示有方块,而且是第1种方块(使用第一种颜色)
int table[ROW_COUNT][COL_COUNT] = { 0 };
int blockIndex;//表示当前方块种类
//游戏方块表示
struct Point {
int x,y;
}curBlock[4],bakBlock[4];
方块的生成
函数void newBlock()随机获得一个新的方块
void newBlock() {
//获取一个随机值(1-7)
blockIndex = 1 + rand() % 7;
int n = blockIndex - 1;
//序号%2就是x坐标
//序号/2就是y坐标
for (int i = 0; i < 4; i++)
{
curBlock[i].x = blocks[n][i] % 2;
curBlock[i].y = blocks[n][i] / 2;
}
}
对方块的各种操作
函数void doRotate()降落中的方块旋转
函数void moveLeftRight(int offset)方块进行左右移动
*函数void keyEvent(RenderWindow window)根据键盘的输入对方块进行旋转,加速,左右移动等操作
函数bool check()用来检测方块合法性()
设置主方块curBlock[4]和备份方块bakBlock[4]
每次方块移动一个位置备用方块都会备份一次
当方块不能移动时则回到备份的方块
比如
void doRotate()
{
if (blockIndex == 7)
{
return;
}
//先备份
for (int i = 0; i < 4; i++)
{
bakBlock[i] = curBlock[i];
}
Point p = curBlock[1];//旋转中心
/*
x坐标计算:p.x-a[i].y+p.y
y坐标计算:a[i].x-p.x+p.y
*/
for (int i = 0; i < 4; i++)
{
Point tmp = curBlock[i];
curBlock[i].x = p.x - tmp.y + p.y;
curBlock[i].y = tmp.x - p.x + p.y;
}
//检查合法性
if (!check())
{
for (int i = 0; i < 4; i++)
{
curBlock[i] = bakBlock[i];
}
}
}
当检测到方块移动后不合法时方块不旋转
方块落地
函数void drop()判断方块是否落地并固化方块
函数void cleanLine()用来消除满行方块并播放音效
对方块和分数的渲染
**函数void drawBlocks(Sprite spriteBlock,RenderWindow window)来画出方块图案
函数void initScore() 显示分数
背景音乐
Music music;
if (!music.openFromFile("Elizabeth Naccarato - Unspoken.wav"))
{
return -1;
}
music.setLoop(true);
music.play();
//添加背景音乐
SoundBuffer xiaochu;
if (!xiaochu.loadFromFile("xiaochu.wav"))
{
return -1;
}
sou.setBuffer(xiaochu);
//添加消除音效
游戏整体背景渲染
//1.创建游戏窗口
//1.1准备窗口的背景图片
RenderWindow window(
VideoMode(320, 416),//窗口模式,大小
"Rock 2020");
//2.添加游戏背景
Texture t1;//把图片文件加载到内存
t1.loadFromFile("image/bg2.jpg");
Sprite spriteBg(t1);//根据图片创建精灵
Texture t2;
t2.loadFromFile("image/tiles.png");
Sprite spriteBlock(t2);
//相框
Texture t3;
t3.loadFromFile("image/frame.png");
Sprite spriteFrame(t3);
initScore();
//渲染精灵
window.draw(spriteBg);
window.display();//刷新并显示窗口
存在的问题
1.没有预览框
2.没有判断结束
3.双人版还没有写出来

浙公网安备 33010602011771号