Bingshuo Chen
/
Monster_copy_copy
27
main.cpp@0:ef959ad27a16, 2021-04-08 (annotated)
- Committer:
- cbs27
- Date:
- Thu Apr 08 02:09:25 2021 +0000
- Revision:
- 0:ef959ad27a16
- Child:
- 1:48a028c4089e
Monster
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cbs27 | 0:ef959ad27a16 | 1 | /* mbed Microcontroller Library |
cbs27 | 0:ef959ad27a16 | 2 | * Copyright (c) 2019 ARM Limited |
cbs27 | 0:ef959ad27a16 | 3 | * SPDX-License-Identifier: Apache-2.0 |
cbs27 | 0:ef959ad27a16 | 4 | */ |
cbs27 | 0:ef959ad27a16 | 5 | |
cbs27 | 0:ef959ad27a16 | 6 | #include "mbed.h" |
cbs27 | 0:ef959ad27a16 | 7 | #include "platform/mbed_thread.h" |
cbs27 | 0:ef959ad27a16 | 8 | #include "N5110.h" |
cbs27 | 0:ef959ad27a16 | 9 | |
cbs27 | 0:ef959ad27a16 | 10 | #include "shapes.h" |
cbs27 | 0:ef959ad27a16 | 11 | #include "main.h" |
cbs27 | 0:ef959ad27a16 | 12 | #include "monster.h" |
cbs27 | 0:ef959ad27a16 | 13 | |
cbs27 | 0:ef959ad27a16 | 14 | N5110 lcd(p8,p9,p10,p11,p13,p21); |
cbs27 | 0:ef959ad27a16 | 15 | DigitalIn button_A(p29); |
cbs27 | 0:ef959ad27a16 | 16 | DigitalIn button_B(p28); |
cbs27 | 0:ef959ad27a16 | 17 | DigitalIn button_C(p27); |
cbs27 | 0:ef959ad27a16 | 18 | DigitalIn button_D(p26); |
cbs27 | 0:ef959ad27a16 | 19 | |
cbs27 | 0:ef959ad27a16 | 20 | Monster monster; |
cbs27 | 0:ef959ad27a16 | 21 | |
cbs27 | 0:ef959ad27a16 | 22 | int lifes; |
cbs27 | 0:ef959ad27a16 | 23 | int score, best_score; |
cbs27 | 0:ef959ad27a16 | 24 | |
cbs27 | 0:ef959ad27a16 | 25 | void init(); |
cbs27 | 0:ef959ad27a16 | 26 | void welcome(); |
cbs27 | 0:ef959ad27a16 | 27 | |
cbs27 | 0:ef959ad27a16 | 28 | void choice(); |
cbs27 | 0:ef959ad27a16 | 29 | |
cbs27 | 0:ef959ad27a16 | 30 | void game_begin(); |
cbs27 | 0:ef959ad27a16 | 31 | void show_score(); |
cbs27 | 0:ef959ad27a16 | 32 | void instruction(); |
cbs27 | 0:ef959ad27a16 | 33 | |
cbs27 | 0:ef959ad27a16 | 34 | int main() |
cbs27 | 0:ef959ad27a16 | 35 | { |
cbs27 | 0:ef959ad27a16 | 36 | init(); |
cbs27 | 0:ef959ad27a16 | 37 | welcome(); |
cbs27 | 0:ef959ad27a16 | 38 | |
cbs27 | 0:ef959ad27a16 | 39 | while(1) { |
cbs27 | 0:ef959ad27a16 | 40 | |
cbs27 | 0:ef959ad27a16 | 41 | if (button_A.read() == 1 ) { |
cbs27 | 0:ef959ad27a16 | 42 | |
cbs27 | 0:ef959ad27a16 | 43 | choice(); |
cbs27 | 0:ef959ad27a16 | 44 | |
cbs27 | 0:ef959ad27a16 | 45 | } |
cbs27 | 0:ef959ad27a16 | 46 | } |
cbs27 | 0:ef959ad27a16 | 47 | |
cbs27 | 0:ef959ad27a16 | 48 | } |
cbs27 | 0:ef959ad27a16 | 49 | |
cbs27 | 0:ef959ad27a16 | 50 | //**********************// |
cbs27 | 0:ef959ad27a16 | 51 | |
cbs27 | 0:ef959ad27a16 | 52 | void init() { |
cbs27 | 0:ef959ad27a16 | 53 | |
cbs27 | 0:ef959ad27a16 | 54 | lcd.init(); |
cbs27 | 0:ef959ad27a16 | 55 | |
cbs27 | 0:ef959ad27a16 | 56 | lifes = 3; |
cbs27 | 0:ef959ad27a16 | 57 | score = 0; |
cbs27 | 0:ef959ad27a16 | 58 | |
cbs27 | 0:ef959ad27a16 | 59 | } |
cbs27 | 0:ef959ad27a16 | 60 | |
cbs27 | 0:ef959ad27a16 | 61 | void welcome() { |
cbs27 | 0:ef959ad27a16 | 62 | |
cbs27 | 0:ef959ad27a16 | 63 | lcd.clear(); |
cbs27 | 0:ef959ad27a16 | 64 | |
cbs27 | 0:ef959ad27a16 | 65 | lcd.drawSprite(60,12,12,14,(int *)bigMonster); |
cbs27 | 0:ef959ad27a16 | 66 | lcd.printString("Monster",0,0); |
cbs27 | 0:ef959ad27a16 | 67 | lcd.printString("Game",0,2); |
cbs27 | 0:ef959ad27a16 | 68 | |
cbs27 | 0:ef959ad27a16 | 69 | lcd.printString("Press A NEXT",0,5); |
cbs27 | 0:ef959ad27a16 | 70 | |
cbs27 | 0:ef959ad27a16 | 71 | // thread_sleep_for(200); |
cbs27 | 0:ef959ad27a16 | 72 | lcd.refresh(); |
cbs27 | 0:ef959ad27a16 | 73 | |
cbs27 | 0:ef959ad27a16 | 74 | } |
cbs27 | 0:ef959ad27a16 | 75 | |
cbs27 | 0:ef959ad27a16 | 76 | void choice() { |
cbs27 | 0:ef959ad27a16 | 77 | |
cbs27 | 0:ef959ad27a16 | 78 | lcd.clear(); |
cbs27 | 0:ef959ad27a16 | 79 | |
cbs27 | 0:ef959ad27a16 | 80 | lcd.printString("A. BEGIN",0,0); |
cbs27 | 0:ef959ad27a16 | 81 | lcd.printString("B. SCORE",0,2); |
cbs27 | 0:ef959ad27a16 | 82 | lcd.printString("C. INFO",0,4); |
cbs27 | 0:ef959ad27a16 | 83 | |
cbs27 | 0:ef959ad27a16 | 84 | lcd.refresh(); |
cbs27 | 0:ef959ad27a16 | 85 | |
cbs27 | 0:ef959ad27a16 | 86 | thread_sleep_for(50); |
cbs27 | 0:ef959ad27a16 | 87 | |
cbs27 | 0:ef959ad27a16 | 88 | while(1) { |
cbs27 | 0:ef959ad27a16 | 89 | |
cbs27 | 0:ef959ad27a16 | 90 | if(button_A.read() == 1) { |
cbs27 | 0:ef959ad27a16 | 91 | game_begin(); |
cbs27 | 0:ef959ad27a16 | 92 | } |
cbs27 | 0:ef959ad27a16 | 93 | if(button_B.read() == 1) { |
cbs27 | 0:ef959ad27a16 | 94 | show_score(); |
cbs27 | 0:ef959ad27a16 | 95 | } |
cbs27 | 0:ef959ad27a16 | 96 | if(button_C.read() == 1) { |
cbs27 | 0:ef959ad27a16 | 97 | instruction(); |
cbs27 | 0:ef959ad27a16 | 98 | } |
cbs27 | 0:ef959ad27a16 | 99 | |
cbs27 | 0:ef959ad27a16 | 100 | } |
cbs27 | 0:ef959ad27a16 | 101 | |
cbs27 | 0:ef959ad27a16 | 102 | } |
cbs27 | 0:ef959ad27a16 | 103 | |
cbs27 | 0:ef959ad27a16 | 104 | //***********************// |
cbs27 | 0:ef959ad27a16 | 105 | |
cbs27 | 0:ef959ad27a16 | 106 | void game_begin() { |
cbs27 | 0:ef959ad27a16 | 107 | |
cbs27 | 0:ef959ad27a16 | 108 | monster.monster_main(lcd, button_A, button_B, button_C, button_D); |
cbs27 | 0:ef959ad27a16 | 109 | |
cbs27 | 0:ef959ad27a16 | 110 | } |
cbs27 | 0:ef959ad27a16 | 111 | |
cbs27 | 0:ef959ad27a16 | 112 | void show_score() { |
cbs27 | 0:ef959ad27a16 | 113 | |
cbs27 | 0:ef959ad27a16 | 114 | lcd.clear(); |
cbs27 | 0:ef959ad27a16 | 115 | |
cbs27 | 0:ef959ad27a16 | 116 | lcd.printString("BEST SCORE",12,1); |
cbs27 | 0:ef959ad27a16 | 117 | |
cbs27 | 0:ef959ad27a16 | 118 | char buffer[14]; |
cbs27 | 0:ef959ad27a16 | 119 | sprintf(buffer,"%d",best_score); |
cbs27 | 0:ef959ad27a16 | 120 | lcd.printString(buffer,12,3); |
cbs27 | 0:ef959ad27a16 | 121 | |
cbs27 | 0:ef959ad27a16 | 122 | lcd.printString("Press A BACK",0,5); |
cbs27 | 0:ef959ad27a16 | 123 | |
cbs27 | 0:ef959ad27a16 | 124 | lcd.refresh(); |
cbs27 | 0:ef959ad27a16 | 125 | |
cbs27 | 0:ef959ad27a16 | 126 | while(1) { |
cbs27 | 0:ef959ad27a16 | 127 | |
cbs27 | 0:ef959ad27a16 | 128 | if(button_A.read() == 1) { |
cbs27 | 0:ef959ad27a16 | 129 | choice(); |
cbs27 | 0:ef959ad27a16 | 130 | } |
cbs27 | 0:ef959ad27a16 | 131 | } |
cbs27 | 0:ef959ad27a16 | 132 | |
cbs27 | 0:ef959ad27a16 | 133 | } |
cbs27 | 0:ef959ad27a16 | 134 | |
cbs27 | 0:ef959ad27a16 | 135 | |
cbs27 | 0:ef959ad27a16 | 136 | void instruction() { |
cbs27 | 0:ef959ad27a16 | 137 | |
cbs27 | 0:ef959ad27a16 | 138 | lcd.clear(); |
cbs27 | 0:ef959ad27a16 | 139 | |
cbs27 | 0:ef959ad27a16 | 140 | lcd.drawSprite(0,0,6,7,(int *)whiteMonster); |
cbs27 | 0:ef959ad27a16 | 141 | lcd.drawSprite(0,9,7,7,(int *)blackMonster); |
cbs27 | 0:ef959ad27a16 | 142 | lcd.drawSprite(2,19,3,3,(int *)shapStar); |
cbs27 | 0:ef959ad27a16 | 143 | lcd.drawSprite(1,26,4,5,(int *)shapHeart); |
cbs27 | 0:ef959ad27a16 | 144 | |
cbs27 | 0:ef959ad27a16 | 145 | lcd.printString("Your monster",12,0); |
cbs27 | 0:ef959ad27a16 | 146 | lcd.printString("Enemy",12,1); |
cbs27 | 0:ef959ad27a16 | 147 | lcd.printString("Jewel",12,2); |
cbs27 | 0:ef959ad27a16 | 148 | lcd.printString("Lives",12,3); |
cbs27 | 0:ef959ad27a16 | 149 | |
cbs27 | 0:ef959ad27a16 | 150 | lcd.printString("Press A BACK",0,5); |
cbs27 | 0:ef959ad27a16 | 151 | |
cbs27 | 0:ef959ad27a16 | 152 | // thread_sleep_for(200); |
cbs27 | 0:ef959ad27a16 | 153 | lcd.refresh(); |
cbs27 | 0:ef959ad27a16 | 154 | |
cbs27 | 0:ef959ad27a16 | 155 | while(1) { |
cbs27 | 0:ef959ad27a16 | 156 | |
cbs27 | 0:ef959ad27a16 | 157 | if(button_A.read() == 1) { |
cbs27 | 0:ef959ad27a16 | 158 | choice(); |
cbs27 | 0:ef959ad27a16 | 159 | } |
cbs27 | 0:ef959ad27a16 | 160 | } |
cbs27 | 0:ef959ad27a16 | 161 | |
cbs27 | 0:ef959ad27a16 | 162 | } |