Lab1

Dependencies:   mbed C12832

Committer:
kwstasfane1
Date:
Wed Nov 25 08:54:44 2020 +0000
Revision:
11:aa8de6ff4003
Parent:
10:2fabc22afe95
code to be submitted

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kwstasfane1 10:2fabc22afe95 1 /*
kwstasfane1 10:2fabc22afe95 2 * Konstantinos Fane
kwstasfane1 10:2fabc22afe95 3 * kf289@kent.ac.uk
kwstasfane1 10:2fabc22afe95 4 * ID: 20901830
kwstasfane1 10:2fabc22afe95 5 */
kwstasfane1 10:2fabc22afe95 6
kwstasfane1 10:2fabc22afe95 7 #include "mbed.h"
kwstasfane1 10:2fabc22afe95 8 #include "C12832.h" // LCD display
kwstasfane1 10:2fabc22afe95 9 #include "graphics.h"
kwstasfane1 10:2fabc22afe95 10
kwstasfane1 10:2fabc22afe95 11 int numsprite;
kwstasfane1 10:2fabc22afe95 12 int setup_shooter();
kwstasfane1 10:2fabc22afe95 13 int draw_shooter(int, int); //Display the shooter at some point shx
kwstasfane1 10:2fabc22afe95 14 int getnew_shooter(int); // get the new shooter position (X axis)
kwstasfane1 10:2fabc22afe95 15 void draw_sprite(struct sprite *);
kwstasfane1 10:2fabc22afe95 16 void endgame(char);
kwstasfane1 10:2fabc22afe95 17 Bitmap bm ; //Create a bitmap structure
kwstasfane1 10:2fabc22afe95 18
kwstasfane1 10:2fabc22afe95 19 //custom implemented functions and variables
kwstasfane1 10:2fabc22afe95 20 void move_sprite();
kwstasfane1 10:2fabc22afe95 21 void draw_moving_bullet(int, int);
kwstasfane1 10:2fabc22afe95 22 void kill_sprite(struct sprite *);
kwstasfane1 10:2fabc22afe95 23 int bullet_x_pos;
kwstasfane1 10:2fabc22afe95 24 int killed_sprites = 0;
kwstasfane1 10:2fabc22afe95 25 Timer m_t; //to be used for the interrupts that trigger the downward movement
kwstasfane1 10:2fabc22afe95 26
kwstasfane1 10:2fabc22afe95 27
kwstasfane1 10:2fabc22afe95 28 int main()
kwstasfane1 10:2fabc22afe95 29 {
kwstasfane1 10:2fabc22afe95 30
kwstasfane1 10:2fabc22afe95 31 m_t.start(); //initialise the "move_down" timer
kwstasfane1 10:2fabc22afe95 32
kwstasfane1 10:2fabc22afe95 33 int nsxp; // new shooter x position
kwstasfane1 10:2fabc22afe95 34 int osxp; // old shooter x position
kwstasfane1 10:2fabc22afe95 35
kwstasfane1 10:2fabc22afe95 36 //Set up the bitmap parameters for the display objects
kwstasfane1 10:2fabc22afe95 37 bm.xSize = 16; // 16 pixels wide
kwstasfane1 10:2fabc22afe95 38 bm.ySize = 8; // 8 pixels high
kwstasfane1 10:2fabc22afe95 39 bm.Byte_in_Line = 2; //i.e. 16bits
kwstasfane1 10:2fabc22afe95 40
kwstasfane1 10:2fabc22afe95 41 //Initialise the PWM period for the multi colour LED control
kwstasfane1 10:2fabc22afe95 42 r.period(0.001);
kwstasfane1 10:2fabc22afe95 43 //And turn the 2 active (R and G) colours off
kwstasfane1 10:2fabc22afe95 44 r = 1.0; g = 1.0;
kwstasfane1 10:2fabc22afe95 45
kwstasfane1 10:2fabc22afe95 46 // Draw the shooter image in its initial position
kwstasfane1 10:2fabc22afe95 47 // i.e x = 1 and y = 24 the base line
kwstasfane1 10:2fabc22afe95 48 osxp = setup_shooter();
kwstasfane1 10:2fabc22afe95 49
kwstasfane1 10:2fabc22afe95 50 move_sprite();
kwstasfane1 10:2fabc22afe95 51
kwstasfane1 10:2fabc22afe95 52 while(1)
kwstasfane1 10:2fabc22afe95 53 {
kwstasfane1 10:2fabc22afe95 54 // Get the new shooter position
kwstasfane1 10:2fabc22afe95 55 nsxp = getnew_shooter(osxp);
kwstasfane1 10:2fabc22afe95 56
kwstasfane1 10:2fabc22afe95 57 //And draw it
kwstasfane1 10:2fabc22afe95 58 osxp = draw_shooter(nsxp,osxp);
kwstasfane1 10:2fabc22afe95 59 wait(0.02);
kwstasfane1 10:2fabc22afe95 60
kwstasfane1 10:2fabc22afe95 61 if(m_t.read() > 2) //wait for 2 teconds to create interrupt
kwstasfane1 10:2fabc22afe95 62 {
kwstasfane1 10:2fabc22afe95 63 move_sprite();
kwstasfane1 10:2fabc22afe95 64 m_t.reset();
kwstasfane1 10:2fabc22afe95 65 }
kwstasfane1 10:2fabc22afe95 66
kwstasfane1 10:2fabc22afe95 67 //doesnt matter which spritetab entry is cheched, as even if its killed,
kwstasfane1 10:2fabc22afe95 68 //its position will still be incremented
kwstasfane1 10:2fabc22afe95 69 if(spritetab[0].ypos >= 18)
kwstasfane1 10:2fabc22afe95 70 {
kwstasfane1 10:2fabc22afe95 71 endgame(LOSE);
kwstasfane1 10:2fabc22afe95 72 break; //while is terminated here
kwstasfane1 10:2fabc22afe95 73 }
kwstasfane1 10:2fabc22afe95 74
kwstasfane1 10:2fabc22afe95 75 //checks whether all sprites have been killed
kwstasfane1 10:2fabc22afe95 76 if(killed_sprites == 8)
kwstasfane1 10:2fabc22afe95 77 {
kwstasfane1 10:2fabc22afe95 78 endgame(WIN);
kwstasfane1 10:2fabc22afe95 79 break;
kwstasfane1 10:2fabc22afe95 80 }
kwstasfane1 10:2fabc22afe95 81
kwstasfane1 10:2fabc22afe95 82 if(joyC)
kwstasfane1 10:2fabc22afe95 83 {
kwstasfane1 10:2fabc22afe95 84 //Assign the position of the bullet shooting point to be the same as the tip of the shooter
kwstasfane1 10:2fabc22afe95 85 bullet_x_pos = nsxp + 5;
kwstasfane1 10:2fabc22afe95 86 draw_moving_bullet(bullet_x_pos, BASE);
kwstasfane1 10:2fabc22afe95 87
kwstasfane1 10:2fabc22afe95 88 //destroys the allien sprint
kwstasfane1 10:2fabc22afe95 89 if(bullet_x_pos >=1 && bullet_x_pos <=10)
kwstasfane1 10:2fabc22afe95 90 {
kwstasfane1 10:2fabc22afe95 91 //checks if the sprint has been destroyed already
kwstasfane1 10:2fabc22afe95 92 if(spritetab[0].active == 1)
kwstasfane1 10:2fabc22afe95 93 {
kwstasfane1 10:2fabc22afe95 94 spritetab[0].active = 0;
kwstasfane1 10:2fabc22afe95 95 kill_sprite(&spritetab[0]);
kwstasfane1 10:2fabc22afe95 96 killed_sprites++; //counter to keep track of how many sprites are killed, to determine whether the game has been won
kwstasfane1 10:2fabc22afe95 97 }
kwstasfane1 10:2fabc22afe95 98 }
kwstasfane1 10:2fabc22afe95 99
kwstasfane1 10:2fabc22afe95 100 if(bullet_x_pos >=16 && bullet_x_pos <=26)
kwstasfane1 10:2fabc22afe95 101 {
kwstasfane1 10:2fabc22afe95 102 if(spritetab[1].active == 1)
kwstasfane1 10:2fabc22afe95 103 {
kwstasfane1 10:2fabc22afe95 104 spritetab[1].active = 0;
kwstasfane1 10:2fabc22afe95 105 kill_sprite(&spritetab[1]);
kwstasfane1 10:2fabc22afe95 106 killed_sprites++;
kwstasfane1 10:2fabc22afe95 107 }
kwstasfane1 10:2fabc22afe95 108 }
kwstasfane1 10:2fabc22afe95 109
kwstasfane1 10:2fabc22afe95 110 if(bullet_x_pos >=32 && bullet_x_pos <=42) {
kwstasfane1 10:2fabc22afe95 111 if(spritetab[2].active == 1)
kwstasfane1 10:2fabc22afe95 112 {
kwstasfane1 10:2fabc22afe95 113 spritetab[2].active = 0;
kwstasfane1 10:2fabc22afe95 114 kill_sprite(&spritetab[2]);
kwstasfane1 10:2fabc22afe95 115 killed_sprites++;
kwstasfane1 10:2fabc22afe95 116 }
kwstasfane1 10:2fabc22afe95 117 }
kwstasfane1 10:2fabc22afe95 118
kwstasfane1 10:2fabc22afe95 119 if(bullet_x_pos >=48 && bullet_x_pos <=58) {
kwstasfane1 10:2fabc22afe95 120 if(spritetab[3].active == 1)
kwstasfane1 10:2fabc22afe95 121 {
kwstasfane1 10:2fabc22afe95 122 spritetab[3].active = 0;
kwstasfane1 10:2fabc22afe95 123 kill_sprite(&spritetab[3]);
kwstasfane1 10:2fabc22afe95 124 killed_sprites++;
kwstasfane1 10:2fabc22afe95 125 }
kwstasfane1 10:2fabc22afe95 126 }
kwstasfane1 10:2fabc22afe95 127
kwstasfane1 10:2fabc22afe95 128 if(bullet_x_pos >=64 && bullet_x_pos <=74) {
kwstasfane1 10:2fabc22afe95 129 if(spritetab[4].active == 1)
kwstasfane1 10:2fabc22afe95 130 {
kwstasfane1 10:2fabc22afe95 131 spritetab[4].active = 0;
kwstasfane1 10:2fabc22afe95 132 kill_sprite(&spritetab[4]);
kwstasfane1 10:2fabc22afe95 133 killed_sprites++;
kwstasfane1 10:2fabc22afe95 134 }
kwstasfane1 10:2fabc22afe95 135 }
kwstasfane1 10:2fabc22afe95 136
kwstasfane1 10:2fabc22afe95 137 if(bullet_x_pos >=80 && bullet_x_pos <=90) {
kwstasfane1 10:2fabc22afe95 138 if(spritetab[5].active == 1)
kwstasfane1 10:2fabc22afe95 139 {
kwstasfane1 10:2fabc22afe95 140 spritetab[5].active = 0;
kwstasfane1 10:2fabc22afe95 141 kill_sprite(&spritetab[5]);
kwstasfane1 10:2fabc22afe95 142 killed_sprites++;
kwstasfane1 10:2fabc22afe95 143 }
kwstasfane1 10:2fabc22afe95 144 }
kwstasfane1 10:2fabc22afe95 145
kwstasfane1 10:2fabc22afe95 146 if(bullet_x_pos >=96 && bullet_x_pos <=106) {
kwstasfane1 10:2fabc22afe95 147 if(spritetab[6].active == 1)
kwstasfane1 10:2fabc22afe95 148 {
kwstasfane1 10:2fabc22afe95 149 spritetab[6].active = 0;
kwstasfane1 10:2fabc22afe95 150 kill_sprite(&spritetab[6]);
kwstasfane1 10:2fabc22afe95 151 killed_sprites++;
kwstasfane1 10:2fabc22afe95 152 }
kwstasfane1 10:2fabc22afe95 153 }
kwstasfane1 10:2fabc22afe95 154
kwstasfane1 10:2fabc22afe95 155 if(bullet_x_pos >=112 && bullet_x_pos <=123) {
kwstasfane1 10:2fabc22afe95 156 if(spritetab[7].active == 1)
kwstasfane1 10:2fabc22afe95 157 {
kwstasfane1 10:2fabc22afe95 158 spritetab[7].active = 0;
kwstasfane1 10:2fabc22afe95 159 kill_sprite(&spritetab[7]);
kwstasfane1 10:2fabc22afe95 160 killed_sprites++;
kwstasfane1 10:2fabc22afe95 161 }
kwstasfane1 10:2fabc22afe95 162 }
kwstasfane1 10:2fabc22afe95 163 }
kwstasfane1 10:2fabc22afe95 164
kwstasfane1 10:2fabc22afe95 165 }
kwstasfane1 10:2fabc22afe95 166
kwstasfane1 10:2fabc22afe95 167 return 0;
kwstasfane1 10:2fabc22afe95 168 }
kwstasfane1 10:2fabc22afe95 169
kwstasfane1 10:2fabc22afe95 170 //function to print th travelling bullet
kwstasfane1 10:2fabc22afe95 171 void draw_moving_bullet(int bxpos, int bypos)
kwstasfane1 10:2fabc22afe95 172 {
kwstasfane1 10:2fabc22afe95 173 lcd.setmode(XOR); //similar to how draw_sprite works
kwstasfane1 10:2fabc22afe95 174 for( int j=bypos; j>=0; j--) {
kwstasfane1 10:2fabc22afe95 175 lcd.pixel(bxpos, j, 1);
kwstasfane1 10:2fabc22afe95 176 lcd.pixel(bxpos, j-1, 1);
kwstasfane1 10:2fabc22afe95 177 lcd.pixel(bxpos, j-2, 1);
kwstasfane1 10:2fabc22afe95 178 lcd.pixel(bxpos, j-3, 1);
kwstasfane1 10:2fabc22afe95 179 wait(0.02); //wait is introduced so that the bullet is visible in the screen
kwstasfane1 10:2fabc22afe95 180 lcd.copy_to_lcd(); //only one is used, because if a second was used it would delete the bullet drawn already due to the XOR
kwstasfane1 10:2fabc22afe95 181 lcd.pixel(bxpos, j, 1);
kwstasfane1 10:2fabc22afe95 182 lcd.pixel(bxpos, j-1, 1);
kwstasfane1 10:2fabc22afe95 183 lcd.pixel(bxpos, j-2, 1);
kwstasfane1 10:2fabc22afe95 184 lcd.pixel(bxpos, j-3, 1);
kwstasfane1 10:2fabc22afe95 185 }
kwstasfane1 10:2fabc22afe95 186 }
kwstasfane1 10:2fabc22afe95 187
kwstasfane1 10:2fabc22afe95 188 //implementation of sprite kill, is parsed the value of the spritetab[i]
kwstasfane1 10:2fabc22afe95 189 void kill_sprite(struct sprite *ps)
kwstasfane1 10:2fabc22afe95 190 {
kwstasfane1 10:2fabc22afe95 191 //a second print of the same object at the same position deletes the
kwstasfane1 10:2fabc22afe95 192 //object @ spritetab[i]
kwstasfane1 10:2fabc22afe95 193 bm.data = ps->type; //assigning the type of the spritetab[i] to the bitmap variable
kwstasfane1 10:2fabc22afe95 194 lcd.print_bm(bm, ps->xpos, ps->ypos); //prints the created bitmap at the corresponding x,y positions copies the printed data to lcd
kwstasfane1 10:2fabc22afe95 195 }
kwstasfane1 10:2fabc22afe95 196
kwstasfane1 10:2fabc22afe95 197
kwstasfane1 10:2fabc22afe95 198 void move_sprite()
kwstasfane1 10:2fabc22afe95 199 {
kwstasfane1 10:2fabc22afe95 200 for (numsprite = 0; numsprite <= 7; numsprite++) {
kwstasfane1 10:2fabc22afe95 201
kwstasfane1 10:2fabc22afe95 202 spritetab[numsprite].oldy = spritetab[numsprite].ypos;
kwstasfane1 10:2fabc22afe95 203 spritetab[numsprite].ypos++;
kwstasfane1 10:2fabc22afe95 204
kwstasfane1 10:2fabc22afe95 205 //in here it checks to see whether the sprite is active or not,
kwstasfane1 10:2fabc22afe95 206 //if yes it prints it, if not it doesnt print it
kwstasfane1 10:2fabc22afe95 207 if(spritetab[numsprite].active != 0)
kwstasfane1 10:2fabc22afe95 208 {
kwstasfane1 10:2fabc22afe95 209 draw_sprite(&spritetab[numsprite]);
kwstasfane1 10:2fabc22afe95 210 }
kwstasfane1 10:2fabc22afe95 211 }
kwstasfane1 10:2fabc22afe95 212 }
kwstasfane1 10:2fabc22afe95 213
kwstasfane1 10:2fabc22afe95 214 // setup the shooter image at location 1
kwstasfane1 10:2fabc22afe95 215 int setup_shooter()
kwstasfane1 10:2fabc22afe95 216 {
kwstasfane1 10:2fabc22afe95 217 lcd.cls(); //Clear the screen
kwstasfane1 10:2fabc22afe95 218 return (draw_shooter(1,0));
kwstasfane1 10:2fabc22afe95 219 }
kwstasfane1 10:2fabc22afe95 220
kwstasfane1 10:2fabc22afe95 221
kwstasfane1 10:2fabc22afe95 222 // Display the shooter at location shx the Y location (BASE) is always 24
kwstasfane1 10:2fabc22afe95 223 int draw_shooter(int shx, int oldx)//shx = new position , oldx = current (old) position
kwstasfane1 10:2fabc22afe95 224 {
kwstasfane1 10:2fabc22afe95 225 bm.data = shooter; // use the shooter image
kwstasfane1 10:2fabc22afe95 226 lcd.setmode(XOR); // XOR mode a second write in the same place erases the image
kwstasfane1 10:2fabc22afe95 227
kwstasfane1 10:2fabc22afe95 228 if (oldx != 0) {
kwstasfane1 10:2fabc22afe95 229 //first time round
kwstasfane1 10:2fabc22afe95 230 lcd.print_bm(bm, oldx, BASE); // so this erases current shooter
kwstasfane1 10:2fabc22afe95 231 lcd.copy_to_lcd();
kwstasfane1 10:2fabc22afe95 232 }
kwstasfane1 10:2fabc22afe95 233
kwstasfane1 10:2fabc22afe95 234 lcd.print_bm(bm, shx, BASE); // and this draws the new shooter at location shx
kwstasfane1 10:2fabc22afe95 235 lcd.copy_to_lcd(); // update lcd
kwstasfane1 10:2fabc22afe95 236 return shx; // 'current' position returned to become 'old' position
kwstasfane1 10:2fabc22afe95 237
kwstasfane1 10:2fabc22afe95 238 }
kwstasfane1 10:2fabc22afe95 239
kwstasfane1 10:2fabc22afe95 240 /* Get the new shooter X position in response to left or right
kwstasfane1 10:2fabc22afe95 241 joystick movements stops at limits x = 1 and x = 112 */
kwstasfane1 10:2fabc22afe95 242 int getnew_shooter(int oldx)
kwstasfane1 10:2fabc22afe95 243 {
kwstasfane1 10:2fabc22afe95 244 int tmpx = oldx;
kwstasfane1 10:2fabc22afe95 245 if (joyL) tmpx--;
kwstasfane1 10:2fabc22afe95 246 if (tmpx <= 1) tmpx = 1;
kwstasfane1 10:2fabc22afe95 247 if (joyR) tmpx++;
kwstasfane1 10:2fabc22afe95 248 if (tmpx >= 112) tmpx = 112;
kwstasfane1 10:2fabc22afe95 249 return tmpx;
kwstasfane1 10:2fabc22afe95 250 }
kwstasfane1 10:2fabc22afe95 251
kwstasfane1 10:2fabc22afe95 252 // Determine sprite 1 or 2 and display
kwstasfane1 10:2fabc22afe95 253 void draw_sprite(struct sprite *ps)
kwstasfane1 10:2fabc22afe95 254 {
kwstasfane1 10:2fabc22afe95 255
kwstasfane1 10:2fabc22afe95 256 bm.data = ps->type; // use sprite image as defined by type
kwstasfane1 10:2fabc22afe95 257 lcd.setmode(XOR); // XOR mode a second write in the same place erases the image
kwstasfane1 10:2fabc22afe95 258
kwstasfane1 10:2fabc22afe95 259 if (ps->oldy != 0){ // first time round
kwstasfane1 10:2fabc22afe95 260 lcd.print_bm(bm, ps->xpos, ps->oldy); // so this erases current sprite
kwstasfane1 10:2fabc22afe95 261 lcd.copy_to_lcd();
kwstasfane1 10:2fabc22afe95 262 }
kwstasfane1 10:2fabc22afe95 263
kwstasfane1 10:2fabc22afe95 264 lcd.print_bm(bm, ps->xpos, ps->ypos); // and this draws the new sprite at location
kwstasfane1 10:2fabc22afe95 265 lcd.copy_to_lcd(); // update lcd
kwstasfane1 10:2fabc22afe95 266 }
kwstasfane1 10:2fabc22afe95 267
kwstasfane1 10:2fabc22afe95 268 void endgame( char result) // LED == GREEN for WIN
kwstasfane1 10:2fabc22afe95 269 {
kwstasfane1 10:2fabc22afe95 270 if(result == WIN)
kwstasfane1 10:2fabc22afe95 271 {
kwstasfane1 10:2fabc22afe95 272 lcd.cls();
kwstasfane1 10:2fabc22afe95 273 lcd.locate(20,12);
kwstasfane1 10:2fabc22afe95 274 lcd.printf("*** YOU WON ***");
kwstasfane1 10:2fabc22afe95 275
kwstasfane1 10:2fabc22afe95 276 r = 1.0;
kwstasfane1 10:2fabc22afe95 277 g = 0.0;
kwstasfane1 10:2fabc22afe95 278 }
kwstasfane1 10:2fabc22afe95 279 else
kwstasfane1 10:2fabc22afe95 280 {
kwstasfane1 10:2fabc22afe95 281 lcd.cls();
kwstasfane1 10:2fabc22afe95 282 lcd.locate(17,12);
kwstasfane1 10:2fabc22afe95 283 lcd.printf("*** GAME OVER ***");
kwstasfane1 10:2fabc22afe95 284
kwstasfane1 10:2fabc22afe95 285 r = 0.0;
kwstasfane1 10:2fabc22afe95 286 g = 1.0;
kwstasfane1 10:2fabc22afe95 287 }
kwstasfane1 10:2fabc22afe95 288
kwstasfane1 10:2fabc22afe95 289 wait(2);
kwstasfane1 10:2fabc22afe95 290 lcd.cls();
kwstasfane1 10:2fabc22afe95 291 lcd.locate(13,1);
kwstasfane1 10:2fabc22afe95 292 lcd.printf("This game was made by");
kwstasfane1 10:2fabc22afe95 293 lcd.locate(24,11);
kwstasfane1 10:2fabc22afe95 294 lcd.printf("Konstantinos Fane");
kwstasfane1 10:2fabc22afe95 295 lcd.locate(25,21);
kwstasfane1 10:2fabc22afe95 296 lcd.printf("kf289@kent.ac.uk");
kwstasfane1 10:2fabc22afe95 297 }