主题:[讨论]用VS2005 C++编俄罗斯方块求助
我是刚上大二的学生,也就刚刚学的C++。学校要求用VS2005编个俄罗斯方块,下面是我已经憋出来的代码,其实也就是刚刚完成了键盘控制,想知道怎么才能实现用数组显示方块并旋转,诚心求教,谢谢各位前辈了。
注:colorConsole.h是我们老师弄好的头文件
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <cmath>
#include "colorConsole.h"
using namespace std;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#define KEY_LEFT = 75;
#define KEY_UP = 72;
#define KEY_RIGHT = 77;
#define KEY_DOWN = 80;
void main(void)
{
int x = 0,y = 0;
bool flag = true;
HANDLE handle;
handle = initiate();
WORD wColors[1];
wColors[0] = FOREGROUND_RED | FOREGROUND_INTENSITY;
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
while (x < 70)
{
if (_kbhit())
{
_getch();
switch (_getch())
{
case 75:
{
//flag=!flag;
textout(handle,x-2,y,wColors,1,"■");
textout(handle,x-2,y+1,wColors,1,"■");
textout(handle,x-2,y+2,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
Sleep(200);
textout(handle,x-2,y,wColors,1," ");
textout(handle,x-2,y+1,wColors,1," ");
textout(handle,x-2,y+2,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
x = x - 2;
break;
}
case 72:
{
//flag=!flag;
textout(handle,x,y-1,wColors,1,"■");
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x+2,y+1,wColors,1,"■");
Sleep(200);
textout(handle,x,y-1,wColors,1," ");
textout(handle,x,y,wColors,1," ");
textout(handle,x,y+1,wColors,1," ");
textout(handle,x+2,y+1,wColors,1," ");
y = y - 1;
break;
}
case 77:
{
//flag=!flag;
textout(handle,x+2,y,wColors,1,"■");
textout(handle,x+2,y+1,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
textout(handle,x+4,y+2,wColors,1,"■");
Sleep(200);
textout(handle,x+2,y,wColors,1," ");
textout(handle,x+2,y+1,wColors,1," ");
textout(handle,x+2,y+2,wColors,1," ");
textout(handle,x+4,y+2,wColors,1," ");
x = x + 2;
break;
}
case 80:
{
//flag=!flag;
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x,y+3,wColors,1,"■");
textout(handle,x+2,y+3,wColors,1,"■");
Sleep(200);
textout(handle,x,y+1,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
textout(handle,x,y+3,wColors,1," ");
textout(handle,x+2,y+3,wColors,1," ");
y = y + 1;
break;
}
default:;
}
}
if (flag)
{
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
Sleep(300);
textout(handle,x,y,wColors,1," ");
textout(handle,x,y+1,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
textout(handle,x+2,y+2,wColors,1," ");
y++;
}
}
}
注:colorConsole.h是我们老师弄好的头文件
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <cmath>
#include "colorConsole.h"
using namespace std;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#define KEY_LEFT = 75;
#define KEY_UP = 72;
#define KEY_RIGHT = 77;
#define KEY_DOWN = 80;
void main(void)
{
int x = 0,y = 0;
bool flag = true;
HANDLE handle;
handle = initiate();
WORD wColors[1];
wColors[0] = FOREGROUND_RED | FOREGROUND_INTENSITY;
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
while (x < 70)
{
if (_kbhit())
{
_getch();
switch (_getch())
{
case 75:
{
//flag=!flag;
textout(handle,x-2,y,wColors,1,"■");
textout(handle,x-2,y+1,wColors,1,"■");
textout(handle,x-2,y+2,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
Sleep(200);
textout(handle,x-2,y,wColors,1," ");
textout(handle,x-2,y+1,wColors,1," ");
textout(handle,x-2,y+2,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
x = x - 2;
break;
}
case 72:
{
//flag=!flag;
textout(handle,x,y-1,wColors,1,"■");
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x+2,y+1,wColors,1,"■");
Sleep(200);
textout(handle,x,y-1,wColors,1," ");
textout(handle,x,y,wColors,1," ");
textout(handle,x,y+1,wColors,1," ");
textout(handle,x+2,y+1,wColors,1," ");
y = y - 1;
break;
}
case 77:
{
//flag=!flag;
textout(handle,x+2,y,wColors,1,"■");
textout(handle,x+2,y+1,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
textout(handle,x+4,y+2,wColors,1,"■");
Sleep(200);
textout(handle,x+2,y,wColors,1," ");
textout(handle,x+2,y+1,wColors,1," ");
textout(handle,x+2,y+2,wColors,1," ");
textout(handle,x+4,y+2,wColors,1," ");
x = x + 2;
break;
}
case 80:
{
//flag=!flag;
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x,y+3,wColors,1,"■");
textout(handle,x+2,y+3,wColors,1,"■");
Sleep(200);
textout(handle,x,y+1,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
textout(handle,x,y+3,wColors,1," ");
textout(handle,x+2,y+3,wColors,1," ");
y = y + 1;
break;
}
default:;
}
}
if (flag)
{
textout(handle,x,y,wColors,1,"■");
textout(handle,x,y+1,wColors,1,"■");
textout(handle,x,y+2,wColors,1,"■");
textout(handle,x+2,y+2,wColors,1,"■");
Sleep(300);
textout(handle,x,y,wColors,1," ");
textout(handle,x,y+1,wColors,1," ");
textout(handle,x,y+2,wColors,1," ");
textout(handle,x+2,y+2,wColors,1," ");
y++;
}
}
}