VS2013環境下編寫的俄羅斯方塊原始碼
阿新 • • 發佈:2019-01-25
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <thread>
#include <conio.h>
//#include <graphics.h>
using namespace std;
/*IMAGE qiangbi;*/
#define MAX_X 23
#define MAX_Y 21
#define LOGO 'H'
int array[MAX_X][MAX_Y] = { 0 };
struct x_y
{
int x;
int y;
};
struct x_y point[4];
struct x_y point_1[4];
void gotoxy(int x, int y)
{
int z = 0x0b;
HANDLE houtput;
COORD loc;
loc.X = x;
loc.Y = y;
houtput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(houtput, loc);
}
void display_all()
{
int i, j;
gotoxy(0, 0);
for (j = 0; j < MAX_Y; j++)
{
for (i = 0; i < MAX_X; i++)
{
if (array[i][j] == 2)
{
cout<<LOGO;
}
else if (array[i][j] == 1)
{
cout << "|";
}
else if (array[i][j] == 3)
{
cout << "T";
}
else
{
cout << " ";
}
}
cout << endl;
}
gotoxy(23, 3);
cout << " ";
gotoxy(23, 4);
cout << " ";
gotoxy(23, 5);
cout << " ";
for (i = 0; i < 4; i++)
{
gotoxy((point_1[i].x ) + 26, (point_1[i].y) + 3);
cout << LOGO;
}
gotoxy(30, 12);
cout << "遊戲規則說明:";
gotoxy(30, 13);
cout << "w 旋轉方塊";
gotoxy(30, 14);
cout << "s 向下移動";
gotoxy(30, 15);
cout << "a 向左移動";
gotoxy(30, 16);
cout << "d 向右移動";
gotoxy(30, 17);
cout << "空格 暫停";
gotoxy(30, 18);
cout << "ESC 退出";
}
int ran = 0;
void get_rand()
{
ran = rand() % 7;
}
int point_type;
int point_type_1;
void get_point()
{
point_type = ran;
switch (point_type)
{
case 0:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 5;
point[1].y = 0;
point[2].x = 6;
point[2].y = 0;
point[3].x = 7;
point[3].y = 0;
break;
}
case 1:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 5;
point[1].y = 0;
point[2].x = 6;
point[2].y = 0;
point[3].x = 6;
point[3].y = 1;
break;
}
case 2:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 4;
point[1].y = 1;
point[2].x = 5;
point[2].y = 1;
point[3].x = 5;
point[3].y = 2;
break;
}
case 3:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 5;
point[1].y = 0;
point[2].x = 6;
point[2].y = 0;
point[3].x = 5;
point[3].y = 1;
break;
}
case 4:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 5;
point[1].y = 0;
point[2].x = 4;
point[2].y = 1;
point[3].x = 5;
point[3].y = 1;
break;
}
case 5:
{
point[0].x = 4;
point[0].y = 0;
point[1].x = 5;
point[1].y = 0;
point[2].x = 6;
point[2].y = 0;
point[3].x = 4;
point[3].y = 1;
break;
}
case 6:
{
point[0].x = 5;
point[0].y = 0;
point[1].x = 4;
point[1].y = 1;
point[2].x = 5;
point[2].y = 1;
point[3].x = 4;
point[3].y = 2;
break;
}
default:break;
}
}
void get_point_1()
{
point_type_1 = ran;
switch (point_type_1)
{
case 0:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 5;
point_1[1].y = 0;
point_1[2].x = 6;
point_1[2].y = 0;
point_1[3].x = 7;
point_1[3].y = 0;
break;
}
case 1:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 5;
point_1[1].y = 0;
point_1[2].x = 6;
point_1[2].y = 0;
point_1[3].x = 6;
point_1[3].y = 1;
break;
}
case 2:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 4;
point_1[1].y = 1;
point_1[2].x = 5;
point_1[2].y = 1;
point_1[3].x = 5;
point_1[3].y = 2;
break;
}
case 3:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 5;
point_1[1].y = 0;
point_1[2].x = 6;
point_1[2].y = 0;
point_1[3].x = 5;
point_1[3].y = 1;
break;
}
case 4:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 5;
point_1[1].y = 0;
point_1[2].x = 4;
point_1[2].y = 1;
point_1[3].x = 5;
point_1[3].y = 1;
break;
}
case 5:
{
point_1[0].x = 4;
point_1[0].y = 0;
point_1[1].x = 5;
point_1[1].y = 0;
point_1[2].x = 6;
point_1[2].y = 0;
point_1[3].x = 4;
point_1[3].y = 1;
break;
}
case 6:
{
point_1[0].x = 5;
point_1[0].y = 0;
point_1[1].x = 4;
point_1[1].y = 1;
point_1[2].x = 5;
point_1[2].y = 1;
point_1[3].x = 4;
point_1[3].y = 2;
break;
}
default:break;
}
}
void display_point()
{
int i;
for (i = 0; i < 4; i++)
{
gotoxy(point[i].x, point[i].y);
cout << LOGO;
}
}
int speed = 10;
int count = 0;
int m = 1;
void display_score()
{
if (0 == ::count)
{
gotoxy(50, 0);
cout << "level:" << m << endl;
gotoxy(50, 1);
cout << "score:0" << endl;
}
else if (::count >= 100)
{
m++;
speed--;
::count -= 100;
gotoxy(50, 0);
cout << "level:" << m << endl;
gotoxy(50, 1);
cout << "score:" << ::count<<endl;
}
else
{
gotoxy(50, 0);
cout << "level:" << m << endl;
gotoxy(50, 1);
cout << "score:" << ::count<<endl;
}
if (speed == 0)
{
system("CLS");
gotoxy(36, 10);
system("color a0");
cout << "遊戲結束!恭喜闖關成功!" << endl;
gotoxy(36, 16);
exit(0);
}
}
void block_remove(int line)
{
int i, j;
for (j= line; j > 0; j--)
{
for (i = 1; i < MAX_X-1; i++)
{
array[i][j] = array[i][j-1];
}
}
::count += 100;
display_score();
}
void is_ok()
{
int i, j;
int k = 0;
for (j = MAX_Y - 1; j > 0; j--)
{
for (i = 1; i < MAX_X - 1; i++)
{
if (array[i][j] == 2)
{
k++;
}
}
if (k == MAX_X -2)
{
block_remove(j);
j++;
}
k = 0;
}
}
int blockstate = 0;
void move_down_hinder()
{
int i, j;
for (i = 0; i < 4; i++)
{
array[point[i].x][point[i].y] =2;
}
blockstate = 0;
}
void move_point_left()
{
int i;
if ((array[point[0].x - 1][point[0].y] != 0) || (array[point[1].x - 1][point[1].y] != 0) || (array[point[2].x - 1][point[2].y ] != 0) || (array[point[3].x - 1][point[3].y ] != 0))
{
return;
}
for (i = 0; i < 4; i++)
{
point[i].x -= 1;
}
}
void move_point_down2()
{
int i;
if ((array[point[0].x ][point[0].y + 1] != 0) || (array[point[1].x ][point[1].y + 1] != 0) || (array[point[2].x ][point[2].y + 1] != 0) || (array[point[3].x ][point[3].y + 1] != 0) )
{
return;
}
for (i = 0; i < 4; i++)
{
point[i].y++;
}
}
void move_point_right()
{
int i;
if ((array[point[0].x +1][point[0].y ] != 0) || (array[point[1].x + 1][point[1].y] != 0) || (array[point[2].x + 1][point[2].y] != 0) || (array[point[3].x + 1][point[3].y ] != 0))
{
return;
}
for (i = 0; i < 4; i++)
{
point[i].x += 1;
}
}
void move_point_down()
{
int i;
for (i = 0; i < 4; i++)
{
if (array[point[0].x][point[0].y + 1] != 0 || array[point[1].x][point[1].y + 1] != 0 || array[point[2].x][point[2].y + 1] != 0||array[point[3].x][point[3].y + 1] != 0)
{
move_down_hinder();
get_point();
get_rand();
get_point_1();
return;
}
for (i = 0; i < 4; i++)
{
point[i].y++;
}
}
}
void init_block()
{
int i;
for (i = 0; i < MAX_Y; i++)
{
array[0][i] = 1;
array[MAX_X - 1][i] = 1;
}
for (i = 1; i < MAX_X-1; i++)
{
array[i][MAX_Y-1] = 3;
}
}
void changBlock()
{
switch (point_type)
{
case 0:
{
blockstate += 1;
if (blockstate > 2)
blockstate = 1;
if (blockstate == 1)
{
if (array[point[0].x][point[0].y - 1] != 0)
break;
else
{
point[0].x = point[3].x;
point[0].y = point[3].y - 3;
point[1].x = point[3].x;
point[1].y = point[3].y - 2;
point[2].x = point[3].x;
point[2].y = point[3].y - 1;
break;
}
}
else if (blockstate == 2)
{
if (array[point[3].x + 1][point[0].y] != 0 || array[point[3].x + 2][point[1].y] != 0 || array[point[3].x +3][point[2].y] != 0 || array[point[3].x +4][point[3].y] != 0)
break;
else
{
point[0].x = point[3].x - 3;
point[0].y = point[3].y;
point[1].x = point[3].x - 2;
point[1].y = point[3].y;
point[2].x = point[3].x - 1;
point[2].y = point[3].y;
break;
}
}
}
case 1:
{
blockstate += 1;
if (blockstate > 4)
blockstate = 1;
if (blockstate == 1)
{
point[0].x = point[2].x;
point[0].y = point[2].y - 2;
point[3].x = point[2].x;
point[3].y = point[2].y - 1;
break;
}
else if (blockstate == 2)
{
point[0].x = point[2].x + 1 ;
point[0].y = point[2].y;
point[1].x = point[2].x+2;
point[1].y = point[2].y;
break;
}
else if (blockstate == 3)
{
point[3].x = point[2].x;
point[3].y = point[2].y+1;
point[1].x = point[2].x;
point[1].y = point[2].y+2;
break;
}
else if (blockstate == 4)
{
point[0].x = point[2].x-2;
point[0].y = point[2].y;
point[1].x = point[2].x-1;
point[1].y = point[2].y;
break;
}
}
case 2:
{
blockstate += 1;
if (blockstate > 2)
blockstate = 1;
if (blockstate == 1)
{
point[0].x = point[2].x;
point[0].y = point[2].y -1;
point[3].x = point[2].x +1;
point[3].y = point[2].y -1;
break;
}
if (blockstate == 2)
{
point[0].x = point[2].x-1;
point[0].y = point[2].y - 1;
point[3].x = point[2].x ;
point[3].y = point[2].y + 1;
break;
}
}
case 3:
{
blockstate += 1;
if (blockstate > 4)
blockstate = 1;
if (blockstate == 1)
{
point[2].x = point[1].x ;
point[2].y = point[1].y - 1;
break;
}
if (blockstate == 2)
{
point[3].x = point[1].x+1;
point[3].y = point[1].y ;
break;
}
if (blockstate == 3)
{
point[0].x = point[1].x;
point[0].y = point[1].y + 1;
break;
}
if (blockstate == 4)
{
point[0].x = point[1].x-1;
point[0].y = point[1].y ;
point[2].x = point[1].x + 1;
point[2].y = point[1].y;
point[3].x = point[1].x ;
point[3].y = point[1].y+1;
break;
}
}
case 5:
{
blockstate += 1;
if (blockstate > 4)
blockstate = 1;
if (blockstate == 1)
{
point[1].x = point[0].x;
point[1].y = point[0].y +2;
point[2].x = point[0].x - 1;
point[2].y = point[0].y ;
break;
}
if (blockstate == 2)
{
point[1].x = point[0].x;
point[1].y = point[0].y -1;
point[3].x = point[0].x - 2;
point[3].y = point[0].y;
break;
}
if (blockstate == 3)
{
point[2].x = point[0].x;
point[2].y = point[0].y - 2;
point[3].x = point[0].x + 1;
point[3].y = point[0].y;
break;
}
if (blockstate == 4)
{
point[3].x = point[0].x;
point[3].y = point[0].y +1;
point[1].x = point[0].x + 1;
point[1].y = point[0].y;
point[2].x = point[0].x + 2;
point[2].y = point[0].y;
break;
}
}
case 6:
{
blockstate += 1;
if (blockstate > 2)
blockstate = 1;
if (blockstate == 1)
{
point[1].x = point[2].x +1;
point[1].y = point[2].y ;
point[3].x = point[2].x -1;
point[3].y = point[2].y - 1;
break;
}
if (blockstate == 2)
{
point[1].x = point[2].x - 1;
point[1].y = point[2].y;
point[3].x = point[2].x - 1;
point[3].y = point[2].y + 1;
break;
}
}
}
}
void speedcontrol()
{
int t = 20 - m * 2;
Sleep(t);
}
int main()
{
SetConsoleTitleA("俄羅斯方塊");
int cmd = 0, flag = 0;
int i, p = 2;
srand((unsigned int)time(0));
init_block();
display_score();
get_rand();
get_point();
get_rand();
get_point_1();
while (1)
{
system("color a0");
is_ok();
//Sleep(20);
display_all();
display_point();
flag = 0;
for (i = 0; i < speed; i++)
{
if (_kbhit())
{
cmd = getch();
}
if (cmd == 75)
{
move_point_left();
display_all();
display_point();
cmd = 0;
}
else if (cmd == 77)
{
move_point_right();
display_all();
display_point();
cmd = 0;
}
else if (cmd == 80)
{
move_point_down2();
display_all();
display_point();
cmd = 0;
}
else if (cmd == 72)
{
changBlock();
display_all();
display_point();
cmd = 0;
}
else if (cmd == 32)
{
flag = 1;
p++;
cmd = 0;
}
else if (cmd == 27)
{
system("CLS");
gotoxy(30, 1);
cout << "Game Over" << endl;
exit(0);
}
speedcontrol();
}
if (flag == 0 && p % 2 == 0)
{
move_point_down();
for (int i = 1; i < MAX_X-1; i++)
{
if (array[i][0] > 0)
{
system("CLS");
system("color a0");
gotoxy(30, 1);
cout << "Game Over" << endl;
gotoxy(30, 4);
cout << "level: " << m << endl;
gotoxy(30, 5);
cout << "score: " << ::count << endl;
exit(0);
}
}
}
}
return 0;
}