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
Ball/Ball.cpp
- Committer:
- KaifK
- Date:
- 2020-05-17
- Revision:
- 10:dbc149804a06
- Parent:
- 9:e6566d09f087
- Child:
- 11:59119247dd68
File content as of revision 10:dbc149804a06:
#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, 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0,0,0,0,1,1,0}, {0,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,1,1,0}, {0,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,1,1,0} }; Ball::Ball(N5110 &lcd){ _lcd = &lcd;} Ball::~Ball() {} void Ball::init(){ _level = 1; _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){ _lcd->drawSprite(x, y, 11, 15, (int*) ball); } void Ball::playShot(int user_input_x, int user_input_y){ _user_input_x = user_input_x; _user_input_y = user_input_y; int ball_sprite = 0; while (_user_input_x < _ball_x || _user_input_y < _ball_y){ display_background(); 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[ball_sprite]); if (_ball_y >= 35){ball_sprite = 1;}//ball moved further else if (_ball_y <= 30){ball_sprite = 2;}//ball is furthest //printf("ball sprite no: %d \n", ball_sprite); //printf("ball y pos = %d \n", _ball_y); wait_ms(100); _lcd->refresh(); _lcd->clear(); } } void Ball::bounceBall(int x_pos, int y_pos, int height, int status){ _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::display_background(){ //draw goal _lcd->drawSprite(7,0,24,70,(int *)goal); level2(); //draw grass _lcd->drawSprite(1,17,7,7,(int *)grass); _lcd->drawSprite(76,17,7,7,(int *)grass); _lcd->drawSprite(67,26,3,3,(int *)grass_small); _lcd->drawSprite(67,26,3,3,(int *)grass_small); _lcd->drawSprite(6,30,3,3,(int *)grass_small); _lcd->drawSprite(72,32,3,3,(int *)grass_small); _lcd->drawSprite(69,43,3,3,(int *)grass_small); _lcd->drawSprite(16,29,3,3,(int *)grass_small); //ground line _lcd->drawLine(0,24,84,24,1); //score card _lcd->drawRect(0,38,15,11,FILL_TRANSPARENT); //power meter _lcd->drawRect(77,27,6,20,FILL_TRANSPARENT); // aim triangle base _lcd->drawLine(WIDTH / 2 + 5, 41, WIDTH / 2 - 5, 41, 1); } void Ball::set_status(bool status){ _status = status; } bool Ball::get_status() { bool val = _status; return val; } void Ball::set_level(int level ){ _level = level; } int Ball::get_level() { int val = _level; return val; } void Ball::level1() { _lcd->drawRect(8, 1, 68, 24, FILL_TRANSPARENT); // goal is completely open } void Ball::level2() { _lcd->drawRect(8, 1, 34, 24, FILL_BLACK); // LEFT HALF BLOCKED } void Ball::level3() { _lcd->drawRect(42, 1, 34, 24, FILL_BLACK); // RIGHT HALF BLOCKED } void Ball::level4() { _lcd->drawRect(30, 1, 22, 24, FILL_BLACK); // MIDDLE BLOCKED } void Ball::level5() { _lcd->drawRect(8, 1, 68, 8, FILL_BLACK); // TOP HALF BLOCKED } void Ball::level6() { _lcd->drawRect(8, 16, 68, 8, FILL_BLACK); // BOTTOM HALF BLOCKED } void Ball::level7() { _lcd->drawRect(8, 1, 68, 24, FILL_BLACK); _lcd->drawRect(25, 8, 30, 14, FILL_WHITE); // CENTRE OPEN } void Ball::level8() { _lcd->drawRect(8, 1, 68, 24, FILL_BLACK); _lcd->drawRect(8, 1, 25, 12, FILL_WHITE); // TOP LEFT CORNER OPEN } void Ball::level9() { _lcd->drawRect(8, 1, 68, 24, FILL_BLACK); _lcd->drawRect(50, 1, 25, 12, FILL_WHITE); // TOP RIGHT CORNER OPEN } void Ball::level10() { _lcd->drawRect(8, 1, 68, 24, FILL_BLACK); _lcd->drawRect(50, 12, 25, 12, FILL_WHITE); _lcd->drawRect(8, 12, 25, 12, FILL_WHITE); // BOTTOM CORNERS OPEN }