mywindows.h
#ifndef MYWINDOWS_H_INCLUDED
#define MYWINDOWS_H_INCLUDED
#include <windows.h>
void initHandle();
void setColor(int color);
void setPos(int x,int y);
void hideCursor();
#endif
mywindows.c
#include "mywindows.h"
HANDLE handle;
void initHandle()
{
handle =GetStdHandle(STD_OUTPUT_HANDLE);
hideCursor();
}
void setColor(int color)
{
SetConsoleTextAttribute(handle ,color);
}
void setPos(int x,int y)
{
COORD coord ={x*2,y};
SetConsoleCursorPosition(handle,coord);
}
void hideCursor()
{
CONSOLE_CURSOR_INFO info;
info.bVisible =FALSE;
info.dwSize = 1;
SetConsoleCursorInfo(handle,&info);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "game.h"
int main()
{
SetConsoleTitle("Tetris----Made by Big Data TSH");
gameInit();
return 0;
}
game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <stdio.h>
#include <time.h>
typedef struct{
int x,y,shape,status,color;
}BLOCK;
void windowPrint(int x,int y);
void gameInit();
void printInfo();
void printCheat();
void printGradeLevel(int num);
void gameTime(clock_t star_time);
void printBlock(int x,int y,int shape,int status,int color);
void deleteBlock(int x,int y,int shape,int status);
void startBlock();
void nextBlock();
void copyBlock();
int downBlock();
void leftBlock();
void rightBlock();
void tranBlock();
void tranShape();
void tranColor();
void changeLevel(int grade);
void changeGrade();
void bottomBlock(int time3,int time4);
int crash(int x,int y,int shape,int status);
void save();
void updataGame();
void pause();
void downerBlock();
void lineClear();
void lineDown(int line);
void printOver();
void printFinish();
void againGame();
void printStart(int x,int y);
void deleteStart(int x,int y);
void printAnimation();
void closeMusic();
void openMusic();
#endif
data.h
#ifndef DATA_H_INCLUDED
#define DATA_H_INCLUDED
int windowShape[25][26]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0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
{
{{1,1,1,0},{0,1,0,0},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{1,1,0,0},{0,1,0,0,},{0,0,0,0}},
{{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
{{1,0,0,0},{1,1,0,0},{1,0,0,0,},{0,0,0,0}}
},
{
{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
}
};
#endif
game.c
#include "game.h"
#include "data.h"
#include "mywindows.h"
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
int grade = 0;
int level = 1;
int maxgrade = 0;
int model = 0;
BLOCK cur_block;
BLOCK next_block;
void windowPrint(int x,int y)
{
int i,j;
for(i=0;i<25;i++)
{
for(j=0;j<26;j++)
{
if(windowShape[i][j] == 1)
{
setColor(0xb0);
setPos(x+j,y+i);
printf("%2s","");
}
}
}
}
void printInfo()
{
setColor(0x0b);
setPos(32,9);
printf("操作规则:");
setPos(32,10);
printf("按A或a左移");
setPos(32,11);
printf("按D或d左移");
setPos(32,12);
printf("按S或s下移");
setPos(32,13);
printf("按W或w变形");
setPos(32,14);
printf("按回车直接下落");
setPos(32,15);
printf("按空格暂停");
}
void printGradeLevel(int num)
{
switch(num)
{
case 0:break;
case 1:grade += 10 ;break;
case 2:grade += 30 ;break;
case 3:grade += 60 ;break;
case 4:grade += 100 ;break;
}
changeLevel(grade);
setColor(0x0e);
setPos(32,19);
printf("等级:%d",level);
setPos(32,20);
printf("得分:%d",grade);
setPos(32,21);
if(grade>maxgrade)
maxgrade = grade;
else
maxgrade+=0;
printf("最高得分:%d",maxgrade);
}
void printCheat()
{
setColor(0x0b);
setPos(1,3);
printf("■■■■■■■■■■■■■\n");
printf(" ■ ■\n");
printf(" ■ ■\n");
printf(" ■ ■\n");
printf(" ■■■■■■■■■■■■■");
setPos(2,4);
printf("改变方块形状:Q q");
setPos(2,5);
printf("改变方块颜色:E e");
setPos(2,6);
printf("改变分数等级速度:T t");
}
void gameTime(clock_t start_time)
{
setColor(0x0b);
setPos(2,2);
printf("游戏已运行的时间:%ld s",(clock()-start_time)/CLOCKS_PER_SEC);
}
void printBlock(int x,int y,int shape,int status,int color)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(block[shape][status][i][j] == 1)
{
setColor(color);
setPos(x+j,y+i);
printf("■");
}
}
}
}
void deleteBlock(int x,int y,int shape,int status)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(block[shape][status][i][j] == 1)
{
setPos(x+j,y+i);
printf(" ");
}
}
}
}
void startBlock()
{
srand((unsigned)time(NULL));
cur_block.x = 22;
cur_block.y = 1;
cur_block.shape = rand()%7;
cur_block.status = rand()%4;
cur_block.color = rand()%0x10;
if(cur_block.color == 0x00)
{
cur_block.color = 0x0f;
}
printBlock(cur_block.x,cur_block.y,cur_block.shape,
cur_block.status,cur_block.color);
}
void nextBlock()
{
deleteBlock(next_block.x,next_block.y,
next_block.shape,next_block.status);
next_block.x = 34;
next_block.y = 2;
next_block.shape = rand()%7;
next_block.status = rand()%4;
next_block.color = rand()%0x10;
if(next_block.color == 0x00)
{
next_block.color = 0x0f;
}
printBlock(next_block.x,next_block.y,next_block.shape,
next_block.status,next_block.color);
}
void copyBlock()
{
cur_block = next_block;
cur_block.x= 22;
cur_block.y = 1;
printBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status,cur_block.color);
nextBlock();
}
int downBlock()
{
if(crash(cur_block.x,cur_block.y+1,cur_block.shape,cur_block.status) == -1)
{
save();
lineClear();
updataGame();
copyBlock();
return -1;
}
else if(crash(cur_block.x,cur_block.y+1,cur_block.shape,cur_block.status) == -2)
{
return -2;
}
else
{
deleteBlock(cur_block.x,cur_block.y,
cur_block.shape, cur_block.status);
cur_block.y += 1;
printBlock(cur_block.x,cur_block.y,cur_block.shape,
cur_block.status,cur_block.color);
return 0;
}
}
void leftBlock()
{
if(crash(cur_block.x-1,cur_block.y,cur_block.shape,cur_block.status) == -1)
{
return;
}
deleteBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status);
cur_block.x -= 1;
printBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status,cur_block.color);
}
void rightBlock()
{
if(crash(cur_block.x+1,cur_block.y,cur_block.shape,cur_block.status) == -1)
{
return;
}
deleteBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status);
cur_block.x += 1;
printBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status,cur_block.color);
}
void tranBlock()
{
if(crash(cur_block.x,cur_block.y,cur_block.shape,(cur_block.status+1)%4) == -1)
{
return;
}
deleteBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status);
switch(cur_block.status)
{
case 0:
case 1:
case 2:
cur_block.status++;break;
case 3:
cur_block.status -=3;break;
}
printBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status,cur_block.color);
}
void tranShape()
{
if(crash(cur_block.x,cur_block.y,(cur_block.shape+1)%7,cur_block.status)== -1)
{
return;
}
deleteBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status);
switch(cur_block.shape)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
cur_block.shape++;break;
case 6:
cur_block.shape-=6;break;
}
printBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status,cur_block.color);
}
void tranColor()
{
deleteBlock(cur_block.x,cur_block.y,cur_block.shape,cur_block.status);
cur_block.color = rand()%0x10;
if(cur_block.color == 0x00)
{
cur_block.color = 0x0f;
}
printBlock(cur_block.x,cur_block.y,cur_block.shape,
cur_block.status,cur_block.color);
}
void changeLevel(int grade)
{
if(grade <100)
level =1;
else if(grade >=100&&grade <=300)
level =2;
else if(grade >=300&&grade <=500)
level =3;
else if(grade >=500&&grade <=700)
level =4;
else if(grade >=700&&grade <=900)
level =5;
else if(grade >=1000)
level =6;
}
void changeGrade()
{
grade +=100;
changeLevel(grade);
setColor(0x0e);
setPos(32,19);
printf("等级:%d",level);
setPos(32,20);
printf("得分:%d",grade);
}
int crash(int x,int y,int shape,int status)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(block[shape][status][i][j] == 1)
{
if(windowShape[i+y][j+x-15] == 1)
{
if(cur_block.x == 22 && cur_block.y == 1)
{
return -2;
}
return -1;
}
}
}
}
return 0;
}
void save()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(block[cur_block.shape][cur_block.status][i][j] == 1)
{
windowShape[i+cur_block.y][j+cur_block.x-15] = 1;
}
}
}
}
void updataGame()
{
int i,j;
for(i=23;i>0;i--)
{
for(j=1;j<15;j++)
{
if(windowShape[i][j] == 1)
{
setColor(0x0f);
setPos(15+j,i);
printf("■");
}else{
setColor(0x00);
setPos(15+j,i);
printf(" ");
}
}
}
}
void pause()
{
while(1)
{
if(getch() == 32)
{
break;
}
}
}
void bottomBlock(time3,time4)
{
for(;crash(cur_block.x,cur_block.y+1,cur_block.shape,cur_block.status) != -1;)
{
if(time4-time3>0.01*CLOCKS_PER_SEC)
{
downBlock();
time3 += 1;
time4 += 1;
}
}
}
void downerBlock()
{
while(1)
{
if(crash(cur_block.x,cur_block.y+1,cur_block.shape,cur_block.status) == -1)
{
save();
lineClear();
updataGame();
copyBlock();
return;
}else if(crash(cur_block.x,cur_block.y+1,cur_block.shape,cur_block.status) == -2)
{
return;
}else
{
++cur_block.y;
}
}
}
void lineClear()
{
int i,j,number = 0;
for(i=23;i>0;i--)
{
int total = 0;
for(j=1;j<15;j++)
{
total += windowShape[i][j];
}
if(14 == total)
{
lineDown(i);
i += 1;
number++;
}
}
printGradeLevel(number);
}
void lineDown(int line)
{
int i,j;
for(i=line;i>1;i--)
{
for(j=1;j<15;j++)
{
windowShape[i][j] = windowShape[i-1][j];
}
}
}
void printOver()
{
int i,j;
for(i=23;i>0;i--)
{
for(j=1;j<15;j++)
{
setColor(0xbf);
setPos(15+j,i);
printf("※");
Sleep(5);
}
}
}
void printFinish()
{
setColor(0x0d);
setPos(20,8);
printf("游戏结束!");
setPos(17,9);
printf("按Y重新开始");
setPos(24,9);
printf("按N结束游戏");
switch(getch())
{
case 'Y':
case 'y':
againGame();break;
case 'N':
case 'n':
mciSendString(TEXT("stop a"), NULL, 0, NULL);
break;
default:
printFinish();break;
}
}
void againGame()
{
level = 1;
grade = 0;
int i,j;
for(i=1;i<24;i++)
{
for(j=1;j<15;j++)
{
windowShape[i][j] = 0;
}
}
system("cls");
gameInit();
}
void printStart(int x,int y)
{
int color = rand()%0x10;
if(color == 0x00)
{
color = 0x0f;
}
setColor(color);
setPos(x,y);
printf("■■■■■ ■■■■■ ■■■■■ ■■■■ ■■■ ■■■■");
setPos(x,y+1);
printf(" ■ ■ ■ ■ ■ ■ ■");
setPos(x,y+2);
printf(" ■ ■■■■ ■ ■■■■ ■ ■■■");
setPos(x,y+3);
printf(" ■ ■ ■ ■ ■ ■ ■");
setPos(x,y+4);
printf(" ■ ■■■■■ ■ ■ ■ ■■■ ■■■■");
setPos(25,15);
printf("按任意J键开始游戏!");
}
void deleteStart(int x,int y)
{
int i,j;
for(i=y;i<=y+4;i++)
{
for(j=x;j<=x+33;j++)
{
setPos(j,i);
printf("%2s","");
}
}
}
void printAnimation()
{
clock_t time1,time2;
time1 = clock();
int x = 5;
printStart(x,5);
while(1)
{
time2 = clock();
if(time2-time1>300)
{
time1 = time2;
deleteStart(x,5);
printStart(++x,5);
if(25 == x)
{
deleteStart(x,5);
x=5;
}
}
if(kbhit())
{
break;
}
}
system("cls");
}
void closeMusic()
{
mciSendString(TEXT("stop a"),NULL,0,NULL);
}
void openMusic()
{
mciSendString(TEXT("play a"),NULL,0,NULL);
}
void gameInit()
{
initHandle();
mciSendString("open E:\\codeblocks\\workfile\\Tetris\\bin\\Debug\\res\\俄罗斯方块游戏背景音乐.mp3 alias a",NULL,0,NULL);
mciSendString(TEXT("play a"),NULL,0,NULL);
printAnimation();
printInfo();
printGradeLevel(0);
clock_t startTime =clock();
clock_t time1,time2;
time1 = clock();
setPos(0,0);
windowPrint(15,0);
startBlock();
nextBlock();
while(1){
if(kbhit())
{
switch(getch())
{
case 'W':
case 'w':
case 72:/
tranBlock();break;
case 'A':
case 'a':
case 75:/
leftBlock();break;
case 'D':
case 'd':
case 77:/
rightBlock();break;
case 'S':
case 's':
case 80:/
downBlock();break;
case 13:/
downerBlock();break;
case 32:/
pause();break;
case 'Q':
case 'q':/
tranShape();break;
case 'E':
case 'e':/
tranColor();break;
case 'T':
case 't':/
changeGrade();break;
case 'C':
case 'c':/
printCheat();break;
case 'O':
case 'o':/
closeMusic();break;
case 'I':
case 'i':/
openMusic();break;
}
}
time2 = clock();
if(time2-time1>0.5*CLOCKS_PER_SEC/level)
{
gameTime(startTime);
if(downBlock() == -2)
break;
time1 = time2;
}
}
printOver();
printFinish();
}