Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Game_StarCraft_III by
main.cpp
- Committer:
- el15cl
- Date:
- 2016-04-28
- Revision:
- 0:33ba6cd82532
- Child:
- 1:97ef4864d6fb
File content as of revision 0:33ba6cd82532:
#include "mbed.h" #include "N5110.h" #include <stdio.h> #include <stdlib.h> #define DIRECTION_TOLERANCE 0.05 // VCC,SCE,RST,D/C,MOSI,SCLK,LED N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); PwmOut buzzer(PTA2); DigitalIn button(PTB18); DigitalIn buttonjoystick(PTB11); AnalogIn xPot(PTB3); AnalogIn yPot(PTB2); Ticker pollJoystick; Ticker bullet; Ticker enermy; Serial serial(USBTX,USBRX); enum DirectionName { UP, DOWN, LEFT, RIGHT, CENTRE, UNKNOWN }; typedef struct JoyStick Joystick; struct JoyStick { float x; // current x value float x0; // 'centred' x value float y; // current y value float y0; // 'centred' y value int buttonjoystick; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) DirectionName direction; // current direction }; // create struct variable Joystick joystick; int printFlag = 0; int enermyFlag = 0; int bulletFlag = 0; void calibrateJoystick(); void updateJoystick(); void createbullet(); void createenermy(); int status =1; bool hit = false; int main() { // first need to initialise display lcd.init(); button.mode(PullDown); buttonjoystick.mode(PullDown); calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second //joystick // these are default settings so not strictly needed lcd.normalMode(); // normal colour mode lcd.setBrightness(1.0); // put LED backlight on 50% lcd.printString("Welcome To",15,1); lcd.printString("Starcraft III!",4,3); lcd.refresh(); wait(5.0); lcd.clear(); lcd.printString("Menu",30,0); lcd.drawLine(0,10,83,10,1); lcd.printString("New Game",20,3); lcd.refresh(); while(1){ if (buttonjoystick){ //buzzer.period_ms(2.9); //buzzer.pulsewidth_ms(1.9); wait(0.5); lcd.clear(); lcd.refresh(); break; } } //loading lcd.printString("Loading......",6,1); for (int x = 0; x < WIDTH ; x+=10) { lcd.drawRect(0,36,10+x,10,1); wait(0.1); lcd.refresh(); } lcd.clear(); int x = 41 ; lcd.drawLine(x,40,x,47,1); lcd.drawLine(x-4,42,x+4,42,1); lcd.drawLine(x-1,43,x+1,43,1); lcd.drawLine(x-1,47,x+1,47,1); lcd.refresh(); int z = 41; int q = 0; int p = 0; int a; int b; a = rand() % 76; b = rand() % 76; int y = 39; int buttonjoystickpress = 0; //bullet.attach(&createbullet,0.1); //enermy.attach(&createenermy,2.0); while(1){ if (printFlag) { // if flag set, clear flag and print joystick values to serial port printFlag = 0; // check joystick direction a-b > 7; b-a > 7; a + b < 83; if(!hit){ lcd.drawLine(a,q+1,a+1,q,0); lcd.drawLine(a+1,q,a+5,q,0); lcd.drawLine(a+5,q,a+6,q+1,0); lcd.drawLine(a+3,q,a+3,q+4,0); q++; lcd.drawLine(a,q+1,a+1,q,1); lcd.drawLine(a+1,q,a+5,q,1); lcd.drawLine(a+5,q,a+6,q+1,1); lcd.drawLine(a+3,q,a+3,q+4,1); lcd.refresh(); } hit = false; /* if(!hit){ lcd.drawLine(a+b,p+1,a+1+b,p,0); lcd.drawLine(a+1+b,p,a+5+b,p,0); lcd.drawLine(a+5+b,p,a+6+b,p+1,0); lcd.drawLine(a+3+b,p,a+3+b,p+4,0); p++; lcd.drawLine(a+b,p+1,a+1+b,p,1); lcd.drawLine(a+1+b,p,a+5+b,p,1); lcd.drawLine(a+5+b,p,a+6+b,p+1,1); lcd.drawLine(a+3+b,p,a+3+b,p+4,1); lcd.refresh(); } hit = false; */ if(q > 50){ q = 0; p = 0; a = rand () % 76; b = rand () % 76; } if (joystick.direction == UP && x > 4){ lcd.drawLine(x,40,x,47,0); lcd.drawLine(x-4,42,x+4,42,0); lcd.drawLine(x-1,43,x+1,43,0); lcd.drawLine(x-1,47,x+1,47,0); x-=1; --z; lcd.drawLine(x,40,x,47,1); lcd.drawLine(x-4,42,x+4,42,1); lcd.drawLine(x-1,43,x+1,43,1); lcd.drawLine(x-1,47,x+1,47,1); lcd.refresh(); } if (joystick.direction == DOWN && x < 78){ lcd.drawLine(x,40,x,47,0); lcd.drawLine(x-4,42,x+4,42,0); lcd.drawLine(x-1,43,x+1,43,0); lcd.drawLine(x-1,47,x+1,47,0); x+=1; ++z; lcd.drawLine(x,40,x,47,1); lcd.drawLine(x-4,42,x+4,42,1); lcd.drawLine(x-1,43,x+1,43,1); lcd.drawLine(x-1,47,x+1,47,1); lcd.refresh(); } if(button){ for (int y = 39; y > -5 ; y--) { lcd.drawLine(z,y,z,y-2,1); if(y<39) lcd.drawLine(z,y+1,z,y-2+1,0); } lcd.refresh(); wait(0.1); } } if(buttonjoystick && buttonjoystickpress < 3){ ++buttonjoystickpress; hit = true; lcd.drawLine(a,q+1,a+1,q,0); lcd.drawLine(a+1,q,a+5,q,0); lcd.drawLine(a+5,q,a+6,q+1,0); lcd.drawLine(a+3,q,a+3,q+4,0); q=0; a = rand() % 76; for (int r = 5; r < 100; r+=8) { lcd.drawCircle(z,45,r,0); if(r < 100){ lcd.drawCircle(z,45,r-7,2); lcd.drawLine(x,40,x,47,1); lcd.drawLine(x-4,42,x+4,42,1); lcd.drawLine(x-1,43,x+1,43,1); lcd.drawLine(x-1,47,x+1,47,1); } lcd.refresh(); wait(0.1); } } if(z == a && q+1 == y){ hit = true; lcd.drawLine(a,q+1,a+1,q,0); lcd.drawLine(a+1,q,a+5,q,0); lcd.drawLine(a+5,q,a+6,q+1,0); lcd.drawLine(a+3,q,a+3,q+4,0); q=0; a = rand() % 76; } } if(a == x-4 && q+1 == 42){ lcd.clear(); lcd.printString ("Game Over",18,1); lcd.printString ("Viking",24,2); lcd.printString ("Destroyed",18,3); lcd.refresh(); break; } } } void calibrateJoystick() { button.mode(PullDown); // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } void updateJoystick() { // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state joystick.buttonjoystick = button; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = CENTRE; } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else { joystick.direction = UNKNOWN; } // set flag for printing printFlag = 1; } void createbullet() { int z = 41; if (joystick.direction == UP && z > 4){ --z; } if (joystick.direction == UP && z > 78){ ++z; } if(button){ for (int y = 39; y > -5 ; y-=7) { lcd.drawLine(z,y,z,y-2,1); if(y<39) { lcd.drawLine(z,y+7,z,y-2+7,0); } lcd.refresh(); } } bulletFlag = 1; } void createenermy() { int a; int b; a = rand() % 76; b = rand() % 76; for (int q=0 ;q < 52; q+=2){ lcd.drawLine(a,q+1,a+1,q,1); lcd.drawLine(a+1,q,a+5,q,1); lcd.drawLine(a+5,q,a+6,q+1,1); lcd.drawLine(a+3,q,a+3,q+4,1); lcd.drawLine(a+b,q+1,a+1+b,q,1); lcd.drawLine(a+1+b,q,a+5+b,q,1); lcd.drawLine(a+5+b,q,a+6+b,q+1,1); lcd.drawLine(a+3+b,q,a+3+b,q+4,1); if (q < 52){ lcd.drawLine(a,q+1-2,a+1,q-2,0); lcd.drawLine(a+1,q-2,a+5,q-2,0); lcd.drawLine(a+5,q-2,a+6,q+1-2,0); lcd.drawLine(a+3,q-2,a+3,q+4-2,0); lcd.drawLine(a+b,q+1-2,a+1+b,q-2,0); lcd.drawLine(a+1+b,q-2,a+5+b,q-2,0); lcd.drawLine(a+5+b,q-2,a+6+b,q+1-2,0); lcd.drawLine(a+3+b,q-2,a+3+b,q+4-2,0); } } enermyFlag = 1; }