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.
main.cpp
- Committer:
- JasonYang
- Date:
- 2016-05-05
- Revision:
- 1:e28c7063fcd6
- Parent:
- 0:e27445dfcf3f
- Child:
- 2:748d7682d43c
File content as of revision 1:e28c7063fcd6:
#include "main.h"
int main()
{
// first need to initialise display+
lcd.init();
button.fall(&ting);
button.mode(PullDown);
//show the name of this game
lcd.printString("Flappy ball",10,1); //show game name
lcd.printString("YangQing",10,3); //show desgin name
lcd.printString("200986056",10,5); //show student code
wait(0.5); //keep screen 0.5s
lcd.clear(); //clear screen show another screen
//just simulate the loading
lcd.printString("LOADING...",8,2);
char loading [14];
for (int a = 0; a<=100; a = a+20) {
sprintf(loading,"%d %%",a);
lcd.printString(loading,18,4);
wait(0.5);
}
lcd.clear(); //clear screen show another screen
ticker.attach(&yellow,0.1); //make yellow light work, lip per 0.1s
// normal colour mode back
lcd.normalMode();
// put LED backlight on 50%
lcd.setBrightness(0.5);
calibrateJoystick(); // get centred values of joystick
pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
while(1) {
// can directly print strings at specified co-ordinates
if(stop == true) {
if (printFlag) { // if flag set, clear flag and print joystick values to serial port
printFlag = 0;
// use a random variable to change the height of vartical line.
if ( linex1<0 ) {
linex1 = 80; // for first line the strat x-axis is 80
liney1 =rand()%48+30;//random value from 30 to 48
}
if ( linex2<0 ) {
linex2 = 80; // for second line the strat x-axis is 80
liney2 =rand()%48+30;//random value from 30 to 48
}
if ( linex3<0 ) {
linex3 = 80; // for third line the strat x-axis is 80
liney3 =rand()%48+20;//random value from 20 to 48
}
if ( linex4<0 ) {
linex4 = 80; // for fourth line the strat x-axis is 80
liney4 =rand()%48+10; //random value from 10 to 48
}
if ( linex5<0 ) {
linex5 = 81; // for fifth line the strat x-axis is 81
liney5 =rand()%48+10; //random value from 10 to 48
}
// let x variable refuce to make these lines like moving to left
linex1--;
linex2--;
linex3--;
linex4--;
linex5--;
lcd.clear();
// use linex and liney to draw lines
lcd.drawLine(linex1,liney1,linex1,48,1); // draw first line
lcd.drawLine(linex2,liney2,linex2,48,1); // draw second line
lcd.drawLine(linex3,liney3,linex3,48,1); // draw third line
lcd.drawLine(linex4,liney4,linex4,48,1); // draw fourth line
lcd.drawLine(linex5,liney5,linex5,48,1); // draw fifth line
// check joystick direction
if (joystick.direction == UP ) {
d-=1; // let ball move to up
char score [14]; // char score variable
sprintf(score,"%d %",s); // let score show s vaule
s++; // s increase 1
touch();
if (flag) {
lcd.clear(); //clean screen
lcd.printString("Game over",5,1);//show game over
lcd.printString("Score:",5,4); //show score on screen
lcd.printString(score,45,4);
buzzer.period(0.001); //game over and buzzer work for 0.001 frequency
buzzer = 0.5; //the volume strong
wait(1.0);
break; //game break
}
lcd.drawCircle(c,d,4,0); //draw circle
}
if (joystick.direction == DOWN /*&& d > 5*/) {
d+=1; // let ball move down
char score [14]; // char score variable
sprintf(score,"%d %",s); // let score show s vaule
s++; // s increase 1
touch();
if (flag) {
lcd.clear(); //clean screen
lcd.printString("Game over",5,1);//show game over
lcd.printString("Score:",5,4); //show score on screen
lcd.printString(score,45,4);
buzzer.period(0.001); //game over and buzzer work for 0.001 frequency
buzzer = 0.5; //the volume strong
wait(1.0);
break; //game break
}
lcd.drawCircle(c,d,4,0); //draw circle
}
if (joystick.direction == LEFT ) {
c+=1; // let ball move to left
char score [14]; // char score variable
sprintf(score,"%d %",s); // let score show s vaule
s++; // s increase 1
touch();
if (flag) {
lcd.clear(); //clean screen
lcd.printString("Game over",5,1);//show game over
lcd.printString("Score:",5,4); //show score on screen
lcd.printString(score,45,4);
buzzer.period(0.001); //game over and buzzer work for 0.001 frequency
buzzer = 0.5; //the volume strong
wait(1.0);
break; //game break
}
lcd.drawCircle(c,d,4,0); //draw circle
}
if (joystick.direction == RIGHT ) {
c-=1; // let ball move to right
char score [14]; // char score variable
sprintf(score,"%d %",s); // let score show s vaule
s++; // s increase 1
touch();
if (flag) {
lcd.clear(); //clean screen
lcd.printString("Game over",5,1);//show game over
lcd.printString("Score:",5,4); //show score on screen
lcd.printString(score,45,4);
buzzer.period(0.001); //game over and buzzer work for 0.001 frequency
buzzer = 0.5; //the volume strong
wait(1.0);
break; //game break
}
lcd.drawCircle(c,d,4,0); //draw circle
}
if (joystick.direction == CENTRE ) {
d+=1; // make ball always fall down
char score [14]; // char score variable
sprintf(score,"%d %",s); // let score show s vaule
s++; // s increase 1
touch();
if (flag) {
lcd.clear(); //clean screen
lcd.printString("Game over",5,1);//show game over
lcd.printString("Score:",5,4); //show score on screen
lcd.printString(score,45,4);
buzzer.period(0.001); //game over and buzzer work for 0.001 frequency
buzzer = 0.5; //the volume strong
wait(1.0);
break; //game break
}
lcd.drawCircle(c,d,4,0); //draw circle
}
}
lcd.refresh();
} else {
lcd.clear();
lcd.printString("Press to play",0,3); //when game stop to show
lcd.refresh();
}
sleep();
}
}
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.button = 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;
}
// this function is to judge whether it touch or mot
void touch()
{
// these information is the condition of judgment
if (lcd.getPixel(c,d+4)||d+4==47||d==-4||lcd.getPixel(-4+c,d)||lcd.getPixel(4+c,d)||lcd.getPixel(c,d-4)||lcd.getPixel(c-1,d-4)||lcd.getPixel(c-2,d-4)||lcd.getPixel(c+1,d-3)) {
// flag = 1 means the conduction is achieved
flag=1;
} else {
flag=0;
}
}
void yellow() //the function judge yellow lights lip
{
yellow_led =! yellow_led;
}
void ting() // the function judge tp stop game
{
stop = !stop;
}