目录

  • 一、项目介绍
  • 二、编译运行
    • 1、编译过程
    • 2、所需文件

一、项目介绍

俄罗斯方块:顾名思义,俄罗斯方块自然是俄罗斯人发明的。这人叫阿列克谢·帕基特诺夫(Алексей Пажитнов 英文:Alexey Pazhitnov)。俄罗斯方块原名是俄语Тетрис(英语是Tetris),这个名字来源于希腊语tetra,意思是“四”,而游戏的作者最喜欢网球(tennis)。于是,他把两个词tetra和tennis合而为一,命名为Tetris,这也就是俄罗斯方块名字的由来。
该项目全部由c++编码实现,代码一共不到200,实现了基础的玩法,项目原地址https://github.com/taylorconor/tinytetris。

逛了一圈GitHub,突然发现了个小巧有意思的游戏,没忍住就给拉取下来编译了一把,分分钟就能玩上童年记忆!这不比博人传热血?

二、编译运行

1、编译过程

  • centos环境:

第一步,安装g++编译环境

yum install gcc-c++

第二步,安装编译依赖第三方库

 yum install ncurses-devel

第三步,编译生成可执行文件

make

第四步,玩

./tinytetris-commented

一共就四条命令,一条条输入就完事儿了,就突出一个特点,一个字——简单!

2、所需文件

  • tinytetris-commented.cpp
#include <ctime>
#include <curses.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>// block layout is: {w-1,h-1}{x0,y0}{x1,y1}{x2,y2}{x3,y3} (two bits each)
int x = 431424, y = 598356, r = 427089, px = 247872, py = 799248, pr,c = 348480, p = 615696, tick, board[20][10],block[7][4] = {{x, y, x, y},{r, p, r, p},{c, c, c, c},{599636, 431376, 598336, 432192},{411985, 610832, 415808, 595540},{px, py, px, py},{614928, 399424, 615744, 428369}},score = 0;// extract a 2-bit number from a block entry
int NUM(int x, int y) { return 3 & block[p][x] >> y; }// create a new piece, don't remove old one (it has landed and should stick)
void new_piece() {y = py = 0;p = rand() % 7;r = pr = rand() % 4;x = px = rand() % (10 - NUM(r, 16));
}// draw the board and score
void frame() {for (int i = 0; i < 20; i++) {move(1 + i, 1); // otherwise the box won't drawfor (int j = 0; j < 10; j++) {board[i][j] && attron(262176 | board[i][j] << 8);printw("  ");attroff(262176 | board[i][j] << 8);}}move(21, 1);printw("Score: %d", score);refresh();
}// set the value fo the board for a particular (x,y,r) piece
void set_piece(int x, int y, int r, int v) {for (int i = 0; i < 8; i += 2) {board[NUM(r, i * 2) + y][NUM(r, (i * 2) + 2) + x] = v;}
}// move a piece from old (p*) coords to new
int update_piece() {set_piece(px, py, pr, 0);set_piece(px = x, py = y, pr = r, p + 1);
}// remove line(s) from the board if they're full
void remove_line() {for (int row = y; row <= y + NUM(r, 18); row++) {c = 1;for (int i = 0; i < 10; i++) {c *= board[row][i];}if (!c) {continue;}for (int i = row - 1; i > 0; i--) {memcpy(&board[i + 1][0], &board[i][0], 40);}memset(&board[0][0], 0, 10);score++;}
}// check if placing p at (x,y,r) will be a collision
int check_hit(int x, int y, int r) {if (y + NUM(r, 18) > 19) {return 1;}set_piece(px, py, pr, 0);c = 0;for (int i = 0; i < 8; i += 2) {board[y + NUM(r, i * 2)][x + NUM(r, (i * 2) + 2)] && c++;}set_piece(px, py, pr, p + 1);return c;
}// slowly tick the piece y position down so the piece falls
int do_tick() {if (++tick > 30) {tick = 0;if (check_hit(x, y + 1, r)) {if (!y) {return 0;}remove_line();new_piece();} else {y++;update_piece();}}return 1;
}// main game loop with wasd input checking
void runloop() {while (do_tick()) {usleep(10000);if ((c = getch()) == 'a' && x > 0 && !check_hit(x - 1, y, r)) {x--;}if (c == 'd' && x + NUM(r, 16) < 9 && !check_hit(x + 1, y, r)) {x++;}if (c == 's') {while (!check_hit(x, y + 1, r)) {y++;update_piece();}remove_line();new_piece();}if (c == 'w') {++r %= 4;while (x + NUM(r, 16) > 9) {x--;}if (check_hit(x, y, r)) {x = px;r = pr;}}if (c == 'q') {return;}update_piece();frame();}
}// init curses and start runloop
int main() {srand(time(0));initscr();start_color();// colours indexed by their position in the blockfor (int i = 1; i < 8; i++) {init_pair(i, i, 0);}new_piece();resizeterm(22, 22);noecho();timeout(0);curs_set(0);box(stdscr, 0, 0);runloop();endwin();
}
  • Makefile
LDFLAGS=-lcursesall: tinytetris-commentedclean:rm -f tinytetris tinytetris-commented

❤️❤️❤️ 如果本文有点意思,请不要忘了点赞、关注、收藏哦!灰常感谢! ❤️❤️❤️

❤️ 硬核玩游戏:200行代码给你整个俄罗斯方块 ❤️相关推荐

  1. js websocket同步等待_WebSocket硬核入门:200行代码,教你徒手撸一个WebSocket服务器...

    本文原题"Node.js - 200 多行代码实现 Websocket 协议",为了提升内容品质,有较大修订. 1.引言 最近正在研究 WebSocket 相关的知识,想着如何能自 ...

  2. 基于Python-Pygame:200行代码实现完整的俄罗斯方块

    源码: # coding : utf-8#: pip install pygame import random import sys import pygame#: 颜色定义 COLOR_WHITE ...

  3. 2048左移JAVA代码实现,200 行代码实现 2048 游戏

    原标题:200 行代码实现 2048 游戏 作者:Guolanzhe 原文: http://www.guolanzhe.com/?p=522创建游戏文件 2048.py 首先导入需要的包: impor ...

  4. Java控制台游戏~600行代码实现打怪小游戏

    Java控制台游戏~600行代码实现打怪小游戏(多图预警) 一,先放个启动界面(一些英雄,怪物技能介绍跟装备属性都写在里边): 二,在这个简单的小游戏里,你可以体验到: 1.打怪: 2.随机玩法寻宝: ...

  5. 【独立游戏】黎明之刃——3D硬核ARPG游戏

    - 项目概念:<黎明之刃>深受主机上<鬼泣>.<黑暗之魂>等硬核动作游戏的启发,是Sunset Game 团队成员合力创作出的拥有全新想法的一款动作游戏 - 我们生 ...

  6. blockchain 区块链200行代码:在JavaScript实现的一个简单的例子

    blockchain 区块链200行代码:在JavaScript实现的一个简单的例子 了解blockchain的概念很简单(区块链,交易链块):它是分布式的(即不是放置在同一台机器上,不同的网络设备上 ...

  7. JavaScript开发区块链只需200行代码

    JavaScript开发区块链只需200行代码 用JavaScript开发实现一个简单区块链.通过这一开发过程,你将理解区块链技术是什么:区块链就是一个分布式数据库,存储结构是一个不断增长的链表,链表 ...

  8. C# winform 简单五子棋 200行代码实现人机对战

    1.功能需求 接上篇博文,本文描述简单人机对战实现过程,只是简单实现考虑走一步策略,如果要想实现走多步策略,可以在本文估值算法的基础上用极大极小值配合剪枝算法,实现考虑多步策略,这样ai会显得更加聪明 ...

  9. C# winform 简单五子棋 200行代码实现双人对战

    1.需求 基于C# winform用200行代码实现简易五子棋双人对战,支持悔棋,需要的知识有C# winform界面,C#,以及几张素材图片. 2.界面 界面设计如图1所示,背影图是用Graphic ...

  10. 200行代码如何实现人脸识别开锁应用?

    多种条件限制之下,如何完成真人实景游戏场景下的人脸识别开锁功能?云加社区邀请到腾讯科技产品经理-高树磊,分享他是如何用200行代码,从系统架构.硬件选型.到系统搭建,一步步地实现此精致小巧的人脸识别开 ...

最新文章

  1. dropdownlist绑定的二种方法
  2. GeoPandas 笔记: GeoDataFrame.plot()
  3. 贴牌是赢在当下,创牌才会更有未来
  4. python实现数据库事务回滚_使用Python脚本实现MySQL误操作的快速回滚
  5. 为什么REST如此重要
  6. 2019java形势,2019Java开发还有哪些发展
  7. .h 与 .hpp 文件
  8. 云服务器 ECS > 快照 > 快照概述
  9. Erlang与ActionScript3采用JSON格式进行Socket通讯
  10. PYPL 1 月 IDE 榜单:Visual Studio Code 猛追 IntelliJ
  11. spark-streaming-连接kafka的两种方式
  12. python常用库大全文库_Python库参考手册
  13. I2S音频接口的理解
  14. 转载python操作wps V9 API,测试好用
  15. Word论文参考文献排版
  16. CKPlayer视频地址加密方法
  17. Studio5000和SE中如何使用替换功能
  18. 【HDU 6608】Fansblog(威尔逊定理+逆元+快速乘+快速幂)
  19. U74LVC1G07G-SOT353R-TG
  20. Apache启用GZIP压缩网页传输

热门文章

  1. 住房和城乡建设部关于2022年第十批一级建造师注册人员名单的公告
  2. unity动态加载FBX模型
  3. CF1313C2 Skyscrapers (hard version) -单调栈优化dp
  4. python re 替换_python字符串替换之re.sub()
  5. html p标签嵌套a,HTML标签嵌套规则详细归纳适合新手朋友
  6. 【浙大版《Python 程序设计》题目集(解)】第4章-9 查询水果价格(15分)
  7. 目标转化出价多少_浅谈什么是 oCPC 目标转化出价?
  8. 网络管理(课程总结3)Week12 澳大利亚维多利亚大学VIT1104 Computer Networks
  9. android跳转谷歌地图导航,Android使用intent调取导航或者地图
  10. android rar文件怎么打开方式,rar文件手机上怎么打开 手机怎么打开zip文件