跳到正文

个人记录

吃星星stareater

sky_of_stars约 13 分钟
原文
文章信息与版本

文章信息

发布
2024年10月26日
收录
2026年6月30日
更新
2026年11月9日
篇幅
约 13 分钟

版本

  1. v1当前

声明:灵感来自用户@JT_JT,并非完全原创


INTRODUTION

just a boring game eating stars.

TRANSLATION翻译

就是个吃星星的无聊小游戏。WASD移动,吃到100分就赢了。黄色是普通星星,1分,蓝色是超级星星,3分。红色是陷阱,-5分。


CODE

C++
#include<bits/stdc++.h>
#include<windows.h>
//#include<cmd.h>
#include<conio.h>
#include<bitset>
#include<winuser.h>
#include<thread>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define SHAKE 5
using namespace std;
extern const int
BLACK=0,
DARKBLUE=1,DBLUE=1,
GREEN=2,
BLUE=3,
RED=4,
PURPLE=5,
DIRT=6,YELLOWGREY=6,
LIGHTGREY=7,LGREY=7,
GREY=8,DGREY=8,DARKGREY=8,
SKYBLUE=9,SBLUE=9,
LGREEN=10,LIGHTGREEN=10,
CANYON=11,
LRED=12,LIGHTRED=12,BRED=12,BRIGHTRED=12,
LPURPLE=13,LIGHTPURPLE=13,
LYELLOW=14,LIGHTYELLOW=14,
WHITE=15
;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
void ResizeFont(int a)
{
	HMODULE hKernel32=GetModuleHandle("kernel32");
	SetConsoleFont=(PROCSETCONSOLEFONT)GetProcAddress(hKernel32,"SetConsoleFont");
	SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Color(int text,int background)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),text+background*16);
}
void BColor(int background)
{
    if(background==15)Color(0,background);
    else Color(15,background);
}
void BColor(string background)
{
	int len=background.length();
	for(int i=0;i<len;i++)
	{
		if(background[i]>96)background[i]-=32;
	}
	if(background=="BLACK")Color(15,0);
	if(background=="DARKBLUE"||background=="DBLUE")Color(15,1);
	if(background=="GREEN")Color(15,2);
	if(background=="BLUE")Color(15,3);
	if(background=="RED")Color(15,4);
	if(background=="PURPLE")Color(15,5);
	if(background=="DIRT"||background=="YELLOWGREY")Color(15,6);
	if(background=="LIGHTGREY"||background=="LGREY")Color(15,7);
	if(background=="GREY"||background=="DGREY"||background=="DARKGREY")Color(15,8);
	if(background=="SKYBLUE"||background=="SBLUE")Color(15,9);
	if(background=="LGREEN"||background=="LIGHTGREEN")Color(15,10);
	if(background=="CANYON")Color(15,11);
	if(background=="LRED"||background=="LIGHTRED"||background=="BRED"||background=="BRIGHTRED")Color(15,12);
	if(background=="LPURPLE"||background=="LIGHTPURPLE")Color(15,13);
	if(background=="LYELLOW"||background=="LIGHTYELLOW")Color(15,14);
	if(background=="WHITE")Color(0,15);
}
void cls()
{
	BColor(BLACK);
	system("cls");
}
struct CTRL
{
	void SetPointerVisual(bool visual)
	{
		//1=show     0=hide
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		CONSOLE_CURSOR_INFO cci;
		GetConsoleCursorInfo(hOut,&cci);
		cci.bVisible=visual;
		SetConsoleCursorInfo(hOut,&cci);
	}
};
extern CTRL Ctrl;
struct Bas
{
	void Hi()
	{
		cout<<"Hello world!";
	}
};
extern Bas Basic;
void PrintByChar(string str,int t)
{
	int len=str.length();
	for(int i=0;i<len;i++)
	{
		Sleep(double(t/len));
		cout<<str[i];
	}
	cout<<endl;
}
struct BigNum
{
	string num;
	int length()
	{
		return num.length();
	}
	void Print()
	{
		bool m=0;
		for(int i=0;i<length();i++)
		{
			if(num[i]!='0')m=1;
			if(m)cout<<num;
		}
	}
	string operator+(const BigNum& B)
	{
		//string a=this->num;
		string b=B.num;
		return "1";
	};
};
struct TO
{
	template < typename T >
	string String( T& n )
    {
        std::ostringstream stm ;
        stm<<fixed<<setprecision(16)<<n;
        return stm.str() ;
    }
};
extern TO To;
int GetWindowPosX()
{
	RECT rect;
	HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.left;
}
int GetWindowPosY()
{
	RECT rect;
	HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.top;
}
int GetCursorPosX()
{
	POINT pt={0,0};
	GetCursorPos(&pt);
	return pt.x;
}
int GetCursorPosY()
{
	POINT pt={0,0};
	GetCursorPos(&pt);
	return pt.y;
}
int GetSubX()
{
	return GetCursorPosX()-GetWindowPosX();
}
int GetSubY()
{
	return GetCursorPosY()-GetWindowPosY();
}
void gotoxy(int x,int y)
{
	int xx=0x0b;
	HANDLE hOutput;
	COORD loc;
	loc.X=x;
	loc.Y=y;
	hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput,loc);
}
#define land 0
#define star 1
#define superstar 2
#define sstar 3
#define trap 4
using namespace std;
int world[250][250];
long long stars[250][250];
int point=0;
int playerX=75,playerY=75;
long long frame=0;
void setup(){//初始化 
	for(int i=1;i<=200;i++){
		for(int j=1;j<=200;j++){
			world[i][j]=land;
		}
	}
}
void spawn(){//生成星星 
	for(int i=30+1;i<170;i++){
		for(int j=30+1;j<170;j++){
			world[i][j]=land;
			stars[i][j]=0;
			if(world[i][j]==land){
				if(rand()%100==0){
					stars[i][j]=200;
					world[i][j]=star;
				}else if(rand()%300==0){
					stars[i][j]=200;
					world[i][j]=sstar;
				}
			}
		}
	}
}
/*void deletestar(){//删除星星 
	for(int i=30+1;i<170;i++){
		for(int j=30+1;j<170;j++){
			if(world[i][j]==star || world[i][j]==sstar){
				stars[i][j]--;
				if(stars[i][j]==0)world[i][j]=land;
			}
		}
	}
}*/
void trymove(){//移动 
	if(GetKeyState('W')<0 && playerX>30)playerX--;
	if(GetKeyState('S')<0 && playerX<170)playerX++;
	if(GetKeyState('A')<0 && playerY>30)playerY--;
	if(GetKeyState('D')<0 && playerY<170)playerY++;
}
void eatstar(){//吃星星 
	if(world[playerX][playerY]!=land){
		if(world[playerX][playerY]==star){
			point++;
		}else if(world[playerX][playerY]==sstar){
			point+=3;
		}
		world[playerX][playerY]=land;
	}
}
void screenupdate(){//更新屏幕 
	gotoxy(0,0);
	for(int i=playerX-15;i<=playerX+15;i++){
		for(int j=playerY-15;j<=playerY+15;j++){
			if(i==playerX && j==playerY){
				BColor(WHITE);
				cout << "  ";
			}else if(world[i][j]==star){
				BColor(LYELLOW);
				cout << "  ";
			}else if(world[i][j]==sstar){
				BColor(CANYON);
				cout << "  ";
			}else if(world[i][j]==land){
				BColor(DIRT);
				cout << "  ";
			}
		}
		cout << endl;
	}
	BColor(BLACK); 
	cout << "----------------------------------------------------------------------------------------" << endl;
	cout << "X:" << playerX << "  Y:" << playerY << " Debug.frame:" << frame << endl;
	cout << "You have " << point << " points" << endl;
}
int main(){
	system("mode con cols=100 lines=40");
	srand(time(0));
	setup();
	while(1){
		if(point>=100){
			cout << "Congradulations! You won the game!" << endl;
    		cout << "We will update this game monthly. Thanks for playing! Have a nice day!" << endl;
    		return 0;
		}
		frame++;
		if(frame>1000000000)frame=0;
		if(frame%200==0){
			spawn();
		}
		//deletestar();
		eatstar();
		trymove();
		screenupdate();
	}
    return 0;
}

就是个简单小游戏。图个乐子

原文