Joe Body / Mbed 2 deprecated ELEC2645_Project_el18jgb

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Heston.cpp Source File

Heston.cpp

00001 #include "Heston.h"
00002 
00003 const int hest [12][11] = 
00004 {
00005     {0,0,1,1,1,1,1,1,1,0,0},
00006     {0,1,0,0,0,0,0,0,0,1,0},
00007     {1,1,1,1,1,1,1,1,1,1,1},
00008     {1,0,1,0,1,0,1,0,1,0,1},
00009     {1,0,1,1,1,0,1,1,1,0,1},
00010     {0,1,0,0,0,1,0,0,0,1,0},
00011     {0,1,0,0,1,1,1,0,0,1,0},
00012     {0,1,0,0,0,0,0,0,0,1,0},
00013     {0,1,0,1,1,1,1,1,0,1,0},
00014     {0,0,1,0,1,1,1,0,1,0,0},
00015     {0,0,1,0,0,0,0,0,1,0,0},
00016     {0,0,0,1,1,1,1,1,0,0,0},
00017     
00018 };
00019 
00020 
00021 Heston::Heston()
00022 {
00023 
00024 }
00025 
00026 Heston::~Heston()
00027 {
00028 
00029 }
00030 
00031 
00032     
00033 
00034 void Heston::init()
00035 {
00036     _y = 24;  // y value on screen is fixed
00037     _x = 42;  // cant be past edge of screen
00038     _height = 11;
00039     _width = 12;
00040     score = 0;
00041     strike = 10;
00042    _speed = 1;  // scale is arbitrary, could be changed in future
00043 }
00044 
00045 void Heston::draw(N5110 &lcd)
00046 {
00047     // draw basket 
00048     
00049     lcd.drawSprite(_x,_y,12,11,(int*)hest);
00050 }
00051 
00052 void Heston::update(Gamepad &pad)
00053 {
00054     
00055     
00056 
00057     // generate random number between 1 and 8: 
00058     d = rand() % 8 + 1;
00059     
00060     
00061     // update x value depending on direction of movement
00062     // North is decrement as origin is at the top-left so decreasing moves up
00063     if (d == 1) {
00064         _y+=_speed;
00065     } else if (d == 2) {
00066         _x+=_speed;
00067         _y+=_speed;
00068     } else if (d == 3) {
00069         _x+=_speed;
00070     } else if (d == 4) {
00071         _x+=_speed;
00072         _y-=_speed;
00073     } else if (d == 5) {
00074         _y-=_speed;
00075     } else if (d == 6) {
00076         _x-=_speed;
00077         _y-=_speed;
00078     } else if (d == 7) {
00079         _x-=_speed;
00080     } else if (d == 8) {
00081         _x-=_speed;
00082         _y+=_speed;
00083     }
00084 
00085     // check the x origin to ensure that the sprite doesn't go off screen
00086     if (_x < 1) {
00087         _x = 1;
00088     }
00089     if (_x > 84 - _width - 1) {
00090         _x = 84 - _width - 1;
00091     }
00092     // check the y origin to ensure that the sprite doesn't go off screen
00093     if (_y < 1) {
00094         _y = 1;
00095     }
00096     if (_y > 48 - _height - 1) {
00097         _y = 48 - _height - 1;
00098     }
00099 }
00100 
00101 Vector2D Heston::get_pos() {
00102     Vector2D p = {_x,_y};
00103     return p;    
00104 }
00105 
00106 void Heston::hit(Gamepad &pad)
00107 {
00108 
00109     pad.led(3,1);
00110     pad.led(6,1);
00111     _x = rand() % 84 + 1;
00112     _y = rand() % 48 + 1;
00113     score+=1;
00114     
00115 }
00116 
00117 void Heston::miss(Gamepad &pad)
00118 {
00119 
00120     pad.led(1,1);
00121     pad.led(4,1);
00122     strike-=1;
00123 
00124     
00125 }
00126 
00127 int Heston::checkscore()
00128 {
00129     return score;
00130 }
00131 
00132 int Heston::checkstrike()
00133 {
00134     return strike;
00135 }
00136 
00137 void Heston::set_speed(int state)
00138 {
00139     if (state == 1){
00140         _speed = (_speed + 1);
00141         if (_speed == 8){
00142             _speed = 7;
00143         }
00144     }
00145     if (state == 0){
00146         _speed = (_speed - 2);
00147         if (_speed <= 0){
00148             _speed = 1;
00149         }
00150     }     
00151 }