跳到正文

休闲·娱乐

俄罗斯方块

hdkghc约 32 分钟
原文
文章信息与版本

文章信息

发布
2024年10月4日
收录
2025年12月12日
更新
2026年11月25日
篇幅
约 32 分钟

版本

  1. v1当前

api.h

C++
#ifndef ____________
#define ____________

# include<conio.h>
# include<windows.h>
# include<bits/stdc++.h>
# include<shlobj.h>
using namespace std;

# define KeyDown(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
# define SetColor(attr) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr)

# define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n", __FILE__, GetLastError(), api, __LINE__);}
# define SUCCESS 0
# define FAIL 1
# define BUF 1024
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);

//# ifndef clrscr
//void clrscr (void)
//{
//    HANDLE hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
//    COORD coordScreen = {0, 0};
//    DWORD cCharsWritten;
//    CONSOLE_SCREEN_BUFFER_INFO csbi;
//    DWORD dwConSize;
//    GetConsoleScreenBufferInfo (hOutput, &csbi);
//    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
//    FillConsoleOutputCharacter (hOutput, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten);
//    GetConsoleScreenBufferInfo (hOutput, &csbi);
//    FillConsoleOutputAttribute (hOutput, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
//    SetConsoleCursorPosition (hOutput, coordScreen);
//}
//# endif
void InsertMode()
{
	DWORD Mode;
	GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &Mode);
	SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Mode & ~ENABLE_QUICK_EDIT_MODE | ENABLE_INSERT_MODE);
}
void MaxSize(HWND m_hWnd)
{
	ShowWindow(m_hWnd, SW_MAXIMIZE);
}
void HideSizebox(HWND m_hWnd)
{
	RECT rc;
	GetWindowRect (m_hWnd, &rc);
	SetWindowLongPtr (m_hWnd, GWL_STYLE, GetWindowLong (m_hWnd, GWL_STYLE) & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
	SetWindowPos (m_hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
}
void GrayCloseButt(HWND m_hWnd)
{
	EnableMenuItem (GetSystemMenu (m_hWnd, false), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
void EnableCloseButt(HWND m_hWnd)
{
	EnableMenuItem (GetSystemMenu (m_hWnd, false), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
}
BOOL ChangeFilename(const char *Filen, char *Dest)
{
	char szPath[1024];
	SHGetSpecialFolderPathA(NULL, szPath, CSIDL_APPDATA, FALSE);
	return sprintf(Dest, "%s\\VOS\\%s.ini", szPath, Filen) == EOF ? FAIL : SUCCESS;
}
class Cursor
{
	protected:
		stack<COORD> pre;
	public:
		Cursor()
		{
			CONSOLE_SCREEN_BUFFER_INFO csbi;
			GetConsoleScreenBufferInfo(hOut, &csbi);
			pre.push(csbi.dwCursorPosition);
		}
		void Goto(int x, int y)
		{
			pre.push({x, y});
			SetConsoleCursorPosition(hOut, pre.top());
		}
		BOOL Back()
		{
			if (pre.empty())
				return FAIL;
			pre.pop();
			if (pre.empty())
				return FAIL;
			SetConsoleCursorPosition(hOut, pre.top());
		}
		BOOL Empty()
		{
			return pre.empty();
		}
};
class MouseEvent
{
	protected:
		INPUT_RECORD rec;
		DWORD dw;
	public:
		MouseEvent flush()
		{
			ReadConsoleInputA(hIn, &rec, 1, &dw);
			return *this;
		}
		BOOL MouseInRange(POINT leftup, POINT rightdown)
		{
			int x, y;
			x = rec.Event.MouseEvent.dwMousePosition.X;
			y = rec.Event.MouseEvent.dwMousePosition.Y;
			if (x >= leftup.x && y >= leftup.y && x <= rightdown.x && y <= rightdown.y)
				return TRUE;
			return FALSE;
		}
		COORD GetMousePos()
		{
			return rec.Event.MouseEvent.dwMousePosition;
		}
		BOOL MouseLeftButtPressed()
		{
			return rec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED;
		}
		BOOL MouseMidButtPressed()
		{
			return rec.Event.MouseEvent.dwButtonState == FROM_LEFT_2ND_BUTTON_PRESSED;
		}
		BOOL MouseRightButtPressed()
		{
			return rec.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED;
		}
		BOOL DoubleClick()
		{
			return rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK;
		}
		BOOL MouseMove()
		{
			return rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED;
		}
		BOOL MouseWheeled()
		{
			return rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED;
		}
		INPUT_RECORD GetRecord()
		{
			return rec;
		}
};
class Button
{
	protected:
		Cursor cs;
		int x, y;
		char name[1024];
		int l, mouseOn;
		POINT lu, rd;
		BOOL press;
		BOOL dk;
	public:
		WORD t, ut, pr;
		Button()
		{
			t = 0x8070;
			ut = 0x8007;
			pr = 0xb0;
			mouseOn = true;
			press = false;
			dk = false;
		}
		void init(int X, int Y, bool _dk = false)
		{
			x = X, y = Y;
			dk = _dk;
		}
		void operator=(const char *s)
		{
			strcpy(name, s);
			l = strlen(s);
			lu.x = this->x;
			lu.y = this->y;
			rd.x = x + l - 1;
			rd.y = this->y;
			cs.Goto(x, y);
			printf("%s", s);
			cs.Back();
		}
		void flush(MouseEvent me)
		{
			if (me.MouseInRange(lu, rd))
			{
				if (!mouseOn)
				{
					mouseOn = true;
					cs.Goto(x, y);
					SetColor(t);
					printf("%s", this->name);
					SetColor(ut);
					cs.Back();
				}
				if (me.MouseLeftButtPressed() && (dk ? me.DoubleClick() : true))
				{
					press = true;
					cs.Goto(x, y);
					SetColor(pr);
					printf("%s", this->name);
					SetColor(ut);
					cs.Back();
					Sleep(100);
				}
			}
			else if (mouseOn)
			{
				mouseOn = false;
				cs.Goto(x, y);
				SetColor(ut);
				printf("%s", this->name);
				cs.Back();
			}
		}
		BOOL pressed()
		{
			return press;
		}
};
class INI
{
	protected:
		char file[1024];
	public:
		void open(const char *File)
		{
			ChangeFilename(File, file);
		}
		BOOL read(const char *section, const char *key, char *value)
		{
			GetPrivateProfileStringA(section, key, "\0", value, BUF, file);
			return (value[0] == '\0') ? FAIL : SUCCESS;
		}
		BOOL write(const char *section, const char *key, const char *value)
		{
			return (WritePrivateProfileStringA(section, key, value, file) == 0) ? FAIL : SUCCESS;
		}
};
void load(string s, int l = 0, int r = 20)
{
	MouseEvent me;
	SetColor(0x07);
	system("cls");
	cout << s << endl;
	for (int i = 0; i <= 100; i++)
	{
		SetColor(0x07);
		printf("\r");
		printf("%3d%% ", i);
		SetColor(0xa0);
		for (int j = 0; j < i; j++)
		{
			printf(" ");
		}
		Sleep(l + rand() % (r - l + 1));
	}
	SetColor(0x07);
	printf("\n");
	Button b;
	b.init(50, 2);
	b = "    OK    ";
	while (1)
	{
		me.flush();
		b.flush(me);
		if (b.pressed())
		{
			return;
		}
	}
}

#endif

main.cpp

C++
#include"api.h"
#include<sys/timeb.h>
#define HIGH 20
#define LONG 10
#define S 200
enum COLOR
{
	NORMAL = 0x0F, FINISHED = 0xF0, CURRENT = 0xC0, EDGE = 0xB0, NEXT = 0xE0, GO = 0xAC, SHADOW = 0x40
};
bool mp[LONG + 2][HIGH + 2];
int score = 0;

struct block
{
	COORD b, c, d;
};

block blocktype[5];
/*
 *   **      **   *
*** **  **** ** ***
*/

// 列 行 

void Goto(int x, int y)
{
	Cursor c;
	c.Goto(x << 1, y);
}

class Current
{
public:
	block b, ob;
	COORD center, ocent;
	Cursor c;
	void rotate()
	{
		ob = b;
		ocent = center;
		swap(b.b.X, b.b.Y);
		swap(b.c.X, b.c.Y);
		swap(b.d.X, b.d.Y);
		b.b.X = -b.b.X;
		b.c.X = -b.c.X;
		b.d.X = -b.d.X;
		if(mp[center.X][center.Y] || mp[center.X + b.b.X][center.Y + b.b.Y] || mp[center.X + b.c.X][center.Y + b.c.Y] || mp[center.X + b.d.X][center.Y + b.d.Y])
		{
			b = ob;
			center = ocent;
		}
	}
	void print()
	{
		SetColor(NORMAL);
		Goto(ocent.X, ocent.Y);
		printf("  ");
		Goto(ocent.X + ob.b.X, ocent.Y + ob.b.Y);
		printf("  ");
		Goto(ocent.X + ob.c.X, ocent.Y + ob.c.Y);
		printf("  ");
		Goto(ocent.X + ob.d.X, ocent.Y + ob.d.Y);
		printf("  ");
		SetColor(CURRENT);
		Goto(center.X, center.Y);
		printf("  ");
		Goto(center.X + b.b.X, center.Y + b.b.Y);
		printf("  ");
		Goto(center.X + b.c.X, center.Y + b.c.Y);
		printf("  ");
		Goto(center.X + b.d.X, center.Y + b.d.Y);
		printf("  ");
		SetColor(NORMAL);
	}
	void left()
	{
		ob = b;
		ocent = center;
		if(mp[center.X - 1][center.Y] || mp[center.X + b.b.X - 1][center.Y + b.b.Y] || mp[center.X + b.c.X - 1][center.Y + b.c.Y] || mp[center.X + b.d.X - 1][center.Y + b.d.Y]) return;
		center.X--;
	}
	void right()
	{
		ob = b;
		ocent = center;
		if(mp[center.X + 1][center.Y] || mp[center.X + b.b.X + 1][center.Y + b.b.Y] || mp[center.X + b.c.X + 1][center.Y + b.c.Y] || mp[center.X + b.d.X + 1][center.Y + b.d.Y]) return;
		center.X++;
	}
	bool down()
	{
		ob = b;
		ocent = center;
		if(mp[center.X][center.Y + 1] || mp[center.X + b.b.X][center.Y + b.b.Y + 1] || mp[center.X + b.c.X][center.Y + b.c.Y + 1] || mp[center.X + b.d.X][center.Y + b.d.Y + 1]) return true;
		center.Y++;
		return false;
	}
	void erase()
	{
		SetColor(NORMAL);
		Goto(ocent.X, ocent.Y);
		printf("  ");
		Goto(ocent.X + ob.b.X, ocent.Y + ob.b.Y);
		printf("  ");
		Goto(ocent.X + ob.c.X, ocent.Y + ob.c.Y);
		printf("  ");
		Goto(ocent.X + ob.d.X, ocent.Y + ob.d.Y);
		printf("  ");
		Goto(center.X, center.Y);
		printf("  ");
		Goto(center.X + b.b.X, center.Y + b.b.Y);
		printf("  ");
		Goto(center.X + b.c.X, center.Y + b.c.Y);
		printf("  ");
		Goto(center.X + b.d.X, center.Y + b.d.Y);
		printf("  ");
	}
	void finish()
	{
		SetColor(NORMAL);
		Goto(ocent.X, ocent.Y);
		printf("  ");
		Goto(ocent.X + ob.b.X, ocent.Y + ob.b.Y);
		printf("  ");
		Goto(ocent.X + ob.c.X, ocent.Y + ob.c.Y);
		printf("  ");
		Goto(ocent.X + ob.d.X, ocent.Y + ob.d.Y);
		printf("  ");
		SetColor(FINISHED);
		Goto(center.X, center.Y);
		printf("  ");
		Goto(center.X + b.b.X, center.Y + b.b.Y);
		printf("  ");
		Goto(center.X + b.c.X, center.Y + b.c.Y);
		printf("  ");
		Goto(center.X + b.d.X, center.Y + b.d.Y);
		printf("  ");
		SetColor(NORMAL);
		mp[center.X][center.Y] = 1;
		mp[center.X + b.b.X][center.Y + b.b.Y] = 1;
		mp[center.X + b.c.X][center.Y + b.c.Y] = 1;
		mp[center.X + b.d.X][center.Y + b.d.Y] = 1;
		ocent = {};
		ob = {};
	}
};

class /* You are the */ shadow // to my life ---- <<Faded>>
{
public:
	block b, ob;
	COORD center, ocent;
	Current *pc;
	bool down()
	{
		if(mp[center.X][center.Y + 1] || mp[center.X + b.b.X][center.Y + b.b.Y + 1] || mp[center.X + b.c.X][center.Y + b.c.Y + 1] || mp[center.X + b.d.X][center.Y + b.d.Y + 1]) return true;
		center.Y++;
		return false;
	}
	void flush()
	{
		ob = b;
		ocent = center;
		b = pc->b;
		center = pc->center;
		while(!down());
		print();
	}
	void finish()
	{
		ob = {};
		ocent = {};
		b = {};
		center = {};
	}
	void print()
	{
		SetColor(NORMAL);
		Goto(ocent.X, ocent.Y);
		printf("  ");
		Goto(ocent.X + ob.b.X, ocent.Y + ob.b.Y);
		printf("  ");
		Goto(ocent.X + ob.c.X, ocent.Y + ob.c.Y);
		printf("  ");
		Goto(ocent.X + ob.d.X, ocent.Y + ob.d.Y);
		printf("  ");
		SetColor(SHADOW);
		Goto(center.X, center.Y);
		printf("  ");
		Goto(center.X + b.b.X, center.Y + b.b.Y);
		printf("  ");
		Goto(center.X + b.c.X, center.Y + b.c.Y);
		printf("  ");
		Goto(center.X + b.d.X, center.Y + b.d.Y);
		printf("  ");
		SetColor(NORMAL);
	}
};

void init()
{
	blocktype[0] = {{-1, 0}, {0, -1}, {1, 0}};
	blocktype[1] = {{-1, 1}, {0, 1}, {1, 0}};
	blocktype[2] = {{0, -1}, {0, 2}, {0, 1}};
	blocktype[3] = {{1, 1}, {1, 0}, {0, 1}};
	blocktype[4] = {{-1, 0}, {1, 0}, {1, -1}};
	for(int i = 0; i <= HIGH; i++)
	{
		mp[0][i] = mp[LONG + 1][i] = 1;
		SetColor(NORMAL);
		Goto(0, i);
		SetColor(EDGE);
		cout << "  ";
		
		SetColor(NORMAL);
		Goto(LONG + 1, i);
		SetColor(EDGE);
		cout << "  ";
	}
	for(int i = 0; i <= LONG + 1; i++)
	{
		mp[i][HIGH + 1] = 1;
		SetColor(NORMAL);
		Goto(i, HIGH + 1);
		SetColor(EDGE);
		cout << "  ";
	}
	SetColor(NORMAL);
}

int main()
{
	FILE *in = fopen("max.score", "r");
	int mxsc;
	fscanf(in, "%d", &mxsc);
	fclose(in);
	srand(time(NULL));
	cout << "请使用 10x20 点阵字体" << endl;
	system("color 0f");
	InsertMode();
	
	cout << "Press [Space] to continue..." << endl;
	
	while(!KeyDown(VK_SPACE));
	
	system("cls");
	
	init();
	
	Sleep(2000);
	
	Current curr;
	Cursor c;
	
	bool last = 0, change = 1;
	int tl = 0, tr = 0, td = 0;
	timeb lastd;
	long long ts1, ts2;
	int next = rand() % 5;
	int cur = rand() % 5;
	
	c.Goto(25, 0);
	cout << "Score : 00000000   Max score : "; // 56
	printf("%08d", mxsc);
	c.Goto(25, 1);
	cout << "Next : ";
	
	time_t ttt;
	const time_t minttt = 200;
	
	time_t starttm = time(NULL);
	
	shadow sd;    // ------------------------------------------------------------
	sd.pc = &curr;// ------------------------------------------------------------
	
	while(1)
	{
		ttt = max(minttt, 1000 - (time(NULL) - starttm));
		Goto(0, 0);
		SetColor(EDGE);
		cout << "  ";
		SetColor(NORMAL);
		if(change)
		{
			curr.center = {5, 1};
			curr.b = blocktype[cur];
			
			sd.flush();// ------------------------------------------------------------
			
			curr.print();
			change = 0;
			ftime(&lastd);
			ts1 = lastd.time * 1000 + lastd.millitm;
			block b = blocktype[next];
			SetColor(NEXT);
			Goto(17, 2);
			printf("  ");
			Goto(17 + b.b.X, 2 + b.b.Y);
			printf("  ");
			Goto(17 + b.c.X, 2 + b.c.Y);
			printf("  ");
			Goto(17 + b.d.X, 2 + b.d.Y);
			printf("  ");
			
			// Game Over?
			bool go = 0;
			for(int i = 1; i <= LONG; i++)
			{
				if(mp[i][0])
				{
					go = 1;
					break;
				}
			}
			if(go)
			{
				Goto(3, 10);
				SetColor(GO);
				cout << " Game Over! ";
				if(score > mxsc)
				{
					FILE *out = fopen("max.score", "w");
					fprintf(out, "%d", score);
					fclose(out);
				}
				Sleep(10000);
				break;
			}
			
			// Delete
			for(int i = 1; i <= HIGH; i++)
			{
				bool full = 1;
				for(int j = 1; j <= LONG; j++)
				{
					if(mp[j][i] == 0)
					{
						full = 0;
						break;
					}
				}
				if(full)
				{
					Goto(1, i);
					SetColor(NORMAL);
					cout << string(LONG * 2, ' ') << flush;
					Sleep(200);
					Goto(1, i);
					SetColor(FINISHED);
					cout << string(LONG * 2, ' ') << flush;
					Sleep(200);
					Goto(1, i);
					SetColor(NORMAL);
					cout << string(LONG * 2, ' ') << flush;
					Sleep(200);
					Goto(1, i);
					SetColor(FINISHED);
					cout << string(LONG * 2, ' ') << flush;
					Sleep(200);
					Goto(1, i);
					SetColor(NORMAL);
					cout << string(LONG * 2, ' ') << flush;
					Sleep(200);
					for(int j = 1; j <= LONG; j++)
					{
						mp[j][i] = 0;
					}
					for(int j = i; j >= 2; j--)
					{
						Goto(1, j);
						for(int k = 1; k <= LONG; k++)
						{
							swap(mp[k][j], mp[k][j - 1]);
							SetColor(mp[k][j] ? FINISHED : NORMAL);
							printf("  ");
						}
					}
					Goto(1, 1);
					for(int k = 1; k <= LONG; k++)
					{
						mp[k][1] = 0;
						SetColor(NORMAL);
						printf("  ");
					}
					score += 5 + rand() % 11;
					c.Goto(33, 0);
					printf("%08d", score);
				}
			}
			
		}
		if(last == 0 && KeyDown(VK_UP))
		{
			curr.rotate();
			sd.flush();// ------------------------------------------------------------
			curr.print();
			last = 1;
		}
		if(!KeyDown(VK_UP))
		{
			last = 0;
		}
		if(tl == 0 && KeyDown(VK_LEFT))
		{
			curr.left();
			sd.flush();// ------------------------------------------------------------
			curr.print();
			tl = S;
		}
		if(!KeyDown(VK_LEFT))
		{
			tl = 0;
		}
		if(tr == 0 && KeyDown(VK_RIGHT))
		{
			curr.right();
			sd.flush();// ------------------------------------------------------------
			curr.print();
			tr = S;
		}
		if(!KeyDown(VK_RIGHT))
		{
			tr = 0;
		}
		ftime(&lastd);
		ts2 = lastd.time * 1000 + lastd.millitm;
		if((ts2 - ts1 >= ttt) || (td == 0 && KeyDown(VK_DOWN)))
		{
			if(curr.down())
			{
				curr.finish();
				sd.finish();//----------------------------------------------------
				change = 1;
				
				block b = blocktype[next]; // erase
				SetColor(NORMAL);
				Goto(17, 2);
				printf("  ");
				Goto(17 + b.b.X, 2 + b.b.Y);
				printf("  ");
				Goto(17 + b.c.X, 2 + b.c.Y);
				printf("  ");
				Goto(17 + b.d.X, 2 + b.d.Y);
				printf("  ");
				
				cur = next;
				next = rand() % 5;
				Sleep(100);
			}
			else
			{
				curr.print();
				td = S;
				ftime(&lastd);
				ts1 = lastd.time * 1000 + lastd.millitm;
			}
		}
		if(!KeyDown(VK_DOWN))
		{
			td = 0;
		}
		if(KeyDown(VK_SPACE))
		{
			curr.erase();
			while(curr.down() == false);
			curr.finish();
			sd.finish();//----------------------------------------------------
			change = 1;
			
			block b = blocktype[next]; // erase
			SetColor(NORMAL);
			Goto(17, 2);
			printf("  ");
			Goto(17 + b.b.X, 2 + b.b.Y);
			printf("  ");
			Goto(17 + b.c.X, 2 + b.c.Y);
			printf("  ");
			Goto(17 + b.d.X, 2 + b.d.Y);
			printf("  ");
			
			cur = next;
			next = rand() % 5;
			Sleep(500);
		}
		if(tl) tl--;
		if(tr) tr--;
		if(td) td--;
		Sleep(1);
	}
	return 0;
}
原文