Doxygen comments added
Dependencies: mbed Gamepad N5110
main.cpp@15:fc6b40fceb4f, 2019-05-09 (annotated)
- Committer:
- rafeh
- Date:
- Thu May 09 13:46:01 2019 +0000
- Revision:
- 15:fc6b40fceb4f
- Parent:
- 14:9a9ac55616c4
- Child:
- 16:95e3706790e5
some changes in main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rafeh | 0:43364ffd7cf6 | 1 | /* |
rafeh | 0:43364ffd7cf6 | 2 | ELEC2645 Embedded Systems Project |
rafeh | 0:43364ffd7cf6 | 3 | School of Electronic & Electrical Engineering |
rafeh | 0:43364ffd7cf6 | 4 | University of Leeds |
rafeh | 0:43364ffd7cf6 | 5 | Name: Rafeh Ishtiaq |
rafeh | 0:43364ffd7cf6 | 6 | Username: el17ri |
rafeh | 0:43364ffd7cf6 | 7 | Student ID Number: 201062291 |
rafeh | 0:43364ffd7cf6 | 8 | Date: 20/03/2019 |
rafeh | 0:43364ffd7cf6 | 9 | */ |
rafeh | 0:43364ffd7cf6 | 10 | |
rafeh | 0:43364ffd7cf6 | 11 | #include "mbed.h" |
rafeh | 0:43364ffd7cf6 | 12 | #include "N5110.h" |
rafeh | 0:43364ffd7cf6 | 13 | #include "Gamepad.h" |
rafeh | 3:5409b50b01b0 | 14 | #include "Pipes.h" |
rafeh | 6:bc580b480ac8 | 15 | #include "Bird.h" |
rafeh | 7:05f433e196d6 | 16 | #include "Entrance.h" |
rafeh | 9:b7a3ec1c7217 | 17 | #include "Scoring.h" |
rafeh | 8:d91564c0f337 | 18 | #define PIPE_WIDTH 8 |
rafeh | 8:d91564c0f337 | 19 | #define BIRD_X 25 |
rafeh | 0:43364ffd7cf6 | 20 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
rafeh | 0:43364ffd7cf6 | 21 | Gamepad pad; |
rafeh | 6:bc580b480ac8 | 22 | Bird bird; |
rafeh | 3:5409b50b01b0 | 23 | Pipes pipes; |
rafeh | 7:05f433e196d6 | 24 | Entrance entrance; |
rafeh | 9:b7a3ec1c7217 | 25 | Scoring flappy; |
rafeh | 14:9a9ac55616c4 | 26 | Pipes pipes2; |
rafeh | 15:fc6b40fceb4f | 27 | |
rafeh | 15:fc6b40fceb4f | 28 | /////////////////////////////Funcions//////////////////////////////////////////// |
rafeh | 15:fc6b40fceb4f | 29 | void produce(int pipex1, int pipex2,int pipeheight1,int pipeheight2,int birdx,int birdy); |
rafeh | 15:fc6b40fceb4f | 30 | void print_score(int scored_points); |
rafeh | 15:fc6b40fceb4f | 31 | ///////////////////////////////////////////////////////////////////////////// |
rafeh | 3:5409b50b01b0 | 32 | |
rafeh | 0:43364ffd7cf6 | 33 | int main() { |
rafeh | 0:43364ffd7cf6 | 34 | lcd.init(); |
rafeh | 0:43364ffd7cf6 | 35 | pad.init(); |
rafeh | 0:43364ffd7cf6 | 36 | lcd.clear(); |
rafeh | 14:9a9ac55616c4 | 37 | lcd.setContrast(0.5); |
rafeh | 15:fc6b40fceb4f | 38 | entrance.welcome_page(lcd,pad); //display the welcome page |
rafeh | 2:9f5714571c41 | 39 | if (pad.check_event(Gamepad::START_PRESSED) == false) { |
rafeh | 2:9f5714571c41 | 40 | lcd.clear(); |
rafeh | 2:9f5714571c41 | 41 | |
rafeh | 15:fc6b40fceb4f | 42 | ///////////////////////// Initialising Values//////////////////////////////////////////////////// |
rafeh | 15:fc6b40fceb4f | 43 | int yaxis=20; //y position of the bird |
rafeh | 15:fc6b40fceb4f | 44 | int xvalue=84; //initialising the x value of the first pipe (there will be two pipes on screen) |
rafeh | 15:fc6b40fceb4f | 45 | int xvalue2=0; //initialising the x value of the second pipe |
rafeh | 15:fc6b40fceb4f | 46 | int height = pipes.generate_height(); //initialising the height of the first pipe (top part of the pipe) |
rafeh | 15:fc6b40fceb4f | 47 | int height2=0; //initialising the height of the second pipe |
rafeh | 15:fc6b40fceb4f | 48 | int score = 0; //initialising the score |
rafeh | 15:fc6b40fceb4f | 49 | int highscore = 0; //initialising the highscore |
rafeh | 15:fc6b40fceb4f | 50 | |
rafeh | 15:fc6b40fceb4f | 51 | /////////////////////////////////////////////////////////////////////////////////////////////////////// |
rafeh | 2:9f5714571c41 | 52 | while(1) { |
rafeh | 15:fc6b40fceb4f | 53 | |
rafeh | 15:fc6b40fceb4f | 54 | produce(xvalue,xvalue2,height,height2,BIRD_X,yaxis); //draw the pipes and the bird |
rafeh | 15:fc6b40fceb4f | 55 | print_score(score); //display the score on the top right |
rafeh | 8:d91564c0f337 | 56 | |
rafeh | 10:75de0f4da176 | 57 | flappy.set_score(score); |
rafeh | 10:75de0f4da176 | 58 | score=flappy.add_score(score,xvalue,BIRD_X,PIPE_WIDTH); //add score if the pipe has gone further left of the bird |
rafeh | 14:9a9ac55616c4 | 59 | score=flappy.add_score(score,xvalue2,BIRD_X,PIPE_WIDTH); |
rafeh | 15:fc6b40fceb4f | 60 | |
rafeh | 15:fc6b40fceb4f | 61 | if(xvalue<29 && xvalue >27) { //generates a new pipe if the previous pipe has crossed a certain point |
rafeh | 14:9a9ac55616c4 | 62 | height2=pipes.generate_height(); //a new height is generated for the new pipe (i.e. the placement of the gap is different) |
rafeh | 14:9a9ac55616c4 | 63 | xvalue2=84; |
rafeh | 14:9a9ac55616c4 | 64 | } |
rafeh | 15:fc6b40fceb4f | 65 | if(xvalue2<29 && xvalue2>27) { //does the same as above for the second pipe |
rafeh | 14:9a9ac55616c4 | 66 | height=pipes.generate_height(); |
rafeh | 3:5409b50b01b0 | 67 | xvalue=84; |
rafeh | 14:9a9ac55616c4 | 68 | } |
rafeh | 15:fc6b40fceb4f | 69 | |
rafeh | 3:5409b50b01b0 | 70 | wait(0.075); |
rafeh | 15:fc6b40fceb4f | 71 | yaxis=bird.get_position(yaxis,pad); //gets the new vertical position for the bird |
rafeh | 14:9a9ac55616c4 | 72 | if(xvalue>2) { |
rafeh | 15:fc6b40fceb4f | 73 | xvalue=xvalue-2; //moves the first pipes towards the left |
rafeh | 14:9a9ac55616c4 | 74 | } |
rafeh | 14:9a9ac55616c4 | 75 | if(xvalue2>2) { |
rafeh | 15:fc6b40fceb4f | 76 | xvalue2=xvalue2-2; //moves the second pipe towards the left |
rafeh | 14:9a9ac55616c4 | 77 | } |
rafeh | 15:fc6b40fceb4f | 78 | if ((flappy.check_collisions(yaxis,xvalue,height))||(flappy.check_collisions(yaxis,xvalue2,height2))) { //checking for collisions |
rafeh | 15:fc6b40fceb4f | 79 | wait(1); // between the bird and both pipes |
rafeh | 15:fc6b40fceb4f | 80 | |
rafeh | 14:9a9ac55616c4 | 81 | if (flappy.check_for_highscore(score,highscore)) { //check if the score was highscore |
rafeh | 10:75de0f4da176 | 82 | highscore=score;} |
rafeh | 14:9a9ac55616c4 | 83 | flappy.update_highscore(highscore); //update the highscore |
rafeh | 15:fc6b40fceb4f | 84 | flappy.display_score(lcd,pad); |
rafeh | 15:fc6b40fceb4f | 85 | wait(0.1); |
rafeh | 14:9a9ac55616c4 | 86 | while (pad.check_event(Gamepad::A_PRESSED) == false) { |
rafeh | 10:75de0f4da176 | 87 | lcd.clear(); |
rafeh | 15:fc6b40fceb4f | 88 | flappy.display_score(lcd,pad); //display the score and highscore until button A is pressed |
rafeh | 14:9a9ac55616c4 | 89 | if(pad.check_event(Gamepad::START_PRESSED) == false) { break;} |
rafeh | 10:75de0f4da176 | 90 | } |
rafeh | 15:fc6b40fceb4f | 91 | score=0; //initialising the values again for the new game |
rafeh | 14:9a9ac55616c4 | 92 | xvalue=84; |
rafeh | 14:9a9ac55616c4 | 93 | yaxis=20; |
rafeh | 14:9a9ac55616c4 | 94 | xvalue2=0; |
rafeh | 14:9a9ac55616c4 | 95 | lcd.refresh(); |
rafeh | 5:3a0948f56de0 | 96 | } |
rafeh | 2:9f5714571c41 | 97 | lcd.refresh(); |
rafeh | 2:9f5714571c41 | 98 | lcd.clear(); |
rafeh | 2:9f5714571c41 | 99 | } |
rafeh | 2:9f5714571c41 | 100 | } |
rafeh | 10:75de0f4da176 | 101 | } |
rafeh | 2:9f5714571c41 | 102 | |
rafeh | 15:fc6b40fceb4f | 103 | |
rafeh | 15:fc6b40fceb4f | 104 | |
rafeh | 15:fc6b40fceb4f | 105 | void produce(int pipex1, int pipex2,int pipeheight1,int pipeheight2,int birdx,int birdy){ |
rafeh | 15:fc6b40fceb4f | 106 | lcd.drawRect(0,45,84,3,FILL_BLACK); //draw the floor |
rafeh | 15:fc6b40fceb4f | 107 | if(pipex1>2) { |
rafeh | 15:fc6b40fceb4f | 108 | pipes.init(pipex1,pipeheight1); // draw the first pipes |
rafeh | 15:fc6b40fceb4f | 109 | pipes.draw(lcd); |
rafeh | 15:fc6b40fceb4f | 110 | } |
rafeh | 15:fc6b40fceb4f | 111 | if(pipex2>2){ //draw the second pipe |
rafeh | 15:fc6b40fceb4f | 112 | pipes2.init(pipex2,pipeheight2); |
rafeh | 15:fc6b40fceb4f | 113 | pipes2.draw(lcd); |
rafeh | 15:fc6b40fceb4f | 114 | } |
rafeh | 15:fc6b40fceb4f | 115 | bird.init(birdx,birdy); //draw the bird |
rafeh | 15:fc6b40fceb4f | 116 | bird.draw(lcd); |
rafeh | 15:fc6b40fceb4f | 117 | } |
rafeh | 15:fc6b40fceb4f | 118 | |
rafeh | 15:fc6b40fceb4f | 119 | |
rafeh | 10:75de0f4da176 | 120 | |
rafeh | 15:fc6b40fceb4f | 121 | void print_score(int scored_points) { //displays the score when the game is being played |
rafeh | 15:fc6b40fceb4f | 122 | char display[3]; |
rafeh | 15:fc6b40fceb4f | 123 | sprintf(display,"%d",scored_points); |
rafeh | 15:fc6b40fceb4f | 124 | lcd.printString(display,60,0); |
rafeh | 15:fc6b40fceb4f | 125 | } |
rafeh | 15:fc6b40fceb4f | 126 |