ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Coin Class Reference

Coin Class Reference

Coin Class. More...

#include <Coin.h>

Public Member Functions

 Coin ()
 Constructor.
 ~Coin ()
 Destructor.
void init ()
 Initialises Coin object.
void set_coin (int rand_x, int rand_y)
 Sets the Coin coordinates.
int * get_coin_sprite ()
 Gets the Coin sprite.
int get_coin_x ()
 Gets the x coordinate.
int get_coin_y ()
 Gets the y coordinate.
void generate_coin ()
 Generates the coin.

Detailed Description

Coin Class.

Generates a coin for the skateboarder to collect *

Author:
Lewis Wooltorton
Date:
April 2019
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Coin.h"
#include <cstdlib>
#include <ctime>

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad gamepad;
Coin _coin;

int _skater_x;
int _skater_y;
int _player_score;
bool _coin_collision_flag;

int main() {
  _coin.init();
  srand(time(NULL));
  _player_score = 0;
  _skater_x = 50;
  _skater_y = 20;
  while(1) {
    
    // If the skater collides with the coin, set the flag, add 1 to the score 
    // move the coin to a new random position and make a noise on the buzzer.
    if (_skater_x == _coin.get_coin_x() 
      && (_skater_y == _coin.get_coin_y() - 10)) {  
    _coin_collision_flag = true;
    _player_score++;
    _coin.set_coin((rand() % 100),(abs(rand() % 100 - 20)));  // Place coin 
    // on a constrained random position.
    gamepad.tone(1500, 0.05);  // Make collection noise on buzzer.
    wait(0.05);
    gamepad.tone(3000, 0.05);   
    }
    
    // Print the coin.
    lcd.drawSprite(_coin.get_coin_x(),_coin.get_coin_y(),5,5,
                   (int*)_coin.get_coin_sprite());
  }
}

Definition at line 58 of file Coin.h.


Constructor & Destructor Documentation

Coin (  )

Constructor.

Non user specified.

Definition at line 21 of file Coin.cpp.

~Coin (  )

Destructor.

Non user specified.

Definition at line 23 of file Coin.cpp.


Member Function Documentation

void generate_coin (  )

Generates the coin.

Selects the coin sprite

Definition at line 32 of file Coin.cpp.

int * get_coin_sprite (  )

Gets the Coin sprite.

Returns:
The Coin sprite (an integer array)

Definition at line 55 of file Coin.cpp.

int get_coin_x (  )

Gets the x coordinate.

Returns:
The x coordinate of the Coin

Definition at line 64 of file Coin.cpp.

int get_coin_y (  )

Gets the y coordinate.

Returns:
The y coordinate of the Coin

Definition at line 68 of file Coin.cpp.

void init (  )

Initialises Coin object.

Definition at line 25 of file Coin.cpp.

void set_coin ( int  rand_x,
int  rand_y 
)

Sets the Coin coordinates.

Parameters:
rand_xa random number that determines the x coordinate
rand_ya random number that determines if the Coin is generated on the upper or lower platforms

Definition at line 42 of file Coin.cpp.