本文实例为大家分享了C++实现俄罗斯方块游戏代码,供大家参考,具体内容如下
1.block.c
#include <stdio.h>#include <termios.h>#include <unistd.h>#include <stdlib.h>#include <setjmp.h>#include <sys/time.h>#include <string.h>#include "block.h" //init for globlevoIDinit_for_globle(voID){ x = X / 2 - 2; // the first diamond appear postion flag_erase = 1; srand(getpID()); //srand num = rand() % 7; // random appear first diamond mode = rand() % 4; // random appear first diamond mode color = rand() % 7 + 41; // random first diamond color next_num = rand() % 7; next_mode = rand() % 4; save_color = rand() % 7 + 41; print_start_interface(); // print game start interface// print_score(); // print init score 0// print_level(); // print init level 1} //print start interfacevoIDprint_start_interface(voID){ int x,y; printf("[2J"); printf("[%d;%dH[32m分数:[0m",p_y + 10,p_x + 25); printf("[%d;%dH[32m等级:[0m",p_y + 14,p_x + 25); for (x = p_x,y = p_y; x <= 46; x++) printf("[%d;%dH[41m==[0m",y,x); for (x = p_x,y = p_y + 1; y <= 25; y++) printf("[%d;%dH[41m||[0m",x); for (x = p_x + 22,x); for (x = p_x + 36,x); for (x = p_x + 24,y = p_y + 8; x <= 44; x++) printf("[%d;%dH[41m--[0m",y = p_y + 21; x <= 46; x++) printf("[%d;%dH[41m==[0m",x); printf("[?25l"); fflush(stdout);} //erase last diamondsvoIDerase_last(voID){ int j,x1,y1,n; x1 = save_x + p_x + 2; for (j = 0,n = 0; j < 16; j++) { if (j / 4 >= shape[num][save_mode][16] && j % 4 == 0) { y1 = save_y + p_y + 1 + n; printf("[%d;%dH",x1); n++; } if (j / 4 >= shape[num][save_mode][16] && j % 4 >= shape[num][save_mode][17]) { if (shape[num][save_mode][j] == 0) { printf("[2C"); } if (shape[num][save_mode][j] == 1) { printf(" "); } } } fflush(stdout);} //print modes shapevoIDprint_mode_shape(voID){ int j,n; int left_flag = 0; if (flag_erase == 0) { erase_last(); } x1 = x + p_x + 2; for (j = 0,n = 0; j < 16; j++) { if (j / 4 >= shape[num][mode][16] && j % 4 == 0) { y1 = y + p_y + 1 + n; printf("[%d;%dH",x1); n++; } if (j / 4 >= shape[num][mode][16] && j % 4 >= shape[num][mode][17]) { if (shape[num][mode][j] == 0) { printf("[2C"); } if (shape[num][mode][j] == 1) { printf("[%dm[][0m",color); } } fflush(stdout); } printf("[0m"); fflush(stdout); save_x = x; save_y = y; save_mode = mode; save_row = 4 - shape[num][mode][16]; save_col = 4 - shape[num][mode][17]; flag_erase = 0; } //store diamonds to matrix by color to flagvoIDstore_flag_color(voID){ int i,a = 0,b = 0; for (i = 0; i < 16; i++) { if (i / 4 >= shape[num][mode][16] && i % 4 == 0) { a++; b = 0; } if (i / 4 >= shape[num][mode][16] && i % 4 >= shape[num][mode][17]) { if (shape[num][save_mode][i] == 0) { b = b + 2; } if (shape[num][save_mode][i] == 1) { matirx[save_y + a - 1][save_x + b] = color; b++; matirx[save_y + a - 1][save_x + b] = color; b++; } } }} //print the save matrixvoIDprint_save_matrix(voID){ int i,j,n = 0; for (i = 0; i < Y; i++) { printf("[%d;%dH",i + p_y + 1,p_x + 2); for (j = 0; j < X; j++) { if (matirx[i][j] != 0) { n = (n + 1) % 2; fprintf(stdout,"[%dm",matirx[i][j]); (n == 1) ? printf("[") : printf("]"); } if (matirx[i][j] == 0) { printf("[0m"); printf(" "); } fflush(stdout); } }} // change shapevoIDchange_shape(voID){ int i,n; for (i = 0; i < save_row; i++) { if (num == 6) { n = 4; } else { n = 0; } if (((x + n) >= X - save_col * 2 && save_col < save_row) || judge_by_color(x,(mode + 1) % 4) == 1) { return; } } mode = (mode + 1) % 4; fflush(stdout); print_mode_shape(); fflush(stdout);} //move rightvoIDmove_right(voID){ int i; if (x >= X - save_col * 2 || judge_by_color(x + 2,mode) == 1) { return; } x = x + 2; print_mode_shape(); fflush(stdout);} // move leftvoIDmove_left(voID){ int i; if (x <= 0 || judge_by_color(x - 2,mode) == 1) { return; } x = x - 2; print_mode_shape(); fflush(stdout);} // move downvoIDmove_down(){ y++; if (y >= Y - save_row + 1 || judge_by_color(x,mode) == 1) { store_flag_color(); game_over(); y = 0; save_row = 0; save_col = 0; x = X / 2 - 2; num = next_num; mode = next_mode; color = save_color; next_num = random() % 7; next_mode = random() % 4; save_color = random() % 7 + 41; print_next(); flag_erase = 1; destroy_line(); fflush(stdout); return; } print_mode_shape(); fflush(stdout);} voIDfall_down(){ while (1) { y++; if (y >= Y - save_row + 1 || judge_by_color(x,mode) == 1) { store_flag_color(); game_over(); y = 0; save_row = 0; save_col = 0; x = X / 2 - 2; num = next_num; mode = next_mode; color = save_color; next_num = rand() % 7; next_mode = rand() % 4; save_color = rand() % 7 + 41; print_next(); flag_erase = 1; destroy_line(); fflush(stdout); return; } print_mode_shape(); fflush(stdout); } } //erase next tip diamondvoIDerase_next(voID){ int i,n = 0; for (i = 0; i < 4; i++) { printf("[%d;%dH",p_y + 3 + n,p_x + X + 7); n++; for (j = 0; j < 4; j++) { printf(" "); } } printf("[30;4H[?25l"); fflush(stdout);} //print next tip diamondvoIDprint_next(voID){ int j,n = 0; erase_next(); for (j = 0; j < 16; j++) { if (j / 4 >= shape[next_num][next_mode][16] && j % 4 == 0) { printf("[%d;%dH",p_x + X + 7); n++; } if (j / 4 >= shape[next_num][next_mode][16] && j % 4 >= shape[next_num][next_mode][17]) { if (shape[next_num][next_mode] == 0) { printf("[2C"); } if (shape[next_num][next_mode][j] == 1) { printf("[%dm[][0m",save_color); } } }} //print scores infovoIDprint_score(voID){ printf("[%d;%dH[31m%d[0m",p_x + X + 10,score); fprintf(stdout,"[%d;0H",p_y + 20 + 2);} //print grades infovoIDprint_level(voID){ printf("[%d;%dH[31m%d[0m",level); fprintf(stdout,p_y + 20 + 2);} //destroy a line or lines voIDdestroy_line(voID){ int i,full; int a,b,c; for (i = 0; i < Y; i++) { full = 1; for (j = 0; j < X; j++) { if (matirx[i][j] == 0) { full = 0; } } if (full == 1) { for (a = 0; a < i; a++) { for (b = 0; b < X; b++) { matirx[i - a][b] = matirx[i - a - 1][b]; } } print_save_matrix(); score = score + 100; if (score % LEVEL_score == 0) { level = level + 1; if (level >= 9) level = 9; change_level(); print_level(); } print_score(); } }} //change level,change rate voIDchange_level(voID){ switch (level) { case 1: setitimer(ITIMER_REAL,&level_01,NulL); break; case 2: setitimer(ITIMER_REAL,&level_02,NulL); break; case 3: setitimer(ITIMER_REAL,&level_03,NulL); break; case 4: setitimer(ITIMER_REAL,&level_04,NulL); break; case 5: setitimer(ITIMER_REAL,&level_05,NulL); break; case 6: setitimer(ITIMER_REAL,&level_06,NulL); break; case 7: setitimer(ITIMER_REAL,&level_07,NulL); break; case 8: setitimer(ITIMER_REAL,&level_08,NulL); break; case 9: setitimer(ITIMER_REAL,&level_09,NulL); break; default: break; }} //by the color to judge whether went across or notintjudge_by_color(int x,int mode){ int i,b = 0; for (i = 0; i < 16; i++) { if (i / 4 >= shape[num][mode][16] && i % 4 == 0) { a++; b = 0; } if (i / 4 >= shape[num][mode][16] && i % 4 >= shape[num][mode][17]) { if (shape[num][mode][i] == 0) { b = b + 2; } if (shape[num][mode][i] == 1) { if (matirx[a + y - 1][b + x] != 0) { return 1; } else b = b + 2; } } }} //control the diamonds shape by the key voIDkey_control(voID){ int ch,flag = 1; struct termios save,raw; tcgetattr(0,&save); cfmakeraw(&raw); tcsetattr(0,&raw); if (setjmp(env) == 0) { while (flag) { ch = getchar(); if (ch == '\r') { fall_down(); } if (ch == '') { ch = getchar(); if (ch == '[') { ch = getchar(); switch (ch) { case 'A': change_shape(); break; case 'B': move_down(); break; case 'C': move_right(); break; case 'D': move_left(); break; } } } if (ch == 'q' || ch == 'Q') { flag = 0; } } printf("[%d;%dH[31m-----game interrupt exit!-----[0m",p_y + Y + 3,p_x); printf("[%d;0H[?25h",p_y + Y + 4); } tcsetattr(0,&save);} //reach the top line,the game is over voIDgame_over(voID){ int i; for (i = 0; i < X; i++) { if (matirx[1][i] != 0) { printf ("[31m[%d;%dH-------game over!--------[0m",p_x); printf("[0m[?25h[%d;0H",p_y + Y + 4); longjmp(env,2); } }}
2.block.h
#ifndef _BLOCK_H_#define _BLOCK_H_ #define p_x 10 //init postion row;#define p_y 5 //init postion col;#define X 20 // game_window_size#define Y 20#define LEVEL_score 500 // need scores to upgrade jmp_buf env;static int x,y; // current diamonds postionstatic int flag_erase; // erase flagstatic int num,mode,next_num,next_mode; // current and next diamondsstatic int save_row,save_col,save_x,save_y,save_mode;// save coordinate,save graphstatic int color,save_color,flag_color; // save the color of the next diamondsstatic int matirx[Y][X] = { 0 }; // save diamonds' matrixstatic int level = 1; // game levelsstatic int score = 0; // game scores typedef struct itimerval LEVEL;static LEVEL level_00 = { {0,0},{0,0} };static LEVEL level_01 = { {0,800000},{1,0} };static LEVEL level_02 = { {0,500000},500000} };static LEVEL level_03 = { {0,400000},300000} };static LEVEL level_04 = { {0,300000},300000} };static LEVEL level_05 = { {0,200000},300000} };static LEVEL level_06 = { {0,150000},300000} };static LEVEL level_07 = { {0,100000},300000} };static LEVEL level_08 = { {0,80000 },300000} };static LEVEL level_09 = { {0,60000 },300000} }; //three-dimensional for saving diamonds and diamonds' shape ://first-dimensional for kind of diamonds-shape//second-dimensional for alterable's mode//third-dimensional for reality value of row and colstatic const int shape[7][4][18] = { {{0,1,2,1},// {0,2},// [] [] [][][] [] {0,// [][][] [][] [] [][] {0,2}},// [] [] {{0,// {0,// [] [][] {0,// [] [] [][][] [] {0,// [][][] [][] [] [] {{0,// [][] [] {0,// [] [] [][][] [] {0,1}},// [][][] [] [] [][] {{0,// {0,// [] {0,// [][] [][] {0,// [] [][] {{0,// [] {0,// [][] [][] {0,// [] [][] {{0,// [][] {0,// [][] {{0,3},3,// [] [][][][] {0,// [] {0,0}} // [] }; voID init_for_globle(voID);voID print_start_interface(voID);voID print_mode_shape(voID);voID print_save_matrix(voID);voID change_shape(voID);voID move_left(voID);voID move_right(voID);voID move_down();voID fall_down();voID store_flag_color(voID);voID key_control(voID);voID erase_last(voID);voID destroy_line(voID);voID print_next(voID);voID erase_next(voID);voID change_level(voID);voID print_score(voID);voID print_level(voID);int judge_by_color(int x,int mode);voID game_over(voID); #endif
3.main.c
#include <stdio.h>#include <signal.h>#include <sys/time.h>#include <setjmp.h>#include "block.h" intmain(int argc,char **argv){ init_for_globle(); //init for globle print_mode_shape(); //print first diamond print_next(); //print next diamond setitimer(ITIMER_REAL,NulL); //init one leve ;interval 800ms signal(SIgalRM,move_down); //diamond down base on the interval time key_control(); //using zhe key to play games return 0;}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Linux下用C++实现俄罗斯方块全部内容,希望文章能够帮你解决Linux下用C++实现俄罗斯方块所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)