SetupBBS 板


// :p 刚才贴出的代码使用中按 ^L 会乱屏,用这个代码就不会了。 // 抱歉 /* so/tetris.c */ /* 俄罗斯方块在线游戏 */ /* 原作者: [email protected] */ /* 改写者: [email protected] */ #include "bbs.h" #ifdef HAVE_TTY_GAME #define MAX_MAP_WIDTH 10 #define MAX_MAP_HEIGHT 20 #define FN_TETRIS_REC "game/tetris.top" #define BLOCK_CHAR "■" static int last_ypos, last_xpos, last_style, last_dir; static int game_times, max_lines, max_scores; static int map[MAX_MAP_HEIGHT + 1][MAX_MAP_WIDTH + 2] = { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; /* 1. ■ ■ (1) ■ ■ 2. ■ ■ ■ ■ ■ (2) ■ ■ ■ 3. ■ ■ ■ ■ ■ ■ (2) ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ 4. ■ ■ ■ ■ ■ ■ (2) ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ 5. ■ ■ ■ ■ ■ ■ ■ (4) ■ ■ ■ ■ ■ ■ ■ ■ ■ (4) 6. ■ ■ ■ ■ ■ ■ ■ (4) ■ ■ ■ ■ ■ ■ ■ ■ ■ 7. ■ ■ ■ ■ ■ ■ (4) ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ */ static int style_x[7][4][4] = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 1, 1, 1, 1, 0, 1, 2, 3, 1, 1, 1, 1, 0, 1, 1, 2, 1, 0, 1, 0, 0, 1, 1, 2, 1, 0, 1, 0, 1, 2, 0, 1, 0, 0, 1, 1, 1, 2, 0, 1, 0, 0, 1, 1, 0, 1, 2, 0, 0, 1, 1, 1, 2, 0, 1, 2, 0, 0, 0, 1, 0, 1, 2, 2, 1, 1, 0, 1, 0, 0, 1, 2, 0, 1, 0, 0, 0, 1, 2, 1, 1, 0, 1, 1, 1, 0, 1, 2, 0, 0, 1, 0 }; static int style_y[7][4][4] = { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 2, 3, 1, 1, 1, 1, 0, 1, 2, 3, 0, 0, 1, 1, 0, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1, 2, 0, 0, 0, 1, 0, 0, 1, 2, 0, 1, 1, 1, 0, 1, 2, 2, 0, 0, 0, 1, 0, 1, 2, 2, 0, 1, 1, 1, 0, 0, 1, 2, 0, 0, 0, 1, 0, 1, 1, 2, 0, 1, 1, 1, 0, 1, 1, 2 }; static int my_style, direction, ypos, xpos, number; static int new_style, my_lines, delay, level, start_time, my_scores; typedef struct { char userid[IDLEN + 1]; int lines; int scores; } TOP_LIST; static TOP_LIST top_list[MAX_MAP_HEIGHT]; // --------------------------------------- // // function start // --------------------------------------- // static void move_map(y, x) int y, x; { move(y + 2, 2 * x); } static int top_cmp(a, b) TOP_LIST *a, *b; { int retval; retval = b->scores - a->scores; if (retval == 0) retval = b->lines - a->lines; return retval; } static void top_init() { int fd, i = 0; fd = open(FN_TETRIS_REC, O_RDONLY); if (fd >= 0) { memset(top_list, 0, sizeof(TOP_LIST) * MAX_MAP_HEIGHT); while (read(fd, &top_list[i], sizeof(TOP_LIST)) == sizeof(TOP_LIST)) i++; close(fd); } } static int save_top() { int fd, wflag, i; i = 0; wflag = 1; memset(top_list, 0, sizeof(TOP_LIST) * MAX_MAP_HEIGHT); if ((fd = open(FN_TETRIS_REC, O_RDWR | O_CREAT, 0600)) >= 0) { f_exlock(fd); while (read(fd, &top_list[i], sizeof(TOP_LIST)) == sizeof(TOP_LIST)) { if (!str_cmp(top_list[i].userid, cuser.userid)) { wflag = 0; if (top_list[i].scores < max_scores) { top_list[i].scores = max_scores; wflag = 2; } if (top_list[i].lines < max_lines) { top_list[i].lines = max_lines; wflag = 2; } } i++; } if (wflag == 1) { if (i < MAX_MAP_HEIGHT) { strcpy(top_list[i].userid, cuser.userid); top_list[i].scores = max_scores; top_list[i].lines = max_lines; i++; } else { if (top_list[i-1].scores < max_scores) { strcpy(top_list[i-1].userid, cuser.userid); top_list[i-1].scores = max_scores; top_list[i-1].lines = max_lines; } else wflag = 0; } } if (wflag) { xsort(top_list, i, sizeof(TOP_LIST), top_cmp); lseek(fd, 0, SEEK_SET); wflag = write(fd, top_list, sizeof(TOP_LIST) * i); ftruncate(fd, sizeof(TOP_LIST) * i); } f_unlock(fd); close(fd); } return wflag; } static void show_welcome() { move(2, 0); prints("欢迎光临 %s!\n\n", cuser.userid); prints("键盘设置如下: \n"); prints("左: '\033[1;32ma\033[m', '\033[1;32m←\033[m';\n"); prints("右: '\033[1;32md\033[m', '\033[1;32m→\033[m';\n"); prints("下: '\033[1;32ms\033[m', '\033[1;32m↓\033[m';\n"); prints("顺时针转动: '\033[1;32mk\033[m', <\033[1;32m回车\033[m>;"); prints("逆时针转动: '\033[1;32mh\033[m', '\033[1;32m↑\033[m'; 180度转动: ' \033[1;32mj\033[m';\n"); prints("快降: '<空格>', 暂停: <\033[1;32m ^S \033[m>.\n"); prints("退出: '\033[1;32m^C ^D\033[m', '\033[1;32m^D\033[m'.\n\n"); prints("每消 \033[1;33m30\033[m 行升一级, 消1行得1分、行3分、三行7分、四行 15分!\n"); prints("退出游戏后方能刷新排行榜!! 进入排行榜者有奖金拿喔~~^0^"); vmsg(NULL); } static void tetris_init(lines) int lines; { /* init screen */ game_times++; /* get the rand style and set other parameter */ start_time = time(0); srand(start_time); level = lines; lines = MAX_MAP_HEIGHT - lines; for (ypos = 0; ypos < MAX_MAP_HEIGHT; ypos++) { for (xpos = 1; xpos <= MAX_MAP_WIDTH; xpos++) { if (ypos >= lines) map[ypos][xpos] = rand() % 3; else map[ypos][xpos] = 0; } } new_style = rand() % 7; my_lines = my_scores = 0; delay = 40; do { delay *= 0.9; } while (lines++ < MAX_MAP_HEIGHT); } static void show_map() { int y, x; move_map(0, 0); for (y = 0; y <= MAX_MAP_HEIGHT; y++) { for(x = 0; x <= MAX_MAP_WIDTH + 1; x++) { if (map[y][x] == 8) outs("□"); else if (map[y][x]) outs(BLOCK_CHAR); else outs(" "); } if (y == 0) prints(" \033[1;33;44m == %s 俄罗斯方块高手榜 ==\033[m \n", str_site); else if(top_list[y-1].userid[0]) prints(" %2d %-14s %6d 分 %6d 条\n", y, top_list[y-1].userid, top_list[y-1].scores, top_list[y-1].lines); } } /* ypost, xpos, style, direction, flag */ static void show_block(y, x, k, d, f) int y, x, k, d, f; { if (d == -1) return; for (number = 0; number <= 3; number++) { move_map(y + style_y[k][d][number], x + style_x[k][d][number]); if (f) outs(BLOCK_CHAR); else outs(" "); } move(1, 0); } static void move_block() { if (last_ypos == ypos && last_xpos == xpos && last_style == my_style && las t_dir == direction) return; show_block(last_ypos, last_xpos, last_style, last_dir, 0); last_ypos = ypos; last_xpos = xpos; last_style = my_style; last_dir = direction; show_block(last_ypos, last_xpos, last_style, last_dir, 1); } /* ypost, xpos, style, direction */ static int crash(x, y, k, d) int x, y, k, d; { for (number = 0; number <= 3; number++) if (map[y + style_y[k][d][number]][x + style_x[k][d][number]]) return 1; return 0; } static void check_lines() { int y, x, s = 1; for (y = 0; y < MAX_MAP_HEIGHT; y++) { for (x = 1; x <= MAX_MAP_WIDTH; x++) { if (map[y][x] == 0) break; } if (x == (MAX_MAP_WIDTH + 1)) { int y2; s *= 2; /* 消去行上部全部下移! */ for (y2 = y; y2 > 0; y2--) { for (x = 1; x <= MAX_MAP_WIDTH; x++) map[y2][x] = map[y2-1][x]; } for (x = 1; x <= MAX_MAP_WIDTH; x++) map[0][x] = 0; if (++my_lines % 30 == 0) { delay *= 0.9; level ++; bell(); } } } s -= 1; if (s > 0) { s = s * (10 + level) / 10; my_scores += s; show_map(); move(b_lines, 0); clrtoeol(); prints("本局成绩: 总消去条数: \033[1;32m%d\033[m , 总得分 \033[1;32m%d\033[ m, 等级: \033[1;32m%d\033[m", my_lines, my_scores, level); } } static void arrived() { int y, x; for (number = 0; number <= 3; number++) { y = ypos + style_y[my_style][direction][number]; x = xpos + style_x[my_style][direction][number]; map[y][x] = my_style + 1; } check_lines(); last_dir = -1; } int p_tetris() { int ch, clock; clear(); utmp_mode(M_TETRIS); vs_bar("俄罗斯方块"); show_welcome(); game_times = max_lines = max_scores = 0; top_init(); start: last_dir = -1; ch = vans("从第几级别开始玩(0-9)? [0]"); if (ch < '0') ch = 0; else if (ch > '9') ch = 9; else ch = ch - 48; tetris_init(ch); move(1, 0); clrtobot(); refresh(); prints("第 \033[1;32m%d\033[m 局", game_times); show_map(); vmsg("游戏开始!"); move(b_lines, 0); clrtoeol(); prints("本局成绩: 总消去条数: \033[1;32m0\033[m, 总得分: \033[1;32m0\033[m , 等级: \033[1;32m%d\033[m", level); add_io(0, 0); while (1) { my_style = new_style; new_style = rand() % 7; direction = 0; xpos = 3; ypos = 0; move_map(0, MAX_MAP_WIDTH + 2); outs(" "); move_map(1, MAX_MAP_WIDTH + 2); outs(" "); show_block(0, MAX_MAP_WIDTH + 2, new_style, direction, new_style + 1); move_block(); if (crash(xpos, ypos, my_style, direction)) /* game over */ break; clock = times(0); while(1) { ch = vkey(); switch(ch) { case Ctrl('L') : break; case Ctrl('S') : add_io(0, 60); vmsg("游戏暂停!"); move(b_lines, 0); clrtoeol(); prints("本局成绩: 总消去条数: \033[1;32m%d\033[m, 总得分 \033[1;32m%d \033[m, 等级: \033[1;32m%d\033[m", my_lines, my_scores, level); add_io(0, 0); break; case 'A' : case 'a' : case KEY_LEFT : if (!crash(xpos - 1, ypos, my_style, direction)) { xpos --; move_block(); } break; case 'D' : case 'd' : case KEY_RIGHT : if (!crash(xpos + 1, ypos, my_style, direction)) { xpos++; move_block(); } break; case 'H' : case 'h' : case KEY_UP : if (!crash(xpos, ypos, my_style, (direction + 3) % 4)) { direction = (direction + 3) % 4; move_block(); } break; case 'K' : case 'k' : if (!crash(xpos, ypos, my_style, (direction + 1) % 4)) { direction = (direction + 1) % 4; move_block(); } break; case 'J' : case 'j' : if (!crash(xpos, ypos, my_style, (direction + 2) % 4)) { direction = (direction + 2) % 4; move_block(); } break; case 'S' : case 's' : case 'Z' : case 'z' : case KEY_DOWN : clock = times(0); if (crash(xpos, ypos + 1, my_style, direction)) { arrived(); ch = '#'; } else { ypos++; move_block(); } break; case ' ' : while (!crash(xpos, ypos + 1, my_style, direction)) ypos++; move_block(); arrived(); ch = '#'; break; default : break; } if (ch == Ctrl('C') || ch == Ctrl('D') || ch == '#') break; /* down auto */ if ((times(0) - clock > delay)) { clock = times(0); if (crash(xpos, ypos + 1, my_style, direction)) { arrived(); break; } else { ypos++; move_block(); } } } if (ch == Ctrl('C') || ch == Ctrl('D')) { break; } } add_io(0, 60); if (my_scores > max_scores) max_scores = my_scores; if (my_lines > max_lines) max_lines = my_lines; ch = vans("本局结束!您还要继续玩吗? [Y/n]"); if (ch != 'n' & ch != 'N') goto start; ch = save_top(); if (ch > 0) vmsg("游戏结束,您刷新了您的记录!"); else vmsg("游戏结束,您未能刷新高手榜!"); return 0; } -- ※ Origin: 明月水轩 <bbs.hightman.net> ◆ From: 10.14.61.151







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」






like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP