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: Character/Character.cpp
- Revision:
- 2:c25ec0da7636
- Child:
- 3:fcc9cf213a61
diff -r a52187d01a78 -r c25ec0da7636 Character/Character.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Character/Character.cpp Sun May 17 12:34:15 2020 +0000
@@ -0,0 +1,112 @@
+#include "Character.h"
+#include <Bitmap.h>
+Serial pcc(USBTX, USBRX);
+// nothing doing in the constructor and destructor
+Character::Character()
+{
+
+}
+
+Character::~Character()
+{
+
+}
+
+void Character::init(int x,int y)
+{
+ _x = x; // initial x
+ _y = y; // initial y
+ _speed = 1; // default speed
+ _level = 0; // start score from zero
+}
+
+void Character::draw(N5110 &lcd)
+{ //sprite is facing left
+ static int sprite_data[] = {
+ 0,1,1,1,1,
+ 1,1,1,1,1,
+ 1,1,1,1,1,
+ 1,1,1,1,1,
+ 0,1,1,1,1
+ };
+ //turning the sprite
+ if (_dir == 0) {
+ for(int i = 0; i < 25; i++){
+ sprite_data[i] = 1;
+ }
+ sprite_data[0] = 0;
+ sprite_data[4] = 0;
+ } else if (_dir == 1) {
+ for(int i = 0; i < 25; i++){
+ sprite_data[i] = 1;
+ }
+ sprite_data[4] = 0;
+ sprite_data[24] = 0;
+ } else if (_dir == 2) {
+ for(int i = 0; i < 25; i++){
+ sprite_data[i] = 1;
+ }
+ sprite_data[20] = 0;
+ sprite_data[24] = 0;
+ } else if (_dir == 3) {
+ for(int i = 0; i < 25; i++){
+ sprite_data[i] = 1;
+ }
+ sprite_data[0] = 0;
+ sprite_data[20] = 0;
+ }
+ // Instantiate the Bitmap
+ Bitmap sprite(sprite_data, 5, 5);
+
+ // Rendered at X and Y
+ sprite.render(lcd, _x, _y);
+}
+
+void Character::update(Direction d,float mag)
+{
+ _speed = int(mag*2.0f); //scale of speed
+
+ //printf statements with char to convert
+ pcc.printf("speed = %d \n", _speed);
+
+ // update x and y value depending on direction of movement
+ // Set direction and speed according to north south directions
+ if (d == N) {
+ _y-=_speed;
+ _dir = 0;
+ } else if (d == S) {
+ _y+=_speed;
+ _dir = 2;
+ } else if (d == E) {
+ _x+=_speed;
+ _dir = 1;
+ } else if (d == W) {
+ _x-=_speed;
+ _dir = 3;
+ }
+ //testing _x and _y
+ pcc.printf("x = %d \n", _x);
+ pcc.printf("y = %d \n", _y);
+
+ // check the x and y position] to ensure that the paddle doesn't go off screen
+ if (_x < 1) {
+ _x = 1;
+ }
+ if (_y < 1) {
+ _y = 1;
+ }
+ if (_x > 79){
+ _x = 79;
+ }
+ if (_y > 43){
+ _y = 43;
+ }
+ }
+void Character::level_up()
+{ //Mutator function for leveling up
+ _level++;
+}
+int Character::get_level()
+{ //Accessor function for level
+ return _level;
+}