最近比较无聊,用C语言写了个俄罗斯方块,游戏效果如下:
代码如下,后续有时间再进行改进。。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string.h>
#include <termios.h>
#include <pthread.h>
#define MAP_H 21
#define MAP_L 10
#define FALL_SPEED 420000 //MICROSECOND
#define FASTER_SPEED 5000 //MICROSECOND
#define FLASH_TIME 130000
const char no_block = '.';
const char block_body = '#';
const char block_flash = '*';
int key;
int speed;
int block_type_count;
int over=0;
int next_block=0;
int point=0;
int has_bomb=0;
int block_attr_changing_flag=0;
char GameField[MAP_H][MAP_L];
static struct termios stored_settings;
pthread_t id;
typedef struct Block
{
char block_form[4][4];
int type;
int x,y;
} s_Block;
s_Block Block;
s_Block Block_Next;
enum block_type
{
Square=0,
Strip=1,
JShape,
LShape,
TShape,
ZShape,
SShape,
Bomb,
};
void Initial();
void CreateBlock();
void ShowGame();
void PrintMap();
void CheckPoints();
void KeyDetectThread(void);
void GetButton();
void move_down();
void move_left();
void move_right();
void move_up();
void move_down_faster();
void FillBlockIntoMap();
void RmBlockFromMap();
int BlockCollide(s_Block *block);
void BlockRotate(s_Block *block);
void BlockExplode(int x,int y);
void set_keypress(void);
void reset_keypress(void);
int main(int argc, char *argv[])
{
Initial();
ShowGame();
return 0;
}
void Initial()
{
int ret;
int key;
memset(GameField, no_block, sizeof(GameField));
set_keypress();
speed = FALL_SPEED;
point = 0;
over = 0;
has_bomb = 0;
system("clear");
printf("\nYou wanna get bomb block randomly? (y/n)\n ");
key=getchar();
if(key=='y')
{
block_type_count=8;
}else
block_type_count=7;
system("clear");
CreateBlock();
Block = Block_Next;
CreateBlock();
PrintMap();
getchar();
GetButton();
ret=pthread_create(&id,NULL,(void *)KeyDetectThread,NULL);
if(ret)
{
printf("Create thread error!\n");
sleep(2);
exit(0);
}
}
void CreateBlock()
{
int b_type;