Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Part 2: Create A Clicker Game With Rust

That is half 2 of the sequence, take a look at half one right here



Introduction

Greetings πŸ‘‹!
On this half, we’ll be trying find out how to make the person have higher expertise by including sounds when clicking the circle, and altering it is shade.



Including sound

Macroquad offers us with features for loading sounds and taking part in them. However first, let’s get the sounds that we’d like.
Create a res folder in the primary folder (out facet of src). That is the place we’ll hold the sources for the sport comparable to sounds, pictures and many others.
Now, get your sounds!
You possibly can obtain the sounds that I used here (within the res folder)
Now let’s get coding

We’ll first import the features we’d like:

use macroquad::audio::{play_sound_once,load_sound};
Enter fullscreen mode

Exit fullscreen mode

The features do precisely what they sound like.
load_sound masses the sound, whereas play_sound_once performs the sound as soon as. There’s additionally one other operate play_sound which requires some extra params which permit extra customization.
Let’s load the sounds!

//snip
let click_sound = load_sound("res/click on.wav").await.unwrap();
Enter fullscreen mode

Exit fullscreen mode

load_sound takes in a single argument, the trail to the sound. We use await to attend for that sound to load and unwrap as a result of load_sound returns a End result sort, which has both the sound or FileError.
Now, let’s play the sound for every click on within the circle, add this in your click on detection:

// snip
if circle.overlaps(&mouse_circ){
            rating+=1;
            play_sound_once(click_sound); // add this!
}
Enter fullscreen mode

Exit fullscreen mode

play_sound_once simply takes the sound to play. Fairly Easy proper? Now to check your self, play one other sound if the rating reaches every a number of of 10. If you have not acquired one other sound, use the milestone one within the repository.
This is one in all finishing that.
Load the sound

let milestone_sound = load_sound("res/milestone.wav").await.unwrap();
Enter fullscreen mode

Exit fullscreen mode

Examine if rating is a a number of of 10:

//snip
if  circle.overlaps(&mouse_circ){
           ...
            if rating%10 ==0 {
               play_sound_once(milestone_sound);
            }
}
Enter fullscreen mode

Exit fullscreen mode

And that is it, you’ve got acquired sounds in your recreation!



Altering Colours

This one is fairly easy too.
First lets declare a continuing array of colours which we will use:

const COLORS:[Color;9] = [
   RED,
   GREEN,
   BLUE,
   PURPLE,
   SKYBLUE,
   BLACK,
   WHITE,
   YELLOW,
   PINK,
];
Enter fullscreen mode

Exit fullscreen mode

I’ve used 9, you should utilize how ever many you need.
Then let’s make a shade variable, in order that we will dynamically change the colour.

let mut shade = RED; //RED being the default shade which we'll use now
Enter fullscreen mode

Exit fullscreen mode

Then make as soon as change within the shade argument of our draw_circle operate:

draw_circle(x,y,r,shade);
Enter fullscreen mode

Exit fullscreen mode

Now, on every click on, randomly get a shade and assign the shade variable to it:

//snip
if  circle.overlaps(&mouse_circ){
           ...
           shade = COLORS[rand::gen_range(0,9)]; //use use 9 because the max worth as a result of we have 9 colours in our array
};
Enter fullscreen mode

Exit fullscreen mode

And we’re achieved, we have the colour altering in our recreation too!

This is the ultimate code:

use macroquad::prelude::*;
use macroquad::audio::{play_sound_once,load_sound};

const COLORS:[Color;9] = [
   RED,
   GREEN,
   BLUE,
   PURPLE,
   SKYBLUE,
   BLACK,
   WHITE,
   YELLOW,
   PINK,
];

#[macroquad::main("Clicker Game")]
async fn fundamental() {
   let (x,y) = (screen_width()/2.,screen_height()/2.);
   let r = 70.;
   let circle = Circle::new(x,y,r);
   let mut rating = 0;
   let click_sound = load_sound("res/click on.wav").await.unwrap();
   let milestone_sound = load_sound("res/milestone.wav").await.unwrap();
   let mut shade:Colour = RED;
   loop {
      clear_background(GRAY);

      if is_mouse_button_pressed(MouseButton::Left) {
         let (mouse_x,mouse_y) = mouse_position();
         let mouse_circ = Circle::new(mouse_x,mouse_y,1.);

         if  circle.overlaps(&mouse_circ){
            rating+=1;
            play_sound_once(click_sound);
            shade = COLORS[rand::gen_range(0,9)];
            if rating%10 ==0 {
               play_sound_once(milestone_sound);
            }

         }
      }

      draw_text("Clicker Sport",screen_width()/2.-100.,100.,50.,WHITE);
      draw_text(format!("Clicks: {}",rating).as_str(),screen_width()/2.-100.,500.,50.,WHITE);
      draw_circle(x,y,r,shade);
      next_frame().await;
   }
}
Enter fullscreen mode

Exit fullscreen mode

Hope this sequence helped you in studying about this simple to make use of framework. I severely advocate exploring their official examples which could be discovered of their web site.
Goodbye πŸ‘‹!



Different Helpful Hyperlinks:

Macroquad Official Website
Macroquad docs.rs
Macroquad Github
Half 1

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?