跳到正文

休闲·娱乐

一个自制小游戏

lipaopao约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年7月6日
收录
2026年2月5日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define fo(i,s,e) for(int i=s; i<=e; i++)
using namespace std;
using ll=long long;
const int N=105;
char a[N][N];
void ready(int n)
{
	fo(i,1,n)
	{
		fo(j,1,n)
		{
			int tmp=rand()%4+1;
			if(tmp==1)
			{
				a[i][j]='#';
			}
			else
			{
				a[i][j]=' ';
			}
		}
	}
}
void out(int n)
{
	system("cls");
	fo(i,1,n)
	{
		fo(j,1,n)
		{
			cout<<a[i][j];
		}
		cout<<endl;
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	srand(time(0));
	int n;
	cin>>n;
	system("cls");
	ready(n);
	a[1][1]='0';
	a[n][n]='X';
	int x=1,y=1;
	char tmp;
	while(x!=n||y!=n)
	{
		out(n);
		tmp=getch();
		if(tmp=='w'&&x>1
		&&a[x-1][y]!='#')
		{
			a[x][y]=' ';
			x--;
			a[x][y]='0';
		}
		if(tmp=='s'&&x<n
		&&a[x+1][y]!='#')
		{
			a[x][y]=' ';
			x++;
			a[x][y]='0';
		}
		if(tmp=='a'&&y>1
		&&a[x][y-1]!='#')
		{
			a[x][y]=' ';
			y--;
			a[x][y]='0';
		}
		if(tmp=='d'&&y<n
		&&a[x][y+1]!='#')
		{
			a[x][y]=' ';
			y++;
			a[x][y]='0';
		}
		if(tmp=='q')
		{
			fo(i,x-1,x+1)
			{
				fo(j,y-1,y+1)
				{
					a[i][j]='*';
				}
			}
		}
		a[x][y]='0';
		a[n][n]='X';
	}
	system("cls");
	cout<<"YOU WIN";
	return 0;
}
原文