ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_PROJECT_EL18KK

Dependencies:   mbed

Ball/Ball.cpp

Committer:
KaifK
Date:
2020-05-16
Revision:
5:ed71996c0534
Parent:
4:08a0ff6668df
Child:
8:5ede90f99a27

File content as of revision 5:ed71996c0534:

#include "Ball.h"

const int ball[3][11][15] = {
        {
        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0 },
        { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },
        { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0 },
        { 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0 },
        { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0 },
        { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },
        { 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }
    },

    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
        { 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0 },
        { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
        { 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    },

    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    },
};

Ball::Ball() {}

Ball::~Ball() {}

void Ball::init(N5110 & lcd){
    _ball_x = BALL_INIT_X;
    _ball_y = BALL_INIT_Y;
    lcd.drawSprite(_ball_x, _ball_y, 11, 15, (int*) ball);
}

void Ball::setLocation(int x, int y, N5110 &lcd){
    lcd.drawSprite(x, y, 11, 15, (int*) ball);
}

void Ball::playShot(int user_input_x, int user_input_y, N5110 &lcd){
    _user_input_x = user_input_x;
    _user_input_y = user_input_y;
    int i = 0;
    int kar = 0;
    while (_user_input_x < _ball_x || _user_input_y < _ball_y){
        if (_user_input_x < _ball_x){_ball_x -= 2.5;}
        else if (_user_input_x > _ball_x){_ball_x += 2.5;}
        if (_user_input_y < _ball_y){_ball_y -= 2.5;}
        else if (_user_input_y > _ball_y){_ball_y += 2.5;}
        lcd.drawSprite(_ball_x, _ball_y, 11, 15, (int*) ball[kar]);
        if (i == 4){kar = 0;}
        else if (i == 6){kar = 1;}
        else if (i == 8){kar = 2;}
        printf("%d \n", i);
        i++;
        wait_ms(100);

        lcd.refresh();
    }
}

void Ball::bounceBall(int x_pos, int y_pos, int height, int status, N5110 &lcd){
    _user_input_x = x_pos;
    _user_input_y = y_pos;
    _height = height;
    _status = status;
    int dir = -2;
    if (_status){
        lcd.drawSprite(_user_input_x, _user_input_y, 11, 15, (int*) ball[0]);
        if (_user_input_y <= (y_pos - _height)){dir = 3;} //falls faster 
        else if (_user_input_y >= (y_pos)){dir = -2;}
        _user_input_y += dir;
        wait(0.1);
        lcd.refresh();
        //clears background only in the area the ball is moving
        lcd.drawRect(_user_input_x, (48 - y_pos), 13, (y_pos + _height), FILL_WHITE);
    }
}

void Ball::set_status(bool status){_status = status;}

int Ball::get_status() {
    int val = _status;
    return val;
}