俄罗斯方块,想问下,红色部分为什么不成功,我利用的是坐标求解
#include<stdio.h>#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define M 27
#define N 20
int a = 0, b = 5;
int id;
int score = 0, next, map[M][N] = { 0 }, gamespeed = 300;
int shape[28][6] = { { 0, -1, 0, -2, 1, 0 }, { 0, 1, 1, 0, 2, 0 }, { -1, 0, 0, 1, 0, 2 }, { 0, -1, -1, 0, -2, 0 },
{ 0, -1, 0, 1, -1, 0 }, { 0, 1, 1, 0, -1, 0 }, { 1, 0, 0, -1, 0, 1 }, { 1, 0, -1, 0, 0, -1 },
{ -1, 1, 0, 1, 1, 0 }, { 0, -1, 1, 0, 1, 1 }, { -1, 0, 0, -1, 1, -1 }, { -1, -1, -1, 0, 0, 1 },
{ -1, 0, 0, 1, 1, 1 }, { 0, 1, 1, -1, 1, 0 }, { -1, 0, 0, 1, 1, 1 }, { 0, 1, 1, -1, 1, 0 },
{ -1, 0, 0, -1, 0, -2 }, { -1, 0, -2, 0, 0, 1 }, { 0, 1, 0, 2, 1, 0 }, { 0, -1, 1, 0, 2, 0 },
{ 0, 1, 1, 0, 1, 1 }, { 0, -1, 1, 0, 1, -1 }, { -1, 0, 0, -1, -1, -1 }, { -1, 0, -1, 1, 0, 1 },
{ 0, 1, 0, 2, 0, 3 }, { 1, 0, 2, 0, 3, 0 }, { 0, -1, 0, -2, 0, -3 }, { -1, 0, -2, 0, -3, 0 }
};
void gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void gameover(int h[M][N])
{
int i, j, flag = 0;
for (j = 1; j < 17; j++)
if (h[1][j] != 0)
{
flag = 0;
break;
}
if (flag == 1)
{
for (i = 1; i < 17; i++)
{
gotoxy(2, i + 1);
for (j = 1; j < 11; j++)
printf("□");
gotoxy(7, 9);
printf("game over!");
gotoxy(1, 19);
exit(0);
}
}
}
void block(int x, int y, int h[M][N])
{
int i;
h[x][y] = 3;
for (i = 0; i < 6; i += 2)
h[x + shape[id][i]][y + shape[id][i + 1]] = 3;
}
void clsc(int x, int y, int h[M][N])
{
int i;
h[x][y] = 0;
for (i = 0; i < 6; i += 2)
h[x + shape[id][i]][y + shape[id][i + 1]] = 0;
}
int judge(int x, int y, int h[M][N])
{
int i;
if (h[x][y] != 0)
return 0;
for (i = 0; i < 6; i += 2)
if (h[x + shape[id][i]][y + shape[id][i + 1]] != 0)
return 0;
return 1;
}
void print(int h[M][N])
{
int i, j;
for (i = 0; i < 19; i++)
{
for (j = 0; j < 12; j++)
{
if (i == 18) h[i][j] = 2;
if (j == 0 || j == 11)h[i][j] = 1;
if (h[i][j] == 0)
printf(" ");
if (h[i][j] == 1)
printf("|");
if (h[i][j] == 2)
printf("--");
if (h[i][j] == 3)
printf("■");
if (i == 1 && j == 11)printf("next:");
if (i == 11 && j == 11)printf("score:%d", score);
if (i == 14 && j == 11)printf("speed:%d", score / 100 + 1);
}
printf("\n");
}
}
void ne(int h[M][N]) {
int i, j;
for (i = 0; i < 6; i += 2)
h[6 + shape[id][i]][16 + shape[id][i + 1]] = 0;
h[6][16] = 3;
for (j = 0; j < 6; j += 2)
h[6 + shape[next][j]][16 + shape[next][j + 1]] = 3;
}
void main()
{
int i, j, id;
int flag = 0;
srand(time(NULL));
id = rand() % 27;
print(map);
while (1)
{
next = rand() % 27;
id = next;
ne(map);
while (!_kbhit())
{
if (judge(a + 1, b, map) == 1)
{
clsc(a, b, map);
block(a++, b, map);
}
else
{
for (i = 1; i < 17; i++)
for (j = 1; j < 11; j++)
if (map[i][j] != 3)
{
flag = 1;
break;
}
if (flag == 0)
map[i][j] = map[i - 1][j];
print(map);
a = 0;
b = 5;
score += 10;
if (score % 100 == 0) gamespeed -= 50;
Sleep(gamespeed);
}
}
switch (_getch())
{
case'a':case'A':if (judge(a, b - 1, map) == 1)clsc(a, b, map), block(a, --b, map); break;
case'd':case'D':if (judge(a, b + 1, map) == 1)clsc(a, b, map), block(a, ++b, map); break;
case's':case'S':if (judge(a + 1, b, map) == 1)clsc(a, b, map), block(a++, b, map); break;
case'w':case'W':if (id % 4 == 0 && id != 0)id -= 4, block(a, b, map); break;
}
}
}