Let's make 16 games in C++(八):CarRacing (Top Down)

1、准备游戏需要的背景

 

2、渲染背景和移动

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

int main()
{
	RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
	app.setFramerateLimit(60);

	Texture t1, t2;
	t1.loadFromFile("./Resources/images/background.png");
	t2.loadFromFile("./Resources/images/car.png");

	Sprite sBackground(t1);
	Sprite sCar(t2);
	sCar.setPosition(300, 300);


	float x = 300;
	float y = 300;
	float speed = 0;
	float angle = 0;
	float maxSpeed = 12.0;
	float acc = 0.2;
	float dec = 0.3;
	float turnSpeed = 0.08;

	while (app.isOpen())
	{
		Event e;
		while (app.pollEvent(e))
		{
			if (e.type == Event::Closed)
			{
				app.close();
			}
		}
		bool Up = false;
		bool Right = false;
		bool Down = false;
		bool Left = false;
		if (Keyboard::isKeyPressed(Keyboard::Up))
		{
			Up = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Right))
		{
			Right = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Down))
		{
			Down = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Left))
		{
			Left = true;
		}

		//car movement
		if (Up && speed < maxSpeed)
		{
			if (speed < 0)
			{
				speed += dec;
			}
			else
			{
				speed += acc;
			}
		}
		if (Down && speed > -maxSpeed)
		{
			if (speed > 0)
			{
				speed -= dec;
			}
			else
			{
				speed -= acc;
			}
		}
		if (!Up && !Down)
		{
			if (speed - dec > 0)
			{
				speed -= dec;
			}
			else if(speed + dec < 0)
			{
				speed += dec;
			}
			else
			{
				speed = 0;
			}
		}
		if (Right && speed != 0)
		{
			angle += turnSpeed * speed / maxSpeed;
		}
		if (Left && speed != 0)
		{
			angle -= turnSpeed * speed / maxSpeed;
		}
		x += sin(angle) * speed;
		y -= cos(angle) * speed;


		//draw
		app.clear(Color::White);
		app.draw(sBackground);

		sCar.setPosition(x, y);
		sCar.setRotation(angle * 180 / 3.141592);
		sCar.setColor(Color::Red);
		app.draw(sCar);

		app.display();

	}

	return 0;
}

3、增加NPC和修改行驶角度

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;


const int num = 8; //checkpoints
int points[num][2] = { 300, 610,
					  1270,430,
					  1380,2380,
					  1900,2460,
					  1970,1700,
					  2550,1680,
					  2560,3150,
					  500, 3300 };
struct Car
{
	float x;
	float y;
	float speed;
	float angle;
	int n;

	Car()
	{
		speed = 2;
		angle = 0;
		n = 0;
	}
	void move()
	{
		x += sin(angle) * speed;
		y -= cos(angle) * speed;
	}
	void findTarget()
	{
		float tx = points[n][0];
		float ty = points[n][1];
		float beta = angle - atan2(tx - x, -ty + y);
		if (sin(beta) < 0) angle += 0.005*speed; else angle -= 0.005*speed;
		if ((x - tx)*(x - tx) + (y - ty)*(y - ty) < 25 * 25) n = (n + 1) % num;
	}
};



int main()
{
	RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
	app.setFramerateLimit(60);

	Texture t1, t2;
	t1.loadFromFile("./Resources/images/background.png");
	t2.loadFromFile("./Resources/images/car.png");
	t1.setSmooth(true);
	t2.setSmooth(true);

	Sprite sBackground(t1);
	Sprite sCar(t2);
	sBackground.scale(2, 2);

	sCar.setOrigin(22, 22);
	float R = 22;

	const int N = 5;
	Car car[N];
	for (int i = 0; i < N; i++)
	{
		car[i].x = 300 + i * 50;
		car[i].y = 1700 + i * 80;
		car[i].speed = 7 + i;
	}



	float speed = 0;
	float angle = 0;
	float maxSpeed = 12.0;
	float acc = 0.2;
	float dec = 0.3;
	float turnSpeed = 0.08;

	int offsetX = 0;
	int offsetY = 0;

	while (app.isOpen())
	{
		Event e;
		while (app.pollEvent(e))
		{
			if (e.type == Event::Closed)
			{
				app.close();
			}
		}
		bool Up = false;
		bool Right = false;
		bool Down = false;
		bool Left = false;
		if (Keyboard::isKeyPressed(Keyboard::Up))
		{
			Up = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Right))
		{
			Right = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Down))
		{
			Down = true;
		}
		if (Keyboard::isKeyPressed(Keyboard::Left))
		{
			Left = true;
		}

		//car movement
		if (Up && speed < maxSpeed)
		{
			if (speed < 0)
			{
				speed += dec;
			}
			else
			{
				speed += acc;
			}
		}
		if (Down && speed > -maxSpeed)
		{
			if (speed > 0)
			{
				speed -= dec;
			}
			else
			{
				speed -= acc;
			}
		}
		if (!Up && !Down)
		{
			if (speed - dec > 0)
			{
				speed -= dec;
			}
			else if(speed + dec < 0)
			{
				speed += dec;
			}
			else
			{
				speed = 0;
			}
		}
		if (Right && speed != 0)
		{
			angle += turnSpeed * speed / maxSpeed;
		}
		if (Left && speed != 0)
		{
			angle -= turnSpeed * speed / maxSpeed;
		}
		car[0].speed = speed;
		car[0].angle = angle;


		for (int i = 0; i < N; i++)
		{
			car[i].move();
		}
		for (int i = 0; i < N; i++)
		{
			car[i].findTarget();
		}

		//collision
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < N; j++)
			{
				int dx = 0;
				int dy = 0;
				while (dx * dx + dy * dy < 4*R*R)
				{
					car[i].x += dx / 10;
					car[i].y += dy / 10;
					car[j].x -= dx / 10;
					car[j].y -= dy / 10;
					dx = car[i].x - car[j].x;
					dy = car[i].y - car[j].y;
					if (!dx && !dy)
					{
						break;
					}
				}
			}
		}
		app.clear(Color::White);

		if (car[0].x > 320)
		{
			offsetX = car[0].x - 320;
		}
		if (car[0].y > 240)
		{
			offsetY = car[0].y - 240;
		}

		//draw
		sBackground.setPosition(-offsetX, -offsetY);
		app.draw(sBackground);

		Color colors[10] = { Color::Red, Color::Green, Color::Magenta, Color::Blue, Color::White };
		for (int i = 0; i < N; i++)
		{
			sCar.setPosition(car[i].x - offsetX, car[i].y - offsetY);
			sCar.setRotation(car[i].angle * 180 / 3.141592);
			sCar.setColor(colors[i]);
			app.draw(sCar);
		}

		app.display();

	}

	return 0;
}

结果图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值