
Pokemon Project
Dependencies: C12832 LM75B MMA7660 mbed
Revision 0:cec9a625dfb9, committed 2017-01-23
- Comitter:
- dhermy01
- Date:
- Mon Jan 23 17:28:34 2017 +0000
- Commit message:
- test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM75B.lib Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/LM75B/#7ac462ba84ac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/Sissors/code/MMA7660/#36a163511e34
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,387 @@ +#include "C12832.h" +#include "MMA7660.h" +#include "LM75B.h" +#include "player.h" +#include "mbed.h" +#define BPM 100.0 +#define VOLUME 0.01 + +// init variable +typedef enum State {START, MENU, CHOICE, PLMENU, FIGHT, STATS} GameState; +DigitalOut led(LED1); +DigitalOut red(D5); +DigitalOut blue(D8); +DigitalOut green(D9); +DigitalIn up(A2); +DigitalIn right(A5); +DigitalIn select(D4); +DigitalIn left(A4); +DigitalIn down(A3); +C12832 lcd(D11, D13, D12, D7, D10); +PwmOut pwm_pin(D6); +LM75B sensor(D14,D15); + +// playNote function +void playNote(float frequency, float duration, float volume) { + pwm_pin.period(1.0/frequency); + pwm_pin = volume/2.0; + wait(duration); + pwm_pin = 0.0; +} + +// play_music function +void play_music(){ + float beat_duration; + + // Calculate duration of a quarter note from bpm + beat_duration = 60.0 / BPM; + + // Loop forever + while(1) { + + lcd.cls(); + lcd.locate(0, 10); + lcd.printf("Luke I'm your father !"); + + // First measure + playNote(391.995, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(391.995, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(391.995, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(311.127, (0.75 * beat_duration), VOLUME); + playNote(466.164, (0.25 * beat_duration), VOLUME); + + // Second measure + playNote(391.995, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(311.127, (0.75 * beat_duration), VOLUME); + playNote(466.164, (0.25 * beat_duration), VOLUME); + playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME); + wait(0.1); + + // Third measure + playNote(587.330, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(587.330, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(587.330, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(622.254, (0.75 * beat_duration), VOLUME); + playNote(466.164, (0.25 * beat_duration), VOLUME); + + // Fourth measure + playNote(369.994, (beat_duration - 0.1), VOLUME); + wait(0.1); + playNote(311.127, (0.75 * beat_duration), VOLUME); + playNote(466.164, (0.25 * beat_duration), VOLUME); + playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME); + wait(0.1); + } +} + +// Start Menu +void drawStart() { + lcd.locate(50,0); + lcd.printf("PLAY"); + lcd.locate(50, 20); + lcd.printf("TEMP"); +} + +// Choice Menu +void drawChoice() { + lcd.locate(50,0); + lcd.printf("FIRE"); + lcd.locate(50,10); + lcd.printf("WATER"); + lcd.locate(50,20); + lcd.printf("GRASS"); +} + +// Fight Menu +void drawPlayerMenu() { + lcd.locate(50,0); + lcd.printf("FIGHT"); + lcd.locate(50,20); + lcd.printf("VIEW STAT"); +} + +// Fight Simulation +void drawFight(Player e, int a, std::string name) { + std::string sss = e.toString(); + std::string m = ""; + std::string newStr = sss + "\n" + "Name: " + name; + char z[130]; + strncpy(z, newStr.c_str(), sizeof(z)); + lcd.cls(); + lcd.locate(0,0); + lcd.printf(z); + wait(4); +} + +// Win View +void win(){ + float beat_duration; + + // Calculate duration of a quarter note from bpm + beat_duration = 60.0 / BPM; + + lcd.cls(); + lcd.locate(0, 10); + lcd.printf("You won and \nyou got 20 exp points !"); + + playNote(1319,beat_duration-0.1,VOLUME); + wait(0.1); + playNote(1175,beat_duration-0.1,VOLUME); + wait(0.1); + + playNote(1319,beat_duration-0.1,VOLUME); + wait(0.1); + + playNote(1024,beat_duration-0.1,VOLUME); + wait(0.1); + + wait(0.05); +} + +// Loose View +void loose (){ + lcd.cls(); + lcd.locate(50, 10); + lcd.printf("LOOSE"); +} + +// Init Var +int i = 0; +int k = 0; +int v = 0; +int r = 0; +int l = 0; + +// Main function +int main() { + + //Reset Captor + led = 0; + red = 1; + blue = 1; + green = 1; + + // Create Player + Player pl(1, 20, FIRE); + + // Create Enemy + std::string nameList[42] = {"Abra", "Absol", "Azelf", "Banette", "Bastiodon", "Beautifly", "Bidoof", "Carvanha", "Clefairy", "Cresselia", "Crobat", "Darkrai", "Delphox", "Dialga", "Ditto", "Doduo", "Dratini", "Dugtrio", "Gothitelle", "Grovyle", "Haxorus", "Honedge", "Hoopa", "Jirachi", "Keldeo", "Lugia", "Makuhita", "Marill", "Meowth", "Minun", "Remoraid", "Rhyhorn", "Sableye", "Sandile", "Sawk", "Sylveon", "Tauros", "Togepi", "Victini", "Wailord", "Wingull", "Zubat"}; + Player enemy[6] = {Player(1, 15, FIRE), Player (1, 20, WATER), Player(1, 15, GRASS), Player(2, 10, FIRE), Player(4, 20, WATER), Player(2, 20, GRASS)}; + + // Draw Start Menu + GameState state = START; + up.mode(PullUp); + down.mode(PullUp); + select.mode(PullUp); + lcd.locate(40,0); + lcd.printf(">"); + drawStart(); + + // Principal loop + while(1){ + + // Start Menu + if(state == START){ + if(up){ + lcd.cls(); + lcd.locate(40,0); + lcd.printf(">"); + drawStart(); + i=0; + } + else if(down){ + lcd.cls(); + lcd.locate(40,20); + lcd.printf(">"); + drawStart(); + i=1; + } + else if(right){ + r=3; + } + else if(left){ + l=3; + } + // Select Menu + else if(select){ + + if(r == 3 && l == 3 && i==0){ + play_music(); + } + else + { + + if(i==0){ + wait(0.8); + state = CHOICE; + drawChoice(); + } + else{ + lcd.cls(); + lcd.locate(0,3); + lcd.printf("Temp = %.1f\n", sensor.temp()); + wait(1.0); + i = 0; + lcd.cls(); + lcd.locate(40,0); + lcd.printf(">"); + drawStart(); + } + } + } + + }//if state == START + + // Choice Menu + else if(state == CHOICE){ + + if(down){ + if(k==0) + { + lcd.cls(); + lcd.locate(40,10); + lcd.printf(">"); + drawChoice(); + k = 1; + wait(0.8); + } + else if(k==1) + { + lcd.cls(); + lcd.locate(40,20); + lcd.printf(">"); + drawChoice(); + k = 2; + wait(0.8); + } + }// if down + + else if(up){ + if(k==2){ + lcd.cls(); + lcd.locate(40,10); + lcd.printf(">"); + drawChoice(); + k=1; + wait(0.8); + } + else if(k==1){ + lcd.cls(); + lcd.locate(40,0); + lcd.printf(">"); + drawChoice(); + k=0; + wait(0.8); + } + }// else if up + + // Select Type + else if(select){ + if(k==0){ + pl.setType(FIRE); + red = 0; + blue = 1; + green = 1; + } + else if(k==1){ + pl.setType(WATER); + red = 1; + blue = 0; + green = 1; + } + else if(k==2){ + pl.setType(GRASS); + red = 1; + blue = 1; + green = 0; + } + wait(0.8); + lcd.cls(); + lcd.locate(40,0); + lcd.printf(">"); + drawPlayerMenu(); + state = PLMENU; + } + }// if state == choice + + // Fight Menu + else if(state == PLMENU){ + if(up){ + lcd.cls(); + lcd.locate(40,0); + lcd.printf(">"); + drawPlayerMenu(); + v=0; + } + else if(down){ + lcd.cls(); + lcd.locate(40,20); + lcd.printf(">"); + drawPlayerMenu(); + v=1; + } + + else if(select){ + wait(0.8); + if(v==0){ + state = FIGHT; + } + else{ + state = STATS; + } + } + + }// if state == PLMENU + + // Fight + else if(state == FIGHT){ + // Rand Enemy + int ind = rand() % 6; + int ind2 = rand() % 42; + std::string str = nameList[ind2]; + Player ene = enemy[ind]; + // Fight Simu + int w = pl.fight(ene); + // Check Exp + pl.checkEXP(); + drawFight(ene, w, str); + + // Result + if(w==1){ + wait(0.8); + win(); + state = PLMENU; + } + else{ + wait(0.8); + loose(); + state = PLMENU; + } + + }//if state == FIGHT + + // View Stat + else if (state == STATS) { + std::string sss = pl.toString(); + char c[50]; + strncpy(c, sss.c_str(), sizeof(c)); + lcd.cls(); + lcd.locate(0,0); + lcd.printf(c); + wait(3); + + lcd.cls(); + state = PLMENU; + }// if state == MENU + + + }// while 1 + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/faff56e089b2 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/player.cpp Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,101 @@ +#include "player.h" +#include "LM75B.h" +#include <stdio.h> +#include "C12832.h" + +// init var and captor +C12832 lcd2(D11, D13, D12, D7, D10); +std::string TYPEStr[3] = {"FIRE", "WATER", "GRASS"}; +LM75B sensorTemp(D14,D15); +int temp; + +// create player +Player::Player(void) { + Player(1, 15, FIRE); +} + +// setters player +Player::Player(uns l, uns att, Type_e t) { + lvl = l; + attackDmg = att; + type = t; + exp = 0; + crit = 0.0f; +} + +// fight function +int Player::fight(Player p) { + int win = 0; + uns newAtt = 0; + if ((p.type == FIRE && type == WATER )|| (p.type == GRASS && type == FIRE) || (p.type == WATER && type == GRASS)) { + newAtt = 2 * attackDmg; + if (newAtt >= p.attackDmg) win = 1; + else if (newAtt < p.attackDmg) win = -1; + } else if ((p.type == WATER && type == FIRE) || (p.type == GRASS && type == WATER) || (p.type == FIRE && type == GRASS)) { + newAtt = (uns)(attackDmg >> 1); + if (newAtt >= p.attackDmg) win = 1; + else if (newAtt < p.attackDmg) win = -1; + } else { + newAtt = attackDmg; + if (newAtt >= p.attackDmg) win = 1; + else if (newAtt < p.attackDmg) win = -1; + } + if (win == 1) exp += 20; + return win; +} + +// level up function +void Player::lvlUp() { + lvl++; + attackDmg += 2; + crit += 0.05f; + exp = 0; +} + +// check exp function +void Player::checkEXP() { + if (exp >= 100) { + Player::lvlUp(); + } +} + +// set the type of pokemon +void Player::setType(Type_e t) { + type = t; + if(t == FIRE){ + temp = sensorTemp.temp(); + if(temp > 28){ + attackDmg = attackDmg+4; + } + } + else if(t==GRASS) + { + temp = sensorTemp.temp(); + if(temp < 28 && temp > 18){ + attackDmg = attackDmg+4; + } + } + else if(t==WATER) + { + temp = sensorTemp.temp(); + if(temp < 18){ + attackDmg = attackDmg+4; + } + } + +} + +// set text of player +std::string Player::toString(void) { + std::string res = "STAT: \n" ; + char num[40]; + char num2[40]; + char num3[40]; + sprintf(num, "Lvl: %u ", lvl); + snprintf(num3, 40, "Exp: %u ", exp); + sprintf(num2, "Att: %u \nType: %s", attackDmg, TYPEStr[type].c_str()); + res += num; + res += num3; + res += num2; + return (res); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/player.h Mon Jan 23 17:28:34 2017 +0000 @@ -0,0 +1,28 @@ +#include <string> +// Init var +typedef unsigned int uns; +extern std::string TYPEStr[3]; +typedef enum Type {FIRE, WATER, GRASS} Type_e; + +// Class player +class Player { + public: + Player(); + Player(uns, uns, Type_e); + //fight + int fight(Player p); + //lvlup + void lvlUp(); + void checkEXP(); + //setting + void setType(Type_e); + std::string toString(void); + + protected: + //stats + uns lvl; + Type_e type; + uns attackDmg; + uns exp; + float crit; +}; \ No newline at end of file