---
# System prepended metadata

title: Tetris projecct

---

# Tetris projecct

1.把程式碼分不同檔
2.把方塊變顏色
3.加背景音樂
4.加遊戲的溫馨小提醒
5.按esc鍵可跳出遊戲
6.結束顯示分數並按任意鍵可退出

-------------------------------


分為兩個檔

副函式.h和主函式.c
![](https://i.imgur.com/xqwK7uj.jpg)

![](https://i.imgur.com/mzjSoEl.jpg)

改方塊顏色

```
void setColor(int c){
    SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),c);
}

void make_new_block()
{
    enum block_type type = (int)(rand()%block_type_num);
    switch(type){
        case O:
        	setColor(13);//淡紫色 
        	copy_block(Block0,block_cur);
            break;
        case J:
        	setColor(10); //淡綠色
            copy_block(Blockj,block_cur);
            break;
        case L:
        	setColor(14); //淡黃色
            copy_block(BlockL,block_cur);
            break;
		case I:
			setColor(12); //淡紅色
            copy_block(BlockI,block_cur);
            break; 
        case Z:
        	setColor(9); //淡藍色
            copy_block(BlockZ,block_cur);
            break;
		case S:
			setColor(3); //湖藍色
            copy_block(BlockS,block_cur);
            break; 
		case T:
			setColor(2); //綠色
            copy_block(BlockT,block_cur);
            break;  
        	default:
            break;
	}
    cur_x = (WIDTH-block_w)/2;
    cur_y = 0;
}
```
![](https://i.imgur.com/eaG5T86.jpg)

播音樂

`PlaySound(TEXT("Light.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);`



遊戲的溫馨小提醒
```
void inter_face(){
	gotoxy(cur_x+11,2*cur_y);
    printf("|  --Game tips--");
    
	gotoxy(cur_x+11,2*cur_y+2);
    printf("|    左移：←");

    gotoxy(cur_x+11,2*cur_y+4);
    printf("|    右移：→");
    
    gotoxy(cur_x+11,2*cur_y+6);
    printf("|    快速往下：↓");
    
    gotoxy(cur_x+11,2*cur_y+8);
    printf("|    轉方向：↑");
    
    gotoxy(cur_x+11,2*cur_y+10);
    printf("|    esc：結束遊戲!");
    
    gotoxy(cur_x+11,2*cur_y+12);
    printf("|   ! 加油┌(> O <)┘!");
    
    gotoxy(cur_x+11,2*cur_y+13);
    printf("| ! fighting┌(> O <)┘!");
    
}
```
![](https://i.imgur.com/Kxxr5IC.jpg)

中途想逃離可以按esc鍵

```
case 27:
    system("cls");
    gotoxy(cur_x/ 2+4, cur_y+2);
    printf("---游戲結束---!\n");
    gotoxy(cur_x/ 2+4, cur_y+4);
    printf("---bye(^O^)bye---!\n");
    gotoxy(cur_x / 2+4,cur_y+6);
    printf("---按任意鍵退出---!\n");
    getch();
    exit(0);
    break;
```
![](https://i.imgur.com/bJYCqIl.jpg)

結束顯示分數並按任意鍵可退出
```
if(isCollision(cur_x,cur_y)==1){
    system("cls");
    close_timer();
    char arr[30];
    setColor(15);
	sprintf(arr,"score : %d",score);
	gotoxy(cur_x+22,2*cur_y+4);
	printf("Game over QAQ");
	gotoxy(cur_x+20,2*cur_y+6);
	printf("!!你很棒了\\(>3<)/!!");
	gotoxy(cur_x+20,2*cur_y+8);
	printf("---按任意鍵退出---!\n");
	getch();
    exit(0);
    printxy(arr,30,0);
			
}
```
![](https://i.imgur.com/8h2chWm.jpg)
