首页 > 作文

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

更新时间:2023-04-04 10:09:19 阅读: 评论:0

俄罗斯方块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.getelapdtime().asconds()金号毛巾广告;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::clod) {w->clo();}if (e.type == event::keypresd) {switch (e.key.code) {ca keyboard::up:rotate = 1;break;ca keyboard::left:x = -1;break;ca keyboard::right:x = 1;break;default:break;}}if (keyboard::iskeypresd(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 real 百度云[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] = b教师节画画简单又漂亮locktype;}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 - bak发明人block[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->ttexturerect(intrect(map[i][j]*18,0,18,18));b->tposition(j * 18, i * 18);b->move(0, -36);w->draw(*b);}}}for (int j = 0; j < 4; j++){b->ttexturerect(intrect(blocktype * 18, 0, 18, 18));b->tposition(block[j].x * 18, block[j].y * 18);b->move(0, -36);w->draw(*b);}}

到此这篇关于c++实现俄罗斯方块游戏代码的文章就介绍到这了,更多相关c++俄罗斯方块内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 10:09:17,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7143c1395091150dbba8758bec79cf46.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:c++实现俄罗斯方块游戏代码.doc

本文 PDF 下载地址:c++实现俄罗斯方块游戏代码.pdf

下一篇:返回列表
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图