欢迎光临
我们一直在努力

基于Linux c framebuffer 俄罗斯方块游戏 多线程(扫描按键)

命令:gcc main.c -lncurses -lm -pthread

#include<stdio.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<unistd.h>
#include<math.h>
#include<ncurses.h>
#include<pthread.h>
#include<stdlib.h>
#define O_X 10
#define O_Y 300
/***********************************************************************************/
typedef unsigned int u32_t;
u32_t *p;
int Pause=0;
int sudo=40000;
int nowspeed=40000;
int xia_x,xia_y=427,last_y;
int gameover=0;
char key;
int check_move=0;
int nextbl=0;
int r_m=1,l_m=1,d_m=1,u_m=1;
int r_move=1,l_move=1,d_move=1;
int startgame=0;
int diffcult;
int play1_score=0;
int play2_score=0;
int old_score=0;
int put_begin=0;
int kill_put_score=0;
int live=1;
int line=0;
u32_t jbcolor[]={0xa000000,0x000000,0x000000,0x010000,0x000100,0x00000};
int now[4][4];
int next[4][4];
int xz[][16]={0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,
0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,
0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,
0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,
0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,
0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,
0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,
0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,
0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,
0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,
0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0};
int background_box[20][10];
/***********************************************************************************/
int kai[32]={0x00,0x00,0x7F,0xFC,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0xFF,0xFE,
0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x20,0x10,0x20,0x20,0x20,0xFF,0xFF};
int shi[32]={0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x40,0xFC,0x48,0x24,0x84,0x25,0xFE,0x24,0x82,
0x24,0x00,0x48,0xFC,0x28,0x84,0x10,0x84,0x28,0x84,0x44,0x84,0x80,0xFC,0xFF,0xFF};
int you[32]={0x02,0x10,0x21,0x10,0x11,0x10,0x17,0xBE,0x82,0x20,0x42,0x40,0x43,0xBC,0x12,0x84,
0x12,0x88,0x22,0x88,0xE2,0xBE,0x22,0x88,0x24,0x88,0x24,0x88,0x29,0xA8,0xFF,0xFF};
int xi[32]={0x00,0x20,0x00,0x28,0x00,0x24,0x7E,0x24,0x02,0x20,0x02,0x3E,0x25,0xE0,0x14,0x24,
0x08,0x24,0x08,0x28,0x14,0x28,0x12,0x10,0x22,0x32,0x40,0x4A,0x00,0x86,0xFF,0xFF};
/***********************************************************************************/
int ji[32]={0x10,0x20,0x12,0x20,0x22,0x20,0x23,0x24,0x4A,0xA8,0xF2,0x20,0x13,0xFC,0x22,0x20,
0x42,0x70,0xFA,0xA8,0x43,0x24,0x02,0x20,0x1A,0x20,0xE2,0x20,0x43,0xFE,0xFF,0xFF};
int xu[32]={0x10,0x20,0x10,0x20,0x21,0xFC,0x24,0x20,0x44,0x20,0xFB,0xFE,0x10,0x02,0x20,0x94,
0x40,0x50,0xFD,0x10,0x40,0x90,0x03,0xFE,0x1C,0x28,0xE0,0x44,0x40,0x82,0xFF,0xFF};
/***********************************************************************************/
int guan[32]={0x10,0x10,0x08,0x10,0x08,0x20,0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,
0xFF,0xFE,0x01,0x00,0x02,0x80,0x02,0x80,0x04,0x40,0x08,0x20,0x30,0x18,0xFF,0xFF};
int yu[32]={0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0xFF,0xFF};
/***********************************************************************************/
int bang[32]={0x08,0x00,0x08,0x7C,0xFF,0x48,0x08,0x50,0x7E,0x48,0x08,0x44,0xFF,0x54,0x10,0x48,
0x20,0x40,0x41,0x00,0xBF,0xF8,0x21,0x08,0x21,0x08,0x21,0x28,0x21,0x10,0xFF,0xFF};
int zhu[32]={0x00,0x20,0x7C,0x20,0x44,0x20,0x44,0x20,0x44,0xFC,0x7C,0x24,0x44,0x24,0x44,0x24,
0x7C,0x24,0x44,0x24,0x44,0x24,0x44,0x44,0x4E,0x44,0xF0,0x84,0x01,0x28,0xFF,0xFF};
/***********************************************************************************/
int tui[32]={0x03,0xF8,0x22,0x08,0x12,0x08,0x13,0xF8,0x02,0x08,0x02,0x08,0xF3,0xF8,0x12,0x44,
0x12,0x28,0x12,0x10,0x12,0x88,0x13,0x04,0x12,0x04,0x28,0x00,0x47,0xFE,0x00,0x00};
int chu[32]={0x01,0x00,0x01,0x00,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x01,0x08,
0x01,0x00,0x01,0x00,0x41,0x04,0x41,0x04,0x41,0x04,0x41,0x04,0x7F,0xFC,0x00,0x04};
/***********************************************************************************/
int nand[32]={0x00,0xA0,0x00,0x90,0xFC,0x80,0x05,0xFE,0x05,0x10,0x4B,0x10,0x2D,0xFC,0x29,0x10,
0x11,0x10,0x11,0xFC,0x29,0x10,0x25,0x10,0x45,0x10,0x81,0xFE,0x01,0x00,0x01,0x00};
int du[32]={0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E};
/***********************************************************************************/
int mo[32]={0x11,0x10,0x11,0x10,0x17,0xFC,0x11,0x10,0xFC,0x00,0x13,0xF8,0x32,0x08,0x3B,0xF8,
0x56,0x08,0x53,0xF8,0x90,0x40,0x17,0xFC,0x10,0xA0,0x11,0x10,0x12,0x08,0x14,0x06};
int mshi[32]={0x00,0x48,0x00,0x44,0x00,0x44,0x00,0x40,0xFF,0xFE,0x00,0x40,0x00,0x40,0x3E,0x40,
0x08,0x40,0x08,0x40,0x08,0x20,0x08,0x22,0x0F,0x12,0x78,0x0A,0x20,0x06,0x00,0x02};
/***********************************************************************************/
int dan[32]={0x10,0x10,0x08,0x20,0x04,0x40,0x3F,0xF8,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,
0x21,0x08,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00};
int ren[32]={0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x80,
0x04,0x40,0x04,0x40,0x08,0x20,0x08,0x20,0x10,0x10,0x20,0x08,0x40,0x04,0x80,0x02};
/***********************************************************************************/
int duo[32]={0x02,0x00,0x02,0x00,0x07,0xF0,0x08,0x20,0x38,0x40,0x04,0x80,0x03,0x40,0x0C,0x80,
0x71,0xF8,0x02,0x08,0x0C,0x10,0x32,0x20,0x01,0x40,0x01,0x80,0x0E,0x00,0x70,0x00};
int lian[32]={0x00,0x88,0xFC,0x48,0x48,0x50,0x48,0x00,0x79,0xFC,0x48,0x20,0x48,0x20,0x78,0x20,
0x4B,0xFE,0x48,0x20,0x4C,0x50,0x78,0x50,0xC8,0x88,0x08,0x88,0x09,0x04,0x0A,0x02};
int lji[32]={0x10,0x00,0x11,0xF0,0x11,0x10,0x11,0x10,0xFD,0x10,0x11,0x10,0x31,0x10,0x39,0x10,
0x55,0x10,0x55,0x10,0x91,0x10,0x11,0x12,0x11,0x12,0x12,0x12,0x12,0x0E,0x14,0x00};
/***********************************************************************************/
int jian[32]={0x20,0x40,0x3F,0x7E,0x48,0x90,0x85,0x08,0x10,0x00,0x0B,0xF8,0x20,0x08,0x27,0xC8,
0x24,0x48,0x24,0x48,0x27,0xC8,0x24,0x48,0x24,0x48,0x27,0xC8,0x20,0x08,0x20,0x18};
int jdan[32]={0x10,0x10,0x08,0x20,0x04,0x40,0x3F,0xF8,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,
0x21,0x08,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00};
int yib[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
int yban[32]={0x08,0x00,0x10,0x78,0x3E,0x48,0x22,0x48,0x32,0x48,0x2A,0x86,0x2A,0x00,0xFE,0xFC,
0x22,0x44,0x32,0x44,0x2A,0x28,0x2A,0x28,0x22,0x10,0x42,0x28,0x4A,0x44,0x85,0x82};
int kun[32]={0x00,0x00,0x7F,0xFC,0x41,0x04,0x41,0x04,0x41,0x04,0x5F,0xF4,0x41,0x04,0x43,0x84,
0x43,0x44,0x45,0x24,0x49,0x14,0x51,0x14,0x41,0x04,0x41,0x04,0x7F,0xFC,0x40,0x04};
/***********************************************************************************/
int number0_9[][16]={0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,
0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x7E,0x44,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,
0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00};
/***********************************************************************************/
int scorezi[][16]={0x00,0x00,0x00,0x3E,0x42,0x42,0x40,0x20,0x18,0x04,0x02,0x42,0x42,0x7C,0x00,0x00,
0x00,0x00,0x00,0x3E,0x42,0x42,0x80,0x80,0x80,0x80,0x80,0x42,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0xFC,0x42,0x42,0x42,0x7C,0x48,0x48,0x44,0x44,0x42,0xE3,0x00,0x00,
0x00,0x00,0x00,0xFC,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFC,0x00,0x00};
/***********************************************************************************/
int ga_ov[][16]={0x00,0x00,0x00,0x3C,0x44,0x44,0x80,0x80,0x80,0x8E,0x84,0x44,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0x10,0x10,0x18,0x28,0x28,0x24,0x3C,0x44,0x42,0x42,0xE7,0x00,0x00,
0x00,0x00,0x00,0xEE,0x6C,0x6C,0x6C,0x6C,0x54,0x54,0x54,0x54,0x54,0xD6,0x00,0x00,
0x00,0x00,0x00,0xFC,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFC,0x00,0x00,
0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00,0x00,
0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00,
0x00,0x00,0x00,0xFC,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFC,0x00,0x00,
0x00,0x00,0x00,0xFC,0x42,0x42,0x42,0x7C,0x48,0x48,0x44,0x44,0x42,0xE3,0x00,0x00};
/***********************************************************************************/
int lv[][16]={0x00,0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x42,0xFE,0x00,0x00,
0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00};
u32_t * dataset();
int checkmove_1();
int checkbianxing();
void nextxz();
void tiange();
void draw_box(int o_x,int o_y,int len,int wide,u32_t color,int jianbian);
void xiezi(int o_x,int o_y,u32_t color,int size,int ez,int zige[32]);
void hua(int x,int y,u32_t color,int xing[4][4]);
void copy(int xing[][16]);
void draw_frame();
void msys();
void xia();
void gameovers();
void xiao();
void draw_grid();
void checkkey();
void check_edge();
void keyscan();
void bianxing();
void welcome();
void check_buton();
void put_score();
void time_up();
/***********************************************************************************/
int main(void)
{
int c;
dataset();
initscr();
noecho();
cbreak();
keypad(stdscr,TRUE);
while(1)
{
welcome();
if(gameover==1)
getchar();
draw_frame();
xia();
}
endwin();
return 0;
}
/***********************************************************************************/
void welcome()
{
int ret,i,j;
pthread_t id1;
gameover=0;
play1_score=0;
kill_put_score=0;
for(i=0;i<16;i++)
for(j=0;j<10;j++)
background_box[i][j]=0;
draw_box(0,0,740,1376,0x000000,1);
ret=pthread_create(&id1,NULL,(void *)check_buton,NULL);
if(ret!=0)
printf("error!\n");
while(startgame==0)
{
xiezi(200,532,0xffffff,2,32,kai);
xiezi(200,564,0xffffff,2,32,shi);
xiezi(200,596,0xffffff,2,32,you);
xiezi(200,628,0xffffff,2,32,xi);

xiezi(250,532,0xffffff,2,32,ji);
xiezi(250,564,0xffffff,2,32,xu);
xiezi(250,596,0xffffff,2,32,you);
xiezi(250,628,0xffffff,2,32,xi);

xiezi(300,532,0xffffff,2,32,you);
xiezi(300,564,0xffffff,2,32,xi);
xiezi(300,596,0xffffff,2,32,mo);
xiezi(300,628,0xffffff,2,32,mshi);

xiezi(350,532,0xffffff,2,32,you);
xiezi(350,564,0xffffff,2,32,xi);
xiezi(350,596,0xffffff,2,32,nand);
xiezi(350,628,0xffffff,2,32,du);

xiezi(400,532,0xffffff,2,32,you);
xiezi(400,564,0xffffff,2,32,xi);
xiezi(400,596,0xffffff,2,32,guan);
xiezi(400,628,0xffffff,2,32,yu);

xiezi(450,532,0xffffff,2,32,you);
xiezi(450,564,0xffffff,2,32,xi);
xiezi(450,596,0xffffff,2,32,bang);
xiezi(450,628,0xffffff,2,32,zhu);

xiezi(500,532,0xffffff,2,32,tui);
xiezi(500,564,0xffffff,2,32,chu);
xiezi(500,596,0xffffff,2,32,you);
xiezi(500,628,0xffffff,2,32,xi);
}
}
/***********************************************************************************/
void check_buton()
{
char key;
int i,j,option=0;
int ox=195,oy=521,len=40,wide=150;
while(startgame==0)
{
draw_box(ox-2,oy-2,len+4,wide+4,0xc00000,0);
draw_box(ox,oy,len,wide,0xff0000,0);
key=getchar();
if(key=='w')
{
ox-=50;
if(ox<195)
{
ox=495;
draw_box(195,oy,len,wide,0x000000,0);
draw_box(0,0,740,1376,0x000000,1);
}
draw_box(ox+50,oy,len,wide,0x000000,0);
draw_box(0,0,740,1376,0x000000,1);
draw_box(ox-2,oy-2,len+4,wide+4,0xc00000,0);
draw_box(ox,oy,len,wide,0xff0000,0);
}
if(key=='s')
{
ox+=50;
if(ox>495)
{
ox=195;
draw_box(495,oy,len,wide,0x000000,0);
draw_box(0,0,740,1376,0x000000,1);
}
draw_box(ox-50,oy,len,wide,0x000000,0);
draw_box(0,0,740,1376,0x000000,1);
draw_box(ox-2,oy-2,len+4,wide+4,0xc00000,0);
draw_box(ox,oy,len,wide,0xff0000,0);
}
if(key=='\r')
{
if(ox==195)
startgame=1;
if(ox==495)
{
startgame=1;
gameover=1;
endwin();
draw_box(0,0,740,1376,0x000000,0);
exit(0);
}
if(ox==295)
{
option=0;
draw_box(ox-2,oy-2,len+4,wide+4,0x900000,0);
draw_box(ox,oy,len,wide,0xa00000,0);
draw_box(ox-2,oy+wide,100,wide+15,0xa00000,0);
draw_box(294,675,len+4,wide+4,0xc00000,0);
draw_box(296,677,len,wide,0xff0000,0);
xiezi(300,690,0xffffff,2,32,dan);
xiezi(300,722,0xffffff,2,32,ren);
xiezi(300,754,0xffffff,2,32,mo);
xiezi(300,786,0xffffff,2,32,mshi);
xiezi(350,690,0xffffff,2,32,duo);
xiezi(350,722,0xffffff,2,32,ren);
xiezi(350,754,0xffffff,2,32,lian);
xiezi(350,786,0xffffff,2,32,lji);
while((key=getchar())!='\r')
{
if(key=='w'||key=='s')
{
option++;
option=option%2;
if(option==1)
{
draw_box(ox-2,oy+wide,100,wide+15,0xa00000,0);
draw_box(344,675,len+4,wide+4,0xc00000,0);
draw_box(346,677,len,wide,0xff0000,0);
xiezi(300,690,0xffffff,2,32,dan);
xiezi(300,722,0xffffff,2,32,ren);
xiezi(300,754,0xffffff,2,32,mo);
xiezi(300,786,0xffffff,2,32,mshi);
xiezi(350,690,0xffffff,2,32,duo);
xiezi(350,722,0xffffff,2,32,ren);
xiezi(350,754,0xffffff,2,32,lian);
xiezi(350,786,0xffffff,2,32,lji);
}
if(option==0)
{
draw_box(ox-2,oy+wide,100,wide+15,0xa00000,0);
draw_box(294,675,len+4,wide+4,0xc00000,0);
draw_box(296,677,len,wide,0xff0000,0);
xiezi(300,690,0xffffff,2,32,dan);
xiezi(300,722,0xffffff,2,32,ren);
xiezi(300,754,0xffffff,2,32,mo);
xiezi(300,786,0xffffff,2,32,mshi);
xiezi(350,690,0xffffff,2,32,duo);
xiezi(350,722,0xffffff,2,32,ren);
xiezi(350,754,0xffffff,2,32,lian);
xiezi(350,786,0xffffff,2,32,lji);
}
}
}
draw_box(0,0,740,1376,0x000000,1);
}
if(ox==345)
{
diffcult=0;
draw_box(ox-2,oy-2,len+4,wide+4,0x900000,0);
draw_box(ox,oy,len,wide,0xa00000,0);
draw_box(ox-2,oy+wide,150,wide+15,0xa00000,0);
draw_box(344,675,len+4,wide+4,0xc00000,0);
draw_box(346,677,len,wide,0xff0000,0);
xiezi(350,690,0xffffff,2,32,jian);
xiezi(350,722,0xffffff,2,32,jdan);
xiezi(400,690,0xffffff,2,32,yib);
xiezi(400,722,0xffffff,2,32,yban);
xiezi(450,690,0xffffff,2,32,kun);
xiezi(450,722,0xffffff,2,32,nand);
while((key=getchar())!='\r')
{
if(key=='w'||key=='s')
{
if(key=='s')
diffcult++;
else diffcult--;
if(diffcult==-1)
diffcult=2;
diffcult=diffcult%3;
if(diffcult==1)
{
draw_box(ox-2,oy+wide,150,wide+15,0xa00000,0);
draw_box(394,675,len+4,wide+4,0xc00000,0);
draw_box(396,677,len,wide,0xff0000,0);
xiezi(350,690,0xffffff,2,32,jian);
xiezi(350,722,0xffffff,2,32,jdan);
xiezi(400,690,0xffffff,2,32,yib);
xiezi(400,722,0xffffff,2,32,yban);
xiezi(450,690,0xffffff,2,32,kun);
xiezi(450,722,0xffffff,2,32,nand);
}
if(diffcult==0)
{
draw_box(ox-2,oy+wide,150,wide+15,0xa00000,0);
draw_box(344,675,len+4,wide+4,0xc00000,0);
draw_box(346,677,len,wide,0xff0000,0);
xiezi(350,690,0xffffff,2,32,jian);
xiezi(350,722,0xffffff,2,32,jdan);
xiezi(400,690,0xffffff,2,32,yib);
xiezi(400,722,0xffffff,2,32,yban);
xiezi(450,690,0xffffff,2,32,kun);
xiezi(450,722,0xffffff,2,32,nand);
}
if(diffcult==2)
{
draw_box(ox-2,oy+wide,150,wide+15,0xa00000,0);
draw_box(444,675,len+4,wide+4,0xc00000,0);
draw_box(446,677,len,wide,0xff0000,0);
xiezi(350,690,0xffffff,2,32,jian);
xiezi(350,722,0xffffff,2,32,jdan);
xiezi(400,690,0xffffff,2,32,yib);
xiezi(400,722,0xffffff,2,32,yban);
xiezi(450,690,0xffffff,2,32,kun);
xiezi(450,722,0xffffff,2,32,nand);
}
}
}
draw_box(0,0,740,1376,0x000000,1);
}
}
}
}
/***********************************************************************************/
u32_t * dataset()
{
int fd;
int i,j,x;
fd=open("/dev/fb0",O_RDWR);
p=(u32_t *)mmap(NULL, 1376 * 768 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,fd,0);
if(p == MAP_FAILED)
perror("mmap error");
srand(time(NULL));
x=rand()%19;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
now[i][j]=xz[x][i*4+j];
}
check_edge();
}
/***********************************************************************************/
void draw_box(int o_x,int o_y,int len,int wide,u32_t color,int jianbian)
{
int i,j;
for(i=1376*o_x;i<1376*(o_x+len);i+=1376)
{
for(j=o_y;j<o_y+wide;j++)
p[i+j]=color;
if(jianbian==1)
color+=jbcolor[i%5];
}
}
/***********************************************************************************/
void draw_frame()
{
int i,lives=live;
draw_box(0,0,740,1376,0x000000,1);
draw_box(O_X-10,O_Y-10,720,539,0x00f000,0);
draw_box(O_X-7,O_Y-7,714,533,0x009000,0);
draw_box(O_X-2,O_Y-2,704,523,0xffffff,0);
draw_box(O_X-7,652,714,6,0x009000,0);
draw_box(O_X-10,658,720,3,0x00f000,0);
draw_box(O_X-7,661,714,6,0x009000,0);
draw_box(O_X,O_Y,700,350,0x2f0000,0);
draw_box(O_X,669,700,150,0x2f0000,0);

draw_box(188,667,17,153,0xffffff,0);
draw_box(190,665,13,160,0x009000,0);
draw_box(195,661,3,165,0x00f000,0);

draw_box(248,667,17,153,0xffffff,0);
draw_box(250,665,13,160,0x009000,0);
draw_box(255,661,3,165,0x00f000,0);

draw_box(368,667,17,153,0xffffff,0);
draw_box(370,665,13,160,0x009000,0);
draw_box(375,661,3,165,0x00f000,0);

draw_box(O_X-10,O_Y-30,720,20,0x00f000,0);
draw_box(O_X-5,O_Y-25,710,10,0x4d4dff,0);

draw_grid(p);

if(diffcult==2)
{
xiezi(210,700,0xffffff,2,32,kun);
xiezi(210,762,0xffffff,2,32,nand);
}
if(diffcult==0)
{
xiezi(210,700,0xffffff,2,32,jian);
xiezi(210,762,0xffffff,2,32,jdan);
}
if(diffcult==1)
{
xiezi(210,700,0xffffff,2,32,yib);
xiezi(210,762,0xffffff,2,32,yban);
}

for(i=0;i<5;i++)
xiezi(280,705+i*16,0xff0000,2,16,scorezi[i]);
for(i=0;i<2;i++)
xiezi(390,710+i*16,0xff0000,2,16,lv[i]);
i=0;
draw_box(390,744,32,70,0x2f0000,0);
while(lives!=0)
{
i++;
xiezi(390,780-i*16,0xffffff,2,16,number0_9[lives%10]);
lives/=10;
}

}
/***********************************************************************************/
void msys()
{
int i,j;
for(i=0;i<10000;i++)
for(j=0;j<sudo;j++)
;
}
/***********************************************************************************/
void draw_grid()
{
int i;
for(i=O_Y;i<O_Y+350;i+=35)
draw_box(O_X,i,700,1,0x00500f,0);
for(i=O_X;i<O_X+700;i+=35)
draw_box(i,O_Y,1,350,0x00500f,0);

}
/***********************************************************************************/
void hua(int x,int y,u32_t color,int xing[4][4])
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(xing[i][j]==1)
draw_box(x+i*35,y+j*35,35,35,color,0);
}
/***********************************************************************************/
void bianxing()
{
int i,j,temp[4][4],ok;
int now_x,now_y;
ok=1;
now_x=(xia_x-O_X)/35;
now_y=(xia_y-O_Y)/35;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
temp[j][i]=now[i][3-j];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(temp[i][j]==1)
{
if(background_box[now_x+i][now_y+j]==1)
ok=0;
}
}
if(ok==1)
{
hua(xia_x,xia_y,0x2f0000,now);
for(i=0;i<4 ;i++)
for(j=0;j<4;j++)
now[i][j]=temp[i][j];
hua(xia_x,xia_y,0xff0000,now);
draw_grid();
check_edge();
}
}
/***********************************************************************************/
void copy(int xing[][16])
{
int i,j,x;
nextbl++;
srand(time(NULL));
x=rand()%19;
if(nextbl>=2)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
now[i][j]=next[i][j];
}
nextbl=2;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
next[i][j]=xing[x][i*4+j];
}

check_edge();
}
/***********************************************************************************/
void nextxz()
{
int i,j,ox=20,oy=670;
int r_move=0,l_move=0;
for(i=0;i<4;i++)
{
if(next[i][0]==1)
r_move=1;
if(next[i][3]==1)
l_move=1;
}
if(r_move==0&&l_move==1)
oy=660;
if(l_move==0&&r_move==1)
oy=690;
draw_box(ox,670,160,145,0x2f0000,0);
hua(ox,oy,0x00ffcc,next);
}
/***********************************************************************************/
void xia()
{
int y,c,i,j;
pthread_t id,id2,id3;
int ret;
ret=pthread_create(&id,NULL,(void *)checkkey,NULL);
if(ret!=0)
printf("error!\n");
ret=pthread_create(&id2,NULL,(void *)put_score,NULL);
if(ret!=0)
printf("error!\n");
ret=pthread_create(&id3,NULL,(void *)time_up,NULL);
if(ret!=0)
printf("error!\n");
while(gameover==0)
{
copy(xz);
nextxz();
xia_y=405;
sudo=nowspeed;
printf("\n");
for(last_y=xia_x=O_X-u_m*35 ; xia_x<=570+(3-d_m)*35 ; last_y=xia_x,xia_x+=35)
{
checkmove_1();
if(Pause==0&&d_move==1)
{
y=xia_y;
hua(xia_x,xia_y,0xff0000,now);
hua(last_y,y,0x2f0000,now);
hua(xia_x,xia_y,0xff0000,now);
draw_grid(p);
msys();
}
if(d_move==0&&xia_x<=17)
gameover=1;
if(d_move==0)break;
while(Pause==1)
;
}
tiange();
xiao();
}
gameovers();
}
/***********************************************************************************/
void checkkey()
{
int last_y,y;
while(startgame==1)
{
key=getchar();
sudo=nowspeed;
checkmove_1();
if(key=='a'&&Pause==0)
if(xia_y>300-(l_m*35)&&l_move==1)
{
xia_y=xia_y-35;
hua(xia_x,xia_y+35,0x2f0000,now);
hua(xia_x,xia_y,0xff0000,now);
draw_grid();
}
if(key=='d'&&Pause==0&&r_move==1)
if(xia_y<510+(3-r_m)*35)
{
xia_y=xia_y+35;
hua(xia_x,xia_y-35,0x2f0000,now);
hua(xia_x,xia_y,0xff0000,now);
draw_grid();
}
if(key=='w'&&Pause==0)
if(checkbianxing()==1)
bianxing();
if(key=='u')
Pause=!Pause;
if(key=='s'&&d_move==1)
{
sudo=2000;
}
key=' ';
}
}
/***********************************************************************************/
void check_edge()
{
int i,j;
r_m=1,l_m=1,d_m=1,u_m=1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(now[i][j]==1)
{
if(i<u_m)
u_m=i;
if(i>d_m)
d_m=i;
if(j>r_m)
r_m=j;
if(j<l_m)
l_m=j;
}
}
/***********************************************************************************/
int checkbianxing()
{
int i,j,temp[4][4];
int r_m1=1,l_m1=1,d_m1=1,u_m1=1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
temp[j][i]=now[i][3-j];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(temp[i][j]==1)
{
if(i<u_m1)
u_m1=i;
if(i>d_m1)
d_m1=i;
if(j>r_m1)
r_m1=j;
if(j<l_m1)
l_m1=j;
}
}
if(xia_y+l_m1*35<O_Y || xia_y-(3-r_m1)*35>510 || xia_x+u_m1*35<O_X || xia_x-(3-d_m1)*35>570)
return 0;
else return 1;
}
/***********************************************************************************/
int checkmove_1()
{
int now_x,now_y;
int i,j,t;
int left[4],down[4],right[4];
now_x=(xia_x-O_X)/35;
now_y=(xia_y-O_Y)/35;
l_move=1;
r_move=1;
d_move=1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(now[i][j]==1)
{
left[i]=j;
break;
}
left[i]=8;
}
for(i=0;i<4;i++)
{
if(background_box[now_x+i][now_y+left[i]-1]==1&&now_x+i>=0&&left[i]!=8)
{
l_move=0;
}
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(now[3-j][i]==1)
{
down[i]=3-j;
break;
}
down[i]=8;
}
for(i=0;i<4;i++)
{
if(background_box[now_x+down[i]][now_y+i]==1&&down[i]!=8)
{
d_move=0;
}
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(now[i][3-j]==1)
{
right[i]=3-j;
break;
}
right[i]=8;
}
for(i=0;i<4;i++)
{
if(background_box[now_x+i][now_y+right[i]+1]==1&&right[i]!=8)
{
r_move=0;
}
}
}
/***********************************************************************************/
void tiange()
{
int now_x,now_y;
int i,j;
now_x=(xia_x-O_X)/35;
now_y=(xia_y-O_Y)/35;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(now[i][j]==1)
{
background_box[now_x+i-1][now_y+j]=now[i][j];
}
for(i=0;i<20;i++)
for(j=0;j<10;j++)
if(background_box[i][j]==1)
{
draw_box(O_X+i*35,O_Y+j*35,35,35,0x00ffff,0);
}
}
/***********************************************************************************/
void xiao()
{
int i,j,x,y,score=0,lives=live;
old_score=play1_score;
play1_score+=10;
for(i=0;i<20;i++)
{
for(j=0;j<10;j++)
{
if(background_box[i][j]==0)
break;
if(j==9)
{
score+=10;
line+=1;
for(x=0;x<350;x++)
{
draw_box(O_X+i*35,O_Y+x,35,1,0x2f0000,1);
for(y=0;y<400000;y++)
;
}
for(y=i;y>0;y--)
{
for(x=0;x<10;x++)
background_box[y][x]=background_box[y-1][x];
}
}
}
}
score+=score/5;
play1_score+=score;
i=0;
draw_box(390,744,32,70,0x2f0000,0);
while(lives!=0)
{
i++;
xiezi(390,780-i*16,0xffffff,2,16,number0_9[lives%10]);
lives/=10;
}
if(play1_score>(live*100)*live)
{
if(diffcult==2)
nowspeed=nowspeed-3000-700*live;
if(diffcult==1)
nowspeed=nowspeed-2000-600*live;
if(diffcult==0)
nowspeed=nowspeed-1000-500*live;

live+=1;
lives=live;
}
put_begin=1;
for(i=0;i<20;i++)
for(j=0;j<10;j++)
if(background_box[i][j]==1)
draw_box(O_X+i*35,O_Y+j*35,35,35,0x00ffff,0);
else draw_box(O_X+i*35,O_Y+j*35,35,35,0x2f0000,0);
draw_grid();
}
/***********************************************************************************/
void put_score()
{
int score,mark,y=0,i,bit,s;
while(kill_put_score!=1)
{
while(put_begin==1)
{
bit=0;
mark=score=old_score;
s=play1_score;
while(s!=0)
{
s/=10;
bit++;
}
while(mark<=play1_score)
{
draw_box(320,670,35,145,0x2f0000,0);
score=mark;
y=0;
while(score!=0)
{
xiezi(320,780+y-(5-bit)*16,0x000fff,2,16,number0_9[score%10]);
score=score/10;
y=y-16;
}
mark++;
for(i=0;i<10000000;i++)
;
}
put_begin=0;
}
}
}
/***********************************************************************************/
void gameovers()
{
int i,j,x,y,ox=337;
u32_t color=0xffffff;
char key;
for(i=0;i<20;i++)
for(j=0;j<10;j++)
{
draw_box(O_X+i*35,O_Y+j*35,35,35,0x2f0000,0);
draw_box(O_X+(19-i)*35,O_Y+(9-j)*35,35,35,0x2f0000,0);
for(x=0;x<1000;x++)
for(y=0;y<1000;y++)
;
}
kill_put_score=1;
startgame=0;
while(1)
{
ox=327;
color+=11;
for(i=0;i<8;i++)
{
if(i==4)
ox+=35;
xiezi(100,ox+i*35,color,3,16,ga_ov[i]);
}
}
}
/***********************************************************************************/
void xiezi(int o_x,int o_y,u32_t color,int size,int ez,int zige[32])
{
int i,j,binary,x,y;
x=0;
y=0;
for(i=0;i<ez;i++)
{
for(j=0;j<8;j++)
{
binary=( zige[i]>>7-j ) & 0x01;
if(binary==1)
draw_box(o_x+x*size,o_y+y*size,size,size,color,0);
y++;
if(y==ez/2)
{
x++;
y=0;
}
}
}
}
/***********************************************************************************/
void time_up()
{
int i,j;
while(gameover==0&&Pause==0)
{
for(i=0;i<=710;i++)
{
draw_box(O_X-5,O_Y-25,i,10,0x2f0000,0);
for(j=nowspeed*500;j>0;j--)
;
while(gameover==1||Pause==1)
{
;
}

}
draw_box(O_X-5,O_Y-25,710,10,0x4d4dff,0);
for(i=0;i<19;i++)
for(j=0;j<10;j++)
{
background_box[i][j]=background_box[i+1][j];
}
for(i=0;i<10;i++)
{
j=rand()%2;
background_box[19][i]=j;
}
for(i=0;i<20;i++)
for(j=0;j<10;j++)
if(background_box[i][j]==1)
draw_box(O_X+i*35,O_Y+j*35,35,35,0x00ffff,0);
else draw_box(O_X+i*35,O_Y+j*35,35,35,0x2f0000,0);
draw_grid();
}

}

赞(1) 打赏
未经允许不得转载:huangea的博客 » 基于Linux c framebuffer 俄罗斯方块游戏 多线程(扫描按键)

评论 3

  1. #1

    初来乍到,支持下

    银基网11年前 (2013-12-17)回复
  2. #2

    网站不错,支持一下哦。

    冷笑话大全10年前 (2014-12-03)回复
  3. #3

    Hi HI

    TimmyMix7年前 (2017-09-23)回复

有趣的网站

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册