Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Sat Apr 18 16:36:52 2020 +0000
Revision:
7:0af4ced868f5
Parent:
6:12e8433382b3
Child:
8:dd1037c5435b
A random map now is drawn on the screen and the map moves with joystick input making it look like the spaceship is moving through the map.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 6:12e8433382b3 1 #include "Map.h"
evanso 7:0af4ced868f5 2 #define MAP_TERRIAN_Y_POSITION 42
evanso 6:12e8433382b3 3
evanso 7:0af4ced868f5 4 AnalogIn adc(PTD5);
evanso 7:0af4ced868f5 5 Serial usb(USBTX, USBRX);
evanso 6:12e8433382b3 6 Map::Map() {
evanso 6:12e8433382b3 7
evanso 6:12e8433382b3 8 }
evanso 6:12e8433382b3 9
evanso 6:12e8433382b3 10 Map::~Map() {
evanso 6:12e8433382b3 11
evanso 6:12e8433382b3 12 }
evanso 6:12e8433382b3 13
evanso 6:12e8433382b3 14 void Map::init() {
evanso 7:0af4ced868f5 15 position_x_map_ = -96;
evanso 7:0af4ced868f5 16 position_y_map_ = MAP_TERRIAN_Y_POSITION;
evanso 7:0af4ced868f5 17 get_random_hight_array();
evanso 7:0af4ced868f5 18 get_random_length_array();
evanso 7:0af4ced868f5 19 //usb.printf("randome seed = %f\n", rand_seed_);
evanso 6:12e8433382b3 20 }
evanso 7:0af4ced868f5 21
evanso 7:0af4ced868f5 22 void Map::get_random_hight_array(){
evanso 7:0af4ced868f5 23 srand(adc.read()*64000);
evanso 7:0af4ced868f5 24 for(int i = 0; i < 12; i++){
evanso 7:0af4ced868f5 25 rand_hights_[i]= rand() % 8 + 5;
evanso 7:0af4ced868f5 26 }
evanso 7:0af4ced868f5 27 //printf loop to check correct random numbers are genreated
evanso 7:0af4ced868f5 28 //for (int i = 0; i < 12; i++){
evanso 7:0af4ced868f5 29 //usb.printf("map hight random array = %d\n", rand_hights_[i]);
evanso 7:0af4ced868f5 30 //}
evanso 7:0af4ced868f5 31 }
evanso 7:0af4ced868f5 32
evanso 7:0af4ced868f5 33 void Map::get_random_length_array(){
evanso 7:0af4ced868f5 34 srand(adc.read()*64000);
evanso 7:0af4ced868f5 35 for(int i = 0; i < 12; i++){
evanso 7:0af4ced868f5 36 rand_lengths_[i]= rand() % 16+ 15;
evanso 7:0af4ced868f5 37 }
evanso 7:0af4ced868f5 38 }
evanso 6:12e8433382b3 39
evanso 7:0af4ced868f5 40 void Map::draw_triangle(N5110 &lcd,int triangle_hight){
evanso 7:0af4ced868f5 41 // draws triangle by drawing two lines with one line having negative gadient
evanso 7:0af4ced868f5 42 lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,1);
evanso 7:0af4ced868f5 43 lcd.drawLine(position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,position_x_map_ + 2*triangle_hight,position_y_map_,1);
evanso 7:0af4ced868f5 44 position_x_map_ = position_x_map_ + 2*triangle_hight,position_y_map_;
evanso 7:0af4ced868f5 45 }
evanso 7:0af4ced868f5 46
evanso 7:0af4ced868f5 47 void Map::draw_line(N5110 &lcd,int line_length){
evanso 7:0af4ced868f5 48 // draws triangle by drawing two lines with one line having negative gadient
evanso 7:0af4ced868f5 49 lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1);
evanso 7:0af4ced868f5 50 position_x_map_ = position_x_map_ + line_length;
evanso 7:0af4ced868f5 51 }
evanso 7:0af4ced868f5 52
evanso 7:0af4ced868f5 53 void Map::draw_map(N5110 &lcd, int move_map){
evanso 7:0af4ced868f5 54 usb.printf("position_x_map_ = %d\n", position_x_map_);
evanso 7:0af4ced868f5 55 usb.printf("move map = %d\n", move_map);
evanso 7:0af4ced868f5 56 int reset_position_x_map_to_origonal = position_x_map_;
evanso 7:0af4ced868f5 57 for(int i = 0; i < 12; i++){
evanso 7:0af4ced868f5 58 draw_triangle(lcd,rand_hights_[i]);
evanso 7:0af4ced868f5 59 draw_line(lcd,rand_lengths_[i]);
evanso 7:0af4ced868f5 60 }
evanso 7:0af4ced868f5 61 position_x_map_ = reset_position_x_map_to_origonal + move_map;
evanso 7:0af4ced868f5 62 }
evanso 7:0af4ced868f5 63
evanso 7:0af4ced868f5 64 int Map::get_position_x_map(){
evanso 7:0af4ced868f5 65 return position_x_map_;
evanso 7:0af4ced868f5 66 }
evanso 7:0af4ced868f5 67
evanso 7:0af4ced868f5 68 void Map::clear_map(N5110 &lcd){
evanso 7:0af4ced868f5 69 // Clears spaceship from LCD by drawing a white sprite
evanso 7:0af4ced868f5 70 lcd.drawRect(0, 0, 84, 48 , FILL_WHITE);
evanso 7:0af4ced868f5 71 }