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.
Diff: Paddle/Paddle.cpp
- Revision:
- 0:7d4d2023ed9c
- Child:
- 1:e18615d46071
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Paddle/Paddle.cpp Tue Apr 16 18:58:18 2019 +0000
@@ -0,0 +1,64 @@
+#include "Paddle.h"
+
+// nothing doing in the constructor and destructor
+Paddle::Paddle()
+{
+
+}
+
+Paddle::~Paddle()
+{
+
+}
+
+void Paddle::init(int x,int height,int width)
+{
+ _x = x; // x value on screen is fixed
+ _y = HEIGHT/2 - height/2; // y depends on height of screen and height of paddle
+ _height = height;
+ _width = width;
+ _speed = 1; // default speed
+ _score = 0; // start score from zero
+
+}
+
+void Paddle::draw(N5110 &lcd)
+{
+ // draw paddle in screen buffer.
+ lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
+}
+
+void Paddle::update(Direction d,float mag)
+{
+ _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
+
+ // update y value depending on direction of movement
+ // North is decrement as origin is at the top-left so decreasing moves up
+ if (d == W) {
+ _x-=_speed;
+ } else if (d == E) {
+ _x+=_speed;
+ }
+
+ // check the y origin to ensure that the paddle doesn't go off screen
+ if (_x < 1) {
+ _x = 1;
+ }
+ if (_x > WIDTH - _width - 1) {
+ _x = WIDTH - _width - 1;
+ }
+}
+
+void Paddle::add_score()
+{
+ _score++;
+}
+int Paddle::get_score()
+{
+ return _score;
+}
+
+Vector2D Paddle::get_pos() {
+ Vector2D p = {_x,_y};
+ return p;
+}
\ No newline at end of file