Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
engine/engine.cpp
- Committer:
- RickYu
- Date:
- 2018-04-19
- Revision:
- 9:d217a636c18d
- Parent:
- 8:4a5e96ed2347
- Child:
- 10:ef01b3076040
File content as of revision 9:d217a636c18d:
#include "engine.h"
engine::engine()
{
}
engine::~engine()
{
}
void engine::init(int speed)
{
// initialise the game parameters
//_speed = speed;
}
void engine::draw(N5110 &lcd)
{
//draw and re-draw rectangle, boom and money on the screen
_rect.draw(lcd);
_boom.draw(lcd);
_money.draw(lcd);
}
void engine::read_input(Gamepad &pad)
{
_d = pad.get_direction();
_mag = pad.get_mag();
}
void engine::update(Gamepad &pad)
{
_rect.update(_d,_mag); //get the position of rect when jopystick moves
//boom and money will keep moving with initial settings
_boom.update();
_money.update();
check_boom_pos(pad);
check_money_pos(pad);
check_rect_pos(pad);
check_boom_collision(pad);
}
void engine::check_money_pos(Gamepad &pad)
{
Vector2D money_pos = _money.get_pos();
if (money_pos.y > 48) {
money_pos.y = 0;
money_pos.x = rand()%84; //the money will back to top with random x position
}
//avoid money out of the screen
if (money_pos.x+4 >= 84) {
money_pos.x = 80;
}
if (money_pos.x <2){
money_pos.x = 3;
}
_money.set_pos(money_pos);
}
void engine::check_boom_pos(Gamepad &pad)
{
Vector2D boom_pos = _boom.get_pos();
if (boom_pos.y > 48) {
boom_pos.y = 0;
boom_pos.x = rand()%84; //boom will back to top with random x position
}
//avoid boom go outside of the screen
if (boom_pos.x >= 82) {
boom_pos.x = 81;
}
if (boom_pos.x <2){
boom_pos.x = 3;
}
_boom.set_pos(boom_pos);
}
void engine::check_rect_pos(Gamepad &pad)
{
Vector2D rect_pos = _rect.get_pos();
//keep the rect moving inside the screen
if (rect_pos.x < 1) {
rect_pos.x = 1;
}
if (rect_pos.x > 74) {
rect_pos.x = 74;
}
_rect.set_pos(rect_pos);
}
void engine::check_boom_collision(Gamepad &pad)
{
Vector2D boom_pos = _boom.get_pos();
Vector2D rect_pos = _rect.get_pos();
Vector2D money_pos = _money.get_pos();
if (
(boom_pos.x >= rect_pos.x))
{
// money_pos.y = 0;
pad.tone(750.0,0.1);
}
_money.set_pos(money_pos);
_boom.set_pos(boom_pos);
_rect.set_pos(rect_pos);
}