跳到正文

科技·工程

手把手教你自制赛车游戏

aaron0919约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

这是一个类似于游戏机里的赛车游戏,取名字的时候脑抽取了个 dino…

手把手教你自制赛车游戏

本游戏很简单。你一定能打出来!!!

是一个系列哟!还有二

简介

我们的游戏为控制 $(上下键控制)来躲避前方的障碍,可以自行更改其他常量。

运行界面:

代码
控制台
      #          #
      #
  $              #
      #          #
Score: 23

主体框架(伪代码):

代码
定义角色、场景和各种各样的函数;
主函数
	预处理;
	如果按下键盘:
		处理输入;
	继续运行

大部分游戏都是这样的框架,当然,正常的游戏肯定不止键盘输入,还有其他输入(如鼠标点击等)。我们会使用一个消息队列依次处理。

讲一下重点:

壹

C++
#include <conio.h>
int _kbhit();
int _getch();

实际上 _kbhit() 函数返回的是是否有按键输入,_getch() 返回的是按下键的 ascii 码(瞬间读入,不回显)。

我们搭配起来:

C++
while(true)
{
	if (_kbhit())
	{
		char ch = _getch();
		/* use ch to do sth... */
	}
	/* do another sth... */
}

就可以不等待输入(按回车)做其他事了。

贰

_getch() 可以读入方向键,但要读入两次。

例如:

C++
unsigned char ch;
ch = _getch();
if (ch == 224)
{
	ch = _getch();
	if (ch == 80)
	{
		// 下
	}
	else if (ch == 72)
	{
		// 上
	}
	/* ... */
}

叁

有一个神奇的转义字符,叫做 \033[,是 <esc> 键的转义。

详见 https://www.luogu.com.cn/paste/3e3zs78o。

我们就可以打出来一个移动光标的函数:

C++
void gotoxy(const int &x, const int &y)
{
	printf("\033[%d;%dH", x + 1, y + 1);
}

那么我们想要移动一个角色,我们可以这样:

  1. 将原来的图标填充掉
  2. 在新的地方绘制图标

举例就是:

C++
gotoxy(x, y);
putchar(background[x][y]);
x += new_x;
y += new_y;
gotoxy(x, y);
putchar(shape);

肆

把他们组合在一起。。。

code

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

const int N = 4;      // 行数
const int M = 40;     // 列数
const int FRESH = 75; // 帧数

char background[N + 1][M + 1];

void gotoxy(const int &x, const int &y)
{
	printf("\033[%d;%dH", x + 1, y + 1);
}

struct PointSprite
{
	int x, y;
	char shape;

	PointSprite(const int &_x, const int &_y, const char &_shape) : x(_x), y(_y), shape(_shape)
	{
		gotoxy(x, y);
		putchar(shape);
	}

	void move(int dx, int dy)
	{
		gotoxy(x, y);
		putchar(background[x][y]);
		x += dx;
		y += dy;

		if (y < 0)
		{
			y = 0;
		}
		else if (y >= M)
		{
			y = M - 1;
		}
		if (x < 0)
		{
			x = 0;
		}
		else if (x >= N)
		{
			x = N - 1;
		}
		gotoxy(x, y);
		putchar(shape);
	}

	bool touch(PointSprite other)
	{
		if (x == other.x && y == other.y)
		{
			return true;
		}
		return false;
	}
};

struct Dino : PointSprite
{
	Dino() : PointSprite(1, 3, '$') {}
};

struct Wall : PointSprite
{
	Wall(int _x) : PointSprite(_x, M - 1, '#') {}
};

int main()
{
	memset(background, ' ', sizeof(background));
	printf("\033[?25l\033[2J");
	srand(time(0));
	for (int i = 0; i < N; i++)
	{
		puts(background[i]);
	}
	Dino dino;
	deque<Wall> walls;
	int tick = 0, jian = FRESH;
	while (1)
	{
		_sleep(1000 / FRESH);
		if (_kbhit())
		{
			static unsigned char ch;
			ch = _getch();
			if (ch == 224)
			{
				ch = _getch();
				if (ch == 80)
				{
					dino.move(1, 0);
				}
				else if (ch == 72)
				{
					dino.move(-1, 0);
				}
			}
			else if (ch == 'q')
			{
				break;
			}
		}
		if (tick < jian)
		{
			tick++;
		}
		if (tick == jian)
		{
			static bool vis[N];
			memset(vis, 0, sizeof(bool) * N);
			for (int i = 1; i < N; i++)
			{
				vis[rand() % N] = 1;
			}
			for (int i = 0; i < N; i++)
			{
				if (vis[i])
				{
					walls.push_back(Wall(i));
				}
			}
			tick = 0;
			if (jian > FRESH * 0.1 * N)
			{
				jian = jian * 0.99;
			}
		}
		for (auto &wall : walls)
		{
			wall.move(0, -1);
			if (wall.y == 0)
			{
				gotoxy(wall.x, wall.y);
				putchar(background[wall.x][wall.y]);
				walls.pop_front();
			}
			if (wall.touch(dino))
			{
				printf("You are over...");
				return 0;
			}
		}
		gotoxy(N, 0);
		printf("Score: %lu", clock() * FRESH / 5000);
	}

	return 0;
}

祝您玩的开心!!

原文