Bingshuo Chen
/
Monster_copy_copy
27
Diff: Monster/monster.cpp
- Revision:
- 0:ef959ad27a16
- Child:
- 1:48a028c4089e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Monster/monster.cpp Thu Apr 08 02:09:25 2021 +0000 @@ -0,0 +1,233 @@ +#include "monster.h" + +Monster::Monster() {} + +void Monster::monster_main(N5110 &lcd, DigitalIn &button_A, DigitalIn &button_B, DigitalIn &button_C, DigitalIn &button_D) { + + //设置人物初始位置 + monster_x0 = 4; + monster_y0 = 19; + + //设置反派位置 + monster_x1 = 23; + monster_y1 = 19; + turn1 = 0; + + monster_x2 = 18; + monster_y2 = 37; + turn2 = 0; + + monster_x3 = 68; + monster_y3 = 37; + turn3 = 0; + + //开启宝石 + jewels_on1 = 1; + jewels_on2 = 1; + jewels_on3 = 1; + jewels_on4 = 1; + jewels_on5 = 1; + jewels_on6 = 1; + jewels_on7 = 1; + + while(1) { + + lcd.clear(); + + //画背景 + this->drawbackground1(lcd); + this->drawlifes(lcd); + this->drawscores(lcd); + + //画宝石和反派 + this->draw_jewels(lcd); + this->draw_blackmonsters(lcd); + + //画人物 + this->draw_monster(lcd, button_A, button_B, button_C, button_D); + + lcd.refresh(); + + thread_sleep_for(20); + + } +} + + +void Monster::drawbackground1(N5110 &lcd) { + + lcd.drawLine(0,8,84,8,2); + lcd.drawRect(23,26,26,4,FILL_BLACK); + lcd.drawRect(0,44,84,4,FILL_BLACK); + lcd.drawRect(28,40,26,4,FILL_BLACK); + lcd.drawRect(67,22,17,11,FILL_TRANSPARENT); + +} + +void Monster::drawlifes(N5110 &lcd) { + + for(int i=1; i<=lifes; i++) { + + lcd.drawSprite(7*i-4,2,4,5,(int *)shapHeart); + + } + +} + +void Monster::drawscores(N5110 &lcd) { + + char score_buffer[14]; + sprintf(score_buffer,"%d",score); + lcd.printString(score_buffer,60,0); + +} + +//画反派 +void Monster::draw_blackmonsters(N5110 &lcd) { + + + //画反派1 + if (turn1 == 0) { + + monster_x1 += 1; + lcd.drawSprite(monster_x1,monster_y1,7,7,(int *)blackMonster); + + if (monster_x1 >= 43) { + turn1 = 1; + } + + } + + if (turn1 == 1) { + + monster_x1 -= 1; + lcd.drawSprite(monster_x1,monster_y1,7,7,(int *)blackMonster); + + if (monster_x1 <= 23) { + turn1 = 0; + } + + } + + //画反派2 + if (turn2 == 0) { + + monster_x2 += 1; + lcd.drawSprite(monster_x2,monster_y2,7,7,(int *)blackMonster); + + if (monster_x2 >= 20) { + turn2 = 1; + } + + } + + if (turn2 == 1) { + + monster_x2 -= 1; + lcd.drawSprite(monster_x2,monster_y2,7,7,(int *)blackMonster); + + if (monster_x2 <= 1) { + turn2 = 0; + } + + } + + //画反派3 + if (turn3 == 0) { + + monster_x3 += 1; + lcd.drawSprite(monster_x3,monster_y3,7,7,(int *)blackMonster); + + if (monster_x3 >= 75) { + turn3 = 1; + } + + } + + if (turn3 == 1) { + + monster_x3 -= 1; + lcd.drawSprite(monster_x3,monster_y3,7,7,(int *)blackMonster); + + if (monster_x3 <= 56) { + turn3 = 0; + } + + } + +} + +//画宝石 +void Monster::draw_jewels(N5110 &lcd) { + + if (jewels_on1 == 1) { + lcd.drawSprite(41,21,3,3,(int *)shapStar); + } + if (jewels_on2 == 1) { + lcd.drawSprite(46,21,3,3,(int *)shapStar); + } + + if (jewels_on3 == 1) { + lcd.drawSprite(2,39,3,3,(int *)shapStar); + } + if (jewels_on4 == 1) { + lcd.drawSprite(7,39,3,3,(int *)shapStar); + } + if (jewels_on5 == 1) { + lcd.drawSprite(12,39,3,3,(int *)shapStar); + } + + if (jewels_on6 == 1) { + lcd.drawSprite(38,36,3,3,(int *)shapStar); + } + if (jewels_on7 == 1) { + lcd.drawSprite(43,36,3,3,(int *)shapStar); + } + + +} + +//画人物 +void Monster::draw_monster(N5110 &lcd, DigitalIn &button_A, DigitalIn &button_B, DigitalIn &button_C, DigitalIn &button_D) { + + if (button_A.read() == 1) { + monster_x0 += 1; + } + else if (button_B.read() == 1) { + monster_y0 -= 1; + } + else if (button_C.read() == 1) { + monster_x0 -= 1; + } + else if (button_D.read() == 1) { + monster_y0 += 1; + } + + //向右超出 + if (monster_x0 > 84) { + + } + //向左限定 + if (monster_x0 < 0) { + monster_x0 = 0; + } + //向上限定 + if (monster_y0 < 9) { + monster_y0 = 9; + } + //向下限定 + if (monster_y0 > 37) { + monster_y0 = 37; + } + + if (monster_x0 > 20 && monster_x0 < 56 && monster_y0 > 33) { + monster_y0 = 33; + } + + lcd.drawSprite(monster_x0,monster_y0,6,7,(int *)whiteMonster); + + if (abs(monster_x0-41+2) <= 4 + +} + +