Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Sun Apr 19 17:46:57 2020 +0000
Revision:
8:dd1037c5435b
Parent:
7:0af4ced868f5
Child:
9:1ddb8dc93e48
Added GameEnigne class to run the different parts of the game and tidied up code.

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