publish my project

Dependencies:   mbed

Committer:
dongyuhu
Date:
Tue Apr 28 04:39:12 2020 +0000
Revision:
5:c31ba165e49c
Parent:
4:fd47d9c11263
change of function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dongyuhu 0:9e95a5ef2659 1 #include "Start.h"
dongyuhu 0:9e95a5ef2659 2 bool ifnotstop = true;
dongyuhu 0:9e95a5ef2659 3
dongyuhu 0:9e95a5ef2659 4 Start::Start()
dongyuhu 0:9e95a5ef2659 5 {
dongyuhu 0:9e95a5ef2659 6
dongyuhu 0:9e95a5ef2659 7 }
dongyuhu 0:9e95a5ef2659 8
dongyuhu 0:9e95a5ef2659 9 Start::~Start()
dongyuhu 0:9e95a5ef2659 10 {
dongyuhu 0:9e95a5ef2659 11
dongyuhu 0:9e95a5ef2659 12 }
dongyuhu 0:9e95a5ef2659 13
dongyuhu 0:9e95a5ef2659 14 void Start::init(int me_width,int me_height,int attacker_size,int speed)
dongyuhu 0:9e95a5ef2659 15 {
dongyuhu 0:9e95a5ef2659 16 // initialise the game parameters
dongyuhu 0:9e95a5ef2659 17 _me_width = me_width; //3
dongyuhu 0:9e95a5ef2659 18 _me_height = me_height; //3
dongyuhu 0:9e95a5ef2659 19 _attacker_size = attacker_size; //2
dongyuhu 0:9e95a5ef2659 20 _speed = speed; //1
dongyuhu 0:9e95a5ef2659 21
dongyuhu 0:9e95a5ef2659 22
dongyuhu 0:9e95a5ef2659 23
dongyuhu 0:9e95a5ef2659 24 _me.init(_mex,_mey,_me_height,_me_width);
dongyuhu 0:9e95a5ef2659 25
dongyuhu 0:9e95a5ef2659 26 _attacker1.init1(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 27 _attacker2.init2(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 28 _attacker3.init3(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 29 _attacker4.init4(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 30 _attacker5.init5(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 31 _attacker6.init6(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 32 _attacker7.init7(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 33 _attacker8.init8(_attacker_size,_speed);
dongyuhu 0:9e95a5ef2659 34
dongyuhu 0:9e95a5ef2659 35 }
dongyuhu 0:9e95a5ef2659 36
dongyuhu 0:9e95a5ef2659 37 void Start::read_input(Gamepad &pad)
dongyuhu 0:9e95a5ef2659 38 {
dongyuhu 0:9e95a5ef2659 39 _d = pad.get_direction();
dongyuhu 0:9e95a5ef2659 40 _mag = pad.get_mag();
dongyuhu 0:9e95a5ef2659 41 }
dongyuhu 0:9e95a5ef2659 42
dongyuhu 0:9e95a5ef2659 43 void Start::draw(N5110 &lcd)
dongyuhu 0:9e95a5ef2659 44 {
dongyuhu 0:9e95a5ef2659 45
dongyuhu 0:9e95a5ef2659 46 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); //draw the turrets
dongyuhu 0:9e95a5ef2659 47 lcd.drawRect(10,10,64,28,FILL_TRANSPARENT); //map
dongyuhu 0:9e95a5ef2659 48 lcd.drawCircle(5,17,4,FILL_TRANSPARENT);
dongyuhu 0:9e95a5ef2659 49 lcd.drawCircle(5,31,4,FILL_TRANSPARENT); //the two in left
dongyuhu 0:9e95a5ef2659 50 lcd.drawCircle(78,17,4,FILL_TRANSPARENT);
dongyuhu 0:9e95a5ef2659 51 lcd.drawCircle(78,31,4,FILL_TRANSPARENT); //right
dongyuhu 0:9e95a5ef2659 52 lcd.drawCircle(30,5,4,FILL_TRANSPARENT);
dongyuhu 0:9e95a5ef2659 53 lcd.drawCircle(54,5,4,FILL_TRANSPARENT); //up
dongyuhu 0:9e95a5ef2659 54 lcd.drawCircle(30,42,4,FILL_TRANSPARENT);
dongyuhu 0:9e95a5ef2659 55 lcd.drawCircle(54,42,4,FILL_TRANSPARENT); //down
dongyuhu 0:9e95a5ef2659 56
dongyuhu 0:9e95a5ef2659 57 _me.draw(lcd);
dongyuhu 0:9e95a5ef2659 58 _attacker1.draw(lcd);
dongyuhu 0:9e95a5ef2659 59 _attacker2.draw(lcd);
dongyuhu 0:9e95a5ef2659 60 _attacker3.draw(lcd);
dongyuhu 0:9e95a5ef2659 61 _attacker4.draw(lcd);
dongyuhu 0:9e95a5ef2659 62 _attacker5.draw(lcd);
dongyuhu 0:9e95a5ef2659 63 _attacker6.draw(lcd);
dongyuhu 0:9e95a5ef2659 64 _attacker7.draw(lcd);
dongyuhu 0:9e95a5ef2659 65 _attacker8.draw(lcd);
dongyuhu 0:9e95a5ef2659 66
dongyuhu 0:9e95a5ef2659 67 }
dongyuhu 0:9e95a5ef2659 68
dongyuhu 0:9e95a5ef2659 69 void Start::update(Gamepad &pad)
dongyuhu 0:9e95a5ef2659 70 {
dongyuhu 0:9e95a5ef2659 71 _me.update(_d);
dongyuhu 0:9e95a5ef2659 72 _attacker1.update();
dongyuhu 0:9e95a5ef2659 73 _attacker2.update();
dongyuhu 0:9e95a5ef2659 74 _attacker3.update();
dongyuhu 0:9e95a5ef2659 75 _attacker4.update();
dongyuhu 0:9e95a5ef2659 76 _attacker5.update();
dongyuhu 0:9e95a5ef2659 77 _attacker6.update();
dongyuhu 0:9e95a5ef2659 78 _attacker7.update();
dongyuhu 0:9e95a5ef2659 79 _attacker8.update();
dongyuhu 0:9e95a5ef2659 80
dongyuhu 0:9e95a5ef2659 81 check_wall_collision(pad);
dongyuhu 0:9e95a5ef2659 82 check_body_collisions(pad);
dongyuhu 0:9e95a5ef2659 83
dongyuhu 0:9e95a5ef2659 84 }
dongyuhu 5:c31ba165e49c 85 void setpos(Vector2D &attacker_pos,int x,int y)
dongyuhu 3:c89450845b2a 86 {
dongyuhu 5:c31ba165e49c 87 attacker_pos.x = x;
dongyuhu 5:c31ba165e49c 88 attacker_pos.y = y;
dongyuhu 3:c89450845b2a 89 }
dongyuhu 3:c89450845b2a 90
dongyuhu 5:c31ba165e49c 91
dongyuhu 5:c31ba165e49c 92 void JUDGE(Vector2D &attacker11_pos,Vector2D &attacker22_pos,Vector2D &attacker33_pos,Vector2D &attacker44_pos,Vector2D &attacker55_pos,Vector2D &attacker66_pos,Vector2D &attacker77_pos,Vector2D &attacker88_pos)
dongyuhu 3:c89450845b2a 93 {
dongyuhu 4:fd47d9c11263 94 if(attacker11_pos.y < 10||attacker11_pos.y > 36||attacker11_pos.x < 10||attacker11_pos.x > 72)
dongyuhu 3:c89450845b2a 95 setpos(attacker11_pos,30,11);
dongyuhu 3:c89450845b2a 96 else if(attacker22_pos.y < 10||attacker22_pos.y > 36||attacker22_pos.x < 10||attacker22_pos.x > 72)
dongyuhu 3:c89450845b2a 97 setpos(attacker22_pos,54,10);
dongyuhu 3:c89450845b2a 98 else if(attacker33_pos.y < 10||attacker33_pos.y > 36||attacker33_pos.x < 10||attacker33_pos.x > 72)
dongyuhu 3:c89450845b2a 99 setpos(attacker33_pos,10,17);
dongyuhu 3:c89450845b2a 100 else if(attacker44_pos.y < 10||attacker44_pos.y > 36||attacker11_pos.x < 10||attacker44_pos.x > 72)
dongyuhu 3:c89450845b2a 101 setpos(attacker44_pos,10,31);
dongyuhu 3:c89450845b2a 102 else if(attacker55_pos.y < 10||attacker55_pos.y > 36||attacker55_pos.x < 10||attacker55_pos.x > 72)
dongyuhu 3:c89450845b2a 103 setpos(attacker55_pos,30,36);
dongyuhu 3:c89450845b2a 104 else if(attacker66_pos.y < 10||attacker66_pos.y > 36||attacker66_pos.x < 10||attacker66_pos.x >72)
dongyuhu 3:c89450845b2a 105 setpos(attacker66_pos,54,36);
dongyuhu 3:c89450845b2a 106 else if(attacker77_pos.y < 10||attacker77_pos.y > 36||attacker77_pos.x < 10||attacker77_pos.x >72)
dongyuhu 3:c89450845b2a 107 setpos(attacker77_pos,71,31);
dongyuhu 3:c89450845b2a 108 else if(attacker88_pos.y < 10||attacker88_pos.y > 36||attacker88_pos.x < 10||attacker88_pos.x >72)
dongyuhu 5:c31ba165e49c 109 setpos(attacker88_pos,71,17);
dongyuhu 3:c89450845b2a 110 }
dongyuhu 0:9e95a5ef2659 111
dongyuhu 0:9e95a5ef2659 112 void Start::check_wall_collision(Gamepad &pad)
dongyuhu 0:9e95a5ef2659 113 {
dongyuhu 0:9e95a5ef2659 114 Vector2D attacker11_pos = _attacker1.get_pos();
dongyuhu 0:9e95a5ef2659 115 Vector2D attacker22_pos = _attacker2.get_pos();
dongyuhu 0:9e95a5ef2659 116 Vector2D attacker33_pos = _attacker3.get_pos();
dongyuhu 0:9e95a5ef2659 117 Vector2D attacker44_pos = _attacker4.get_pos();
dongyuhu 0:9e95a5ef2659 118 Vector2D attacker55_pos = _attacker5.get_pos();
dongyuhu 0:9e95a5ef2659 119 Vector2D attacker66_pos = _attacker6.get_pos();
dongyuhu 0:9e95a5ef2659 120 Vector2D attacker77_pos = _attacker7.get_pos();
dongyuhu 0:9e95a5ef2659 121 Vector2D attacker88_pos = _attacker8.get_pos();
dongyuhu 5:c31ba165e49c 122 JUDGE(attacker11_pos,attacker22_pos,attacker33_pos,attacker44_pos,attacker55_pos,attacker66_pos,attacker77_pos,attacker88_pos);
dongyuhu 0:9e95a5ef2659 123 _attacker1.set_pos(attacker11_pos);
dongyuhu 0:9e95a5ef2659 124 _attacker2.set_pos(attacker22_pos);
dongyuhu 0:9e95a5ef2659 125 _attacker3.set_pos(attacker33_pos);
dongyuhu 0:9e95a5ef2659 126 _attacker4.set_pos(attacker44_pos);
dongyuhu 0:9e95a5ef2659 127 _attacker5.set_pos(attacker55_pos);
dongyuhu 0:9e95a5ef2659 128 _attacker6.set_pos(attacker66_pos);
dongyuhu 0:9e95a5ef2659 129 _attacker7.set_pos(attacker77_pos);
dongyuhu 0:9e95a5ef2659 130 _attacker8.set_pos(attacker88_pos);
dongyuhu 0:9e95a5ef2659 131
dongyuhu 0:9e95a5ef2659 132 }
dongyuhu 5:c31ba165e49c 133 void set_stop(Vector2D &attacker_pos,int x,int y)
dongyuhu 5:c31ba165e49c 134 {
dongyuhu 5:c31ba165e49c 135 ifnotstop = false;
dongyuhu 5:c31ba165e49c 136 attacker_pos.x = x;
dongyuhu 5:c31ba165e49c 137 attacker_pos.y = y;
dongyuhu 5:c31ba165e49c 138 }
dongyuhu 5:c31ba165e49c 139 bool Judge_for_each_situation(Vector2D &me_pos,Vector2D &attacker_pos)
dongyuhu 5:c31ba165e49c 140 {
dongyuhu 5:c31ba165e49c 141 if ((attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y+1 == me_pos.y)||
dongyuhu 5:c31ba165e49c 142 (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y == me_pos.y)||
dongyuhu 5:c31ba165e49c 143 (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y-1 == me_pos.y)||
dongyuhu 5:c31ba165e49c 144 (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y-2 == me_pos.y)||
dongyuhu 5:c31ba165e49c 145 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y+1 == me_pos.y)||
dongyuhu 5:c31ba165e49c 146 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y == me_pos.y)||
dongyuhu 5:c31ba165e49c 147 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y-1 == me_pos.y)||
dongyuhu 5:c31ba165e49c 148 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y-2 == me_pos.y)||
dongyuhu 5:c31ba165e49c 149 (attacker_pos.x == me_pos.x-1) &&(attacker_pos.y+2 == me_pos.y+1)||
dongyuhu 5:c31ba165e49c 150 (attacker_pos.x == me_pos.x ) &&(attacker_pos.y+2 == me_pos.y+1)||
dongyuhu 5:c31ba165e49c 151 (attacker_pos.x == me_pos.x+1) &&(attacker_pos.y+2 == me_pos.y+1)||
dongyuhu 5:c31ba165e49c 152 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y+2 == me_pos.y+1)||
dongyuhu 5:c31ba165e49c 153 (attacker_pos.x == me_pos.x-1) &&(attacker_pos.y == me_pos.y+2)||
dongyuhu 5:c31ba165e49c 154 (attacker_pos.x == me_pos.x ) &&(attacker_pos.y == me_pos.y+2)||
dongyuhu 5:c31ba165e49c 155 (attacker_pos.x == me_pos.x+1) &&(attacker_pos.y == me_pos.y+2)||
dongyuhu 5:c31ba165e49c 156 (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y == me_pos.y+2))
dongyuhu 5:c31ba165e49c 157 return true;
dongyuhu 5:c31ba165e49c 158 else
dongyuhu 5:c31ba165e49c 159 return false;
dongyuhu 5:c31ba165e49c 160 }
dongyuhu 5:c31ba165e49c 161 void Judge_ifstop(Vector2D &me_pos,Vector2D &attacker11_pos,Vector2D &attacker22_pos,Vector2D &attacker33_pos,Vector2D &attacker44_pos,Vector2D &attacker55_pos,Vector2D &attacker66_pos,Vector2D &attacker77_pos,Vector2D &attacker88_pos)
dongyuhu 5:c31ba165e49c 162 {
dongyuhu 5:c31ba165e49c 163 if(Judge_for_each_situation(me_pos,attacker11_pos))
dongyuhu 5:c31ba165e49c 164 set_stop(attacker11_pos,30,11);
dongyuhu 5:c31ba165e49c 165 else if(Judge_for_each_situation(me_pos,attacker22_pos))
dongyuhu 5:c31ba165e49c 166 set_stop(attacker22_pos,54,10);
dongyuhu 5:c31ba165e49c 167 else if(Judge_for_each_situation(me_pos,attacker33_pos))
dongyuhu 5:c31ba165e49c 168 set_stop(attacker33_pos,10,17);
dongyuhu 5:c31ba165e49c 169 else if(Judge_for_each_situation(me_pos,attacker44_pos))
dongyuhu 5:c31ba165e49c 170 set_stop(attacker44_pos,10,31);
dongyuhu 5:c31ba165e49c 171 else if(Judge_for_each_situation(me_pos,attacker55_pos))
dongyuhu 5:c31ba165e49c 172 set_stop(attacker55_pos,30,38);
dongyuhu 5:c31ba165e49c 173 else if(Judge_for_each_situation(me_pos,attacker66_pos))
dongyuhu 5:c31ba165e49c 174 set_stop(attacker66_pos,54,38);
dongyuhu 5:c31ba165e49c 175 else if(Judge_for_each_situation(me_pos,attacker77_pos))
dongyuhu 5:c31ba165e49c 176 set_stop(attacker77_pos,74,31);
dongyuhu 5:c31ba165e49c 177 else if(Judge_for_each_situation(me_pos,attacker88_pos))
dongyuhu 5:c31ba165e49c 178 set_stop(attacker88_pos,74,17);
dongyuhu 5:c31ba165e49c 179 }
dongyuhu 0:9e95a5ef2659 180
dongyuhu 0:9e95a5ef2659 181
dongyuhu 5:c31ba165e49c 182 void Start::check_body_collisions(Gamepad &pad)
dongyuhu 0:9e95a5ef2659 183 {
dongyuhu 5:c31ba165e49c 184 Vector2D attacker11_pos = _attacker1.get_pos();
dongyuhu 5:c31ba165e49c 185 Vector2D attacker22_pos = _attacker2.get_pos();
dongyuhu 5:c31ba165e49c 186 Vector2D attacker33_pos = _attacker3.get_pos();
dongyuhu 5:c31ba165e49c 187 Vector2D attacker44_pos = _attacker4.get_pos();
dongyuhu 5:c31ba165e49c 188 Vector2D attacker55_pos = _attacker5.get_pos();
dongyuhu 5:c31ba165e49c 189 Vector2D attacker66_pos = _attacker6.get_pos();
dongyuhu 5:c31ba165e49c 190 Vector2D attacker77_pos = _attacker7.get_pos();
dongyuhu 5:c31ba165e49c 191 Vector2D attacker88_pos = _attacker8.get_pos();
dongyuhu 5:c31ba165e49c 192 Vector2D me_pos = _me.get_pos();
dongyuhu 5:c31ba165e49c 193 Judge_ifstop(me_pos,attacker11_pos,attacker22_pos,attacker33_pos,attacker44_pos,attacker55_pos,attacker66_pos,attacker77_pos,attacker88_pos);
dongyuhu 0:9e95a5ef2659 194 _attacker1.set_pos(attacker11_pos);
dongyuhu 0:9e95a5ef2659 195 _attacker2.set_pos(attacker22_pos);
dongyuhu 0:9e95a5ef2659 196 _attacker3.set_pos(attacker33_pos);
dongyuhu 0:9e95a5ef2659 197 _attacker4.set_pos(attacker44_pos);
dongyuhu 0:9e95a5ef2659 198 _attacker5.set_pos(attacker55_pos);
dongyuhu 0:9e95a5ef2659 199 _attacker6.set_pos(attacker66_pos);
dongyuhu 0:9e95a5ef2659 200 _attacker7.set_pos(attacker77_pos);
dongyuhu 5:c31ba165e49c 201 _attacker8.set_pos(attacker88_pos);
dongyuhu 0:9e95a5ef2659 202 }