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
Baby.cpp
00001 #include "Baby.h" 00002 00003 Baby::Baby() 00004 { 00005 00006 } 00007 00008 Baby::~Baby() 00009 { 00010 00011 } 00012 00013 void Baby::init(int x, int y){ 00014 x_char = x + 13; 00015 y_char = y + 40; 00016 } 00017 00018 int Baby::get_x_char() 00019 { 00020 return x_char; 00021 } 00022 00023 int Baby::get_y_char() 00024 { 00025 return y_char; 00026 } 00027 00028 void Baby::drawSprite(N5110 &lcd) 00029 { 00030 lcd.drawSprite(x_char,y_char,7,7,(int *)sprite_01); 00031 } 00032 00033 void Baby::movement(Direction dir, N5110 &lcd) 00034 { 00035 // if any direction and collision not detected then movement occurs // 00036 accel = 1; 00037 if (dir == N && northCollision(x_char, y_char, lcd) == false) { 00038 y_char -= accel; 00039 } else if (dir == S && southCollision(x_char, y_char, lcd) == false) { 00040 y_char += accel; 00041 } else if (dir == E && eastCollision(x_char, y_char, lcd) == false) { 00042 x_char += accel; 00043 } else if (dir == W && westCollision(x_char, y_char, lcd) == false) { 00044 x_char -= accel; 00045 } 00046 } 00047 00048 // each function detects collisions on one of four sides of the sprite // 00049 bool Baby::northCollision(int x, int y, N5110 &lcd) 00050 { 00051 for (int i = 0; i < 7; i++) { 00052 if (lcd.getPixel(x + i,y - 1) == 1) { 00053 return true; 00054 } 00055 } 00056 return false; 00057 } 00058 00059 bool Baby::southCollision(int x, int y, N5110 &lcd) 00060 { 00061 for (int i = 0; i < 7; i++) { 00062 if (lcd.getPixel(x + i,y + 7) == 1) { 00063 return true; 00064 } 00065 } 00066 return false; 00067 } 00068 00069 bool Baby::eastCollision(int x, int y, N5110 &lcd) 00070 { 00071 for (int i = 0; i < 7; i++) { 00072 if (lcd.getPixel(x + 7,y + i) == 1) { 00073 return true; 00074 } 00075 } 00076 return false; 00077 } 00078 00079 bool Baby::westCollision(int x, int y, N5110 &lcd) 00080 { 00081 for (int i = 0; i < 7; i++) { 00082 if (lcd.getPixel(x - 1,y + i) == 1) { 00083 return true; 00084 } 00085 } 00086 return false; 00087 }
Generated on Wed Jul 13 2022 21:10:10 by
