Giving the player a crosshair
Adding a crosshair is easy and only requires one new concept. Add the following highlighted code, and then we can run through it:
// 100 bullets should do Bullet bullets[100]; int currentBullet = 0; int bulletsSpare = 24; int bulletsInClip = 6; int clipSize = 6; float fireRate = 1; // When was the fire button last pressed? Time lastPressed; // Hide the mouse pointer and replace it with crosshair window.setMouseCursorVisible(true); Sprite spriteCrosshair; Texture textureCrosshair = TextureHolder::GetTexture("graphics/crosshair.png"); spriteCrosshair.setTexture(textureCrosshair); spriteCrosshair.setOrigin(25, 25); // The main game loop while (window.isOpen())
First, we call the setMouseCursorVisible
function on our window
object. We then load a Texture
and declare a Sprite
instance and initialize it in the usual way. Furthermore, we set the sprite's origin to its center to make it convenient and simpler to make the bullets fly to the...