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.
Dependencies: mbed
CrossyChicken/CrossyChicken.cpp@4:aae7f8d4ab78, 2020-05-06 (annotated)
- Committer:
- el19tb
- Date:
- Wed May 06 16:53:18 2020 +0000
- Revision:
- 4:aae7f8d4ab78
- Parent:
- 3:648c9d5001be
- Child:
- 5:6e3afee7eac3
Made the code more structured, added different vehicle types;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el19tb | 2:86cef2afa648 | 1 | #include "CrossyChicken.h" |
| el19tb | 2:86cef2afa648 | 2 | #include "Menu.h" |
| el19tb | 3:648c9d5001be | 3 | #include "GraphicEngine.h" |
| el19tb | 3:648c9d5001be | 4 | #include <vector> |
| el19tb | 3:648c9d5001be | 5 | #include <stdio.h> |
| el19tb | 3:648c9d5001be | 6 | #include <cstddef> |
| el19tb | 2:86cef2afa648 | 7 | |
| el19tb | 2:86cef2afa648 | 8 | Gamepad gamepad; |
| el19tb | 2:86cef2afa648 | 9 | |
| el19tb | 3:648c9d5001be | 10 | //create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water) |
| el19tb | 3:648c9d5001be | 11 | //random |
| el19tb | 3:648c9d5001be | 12 | //algoritmic way of generating lanes |
| el19tb | 3:648c9d5001be | 13 | |
| el19tb | 2:86cef2afa648 | 14 | //size of each each tile in the game |
| el19tb | 4:aae7f8d4ab78 | 15 | int grid = 4; |
| el19tb | 2:86cef2afa648 | 16 | |
| el19tb | 3:648c9d5001be | 17 | //make one object of chicken |
| el19tb | 3:648c9d5001be | 18 | //use this to move around the lcd |
| el19tb | 4:aae7f8d4ab78 | 19 | Chicken chicken((84/2)-4/2, 48-4, 4); |
| el19tb | 3:648c9d5001be | 20 | Chicken *chickenptr= &chicken; |
| el19tb | 3:648c9d5001be | 21 | |
| el19tb | 3:648c9d5001be | 22 | //there will be multiple cars |
| el19tb | 4:aae7f8d4ab78 | 23 | std::vector<Car> cars; |
| el19tb | 3:648c9d5001be | 24 | |
| el19tb | 3:648c9d5001be | 25 | //class that whill show objects |
| el19tb | 3:648c9d5001be | 26 | GraphicEngine graphics(chickenptr); |
| el19tb | 2:86cef2afa648 | 27 | |
| el19tb | 2:86cef2afa648 | 28 | //main function that starts the game |
| el19tb | 3:648c9d5001be | 29 | void CrossyChicken::start(){ |
| el19tb | 2:86cef2afa648 | 30 | //game setup |
| el19tb | 3:648c9d5001be | 31 | graphics.init(); |
| el19tb | 3:648c9d5001be | 32 | graphics.contrast(); |
| el19tb | 3:648c9d5001be | 33 | graphics.backLightOn(); |
| el19tb | 2:86cef2afa648 | 34 | gamepad.init(); |
| el19tb | 4:aae7f8d4ab78 | 35 | |
| el19tb | 4:aae7f8d4ab78 | 36 | for (int i = 0; i < 4; i++){ |
| el19tb | 4:aae7f8d4ab78 | 37 | Car car; |
| el19tb | 4:aae7f8d4ab78 | 38 | cars.push_back(car); |
| el19tb | 4:aae7f8d4ab78 | 39 | } |
| el19tb | 4:aae7f8d4ab78 | 40 | |
| el19tb | 2:86cef2afa648 | 41 | //keep reading and processing user input |
| el19tb | 3:648c9d5001be | 42 | while(1) { |
| el19tb | 3:648c9d5001be | 43 | graphics.clear(); |
| el19tb | 3:648c9d5001be | 44 | graphics.showChicken(); |
| el19tb | 4:aae7f8d4ab78 | 45 | process_input(); |
| el19tb | 3:648c9d5001be | 46 | moveCar(); |
| el19tb | 3:648c9d5001be | 47 | graphics.refresh(); |
| el19tb | 3:648c9d5001be | 48 | wait_ms(100); |
| el19tb | 2:86cef2afa648 | 49 | } |
| el19tb | 2:86cef2afa648 | 50 | } |
| el19tb | 2:86cef2afa648 | 51 | |
| el19tb | 2:86cef2afa648 | 52 | //A moves right |
| el19tb | 2:86cef2afa648 | 53 | //X moves upward |
| el19tb | 2:86cef2afa648 | 54 | //B moves downward |
| el19tb | 2:86cef2afa648 | 55 | //Y moves left |
| el19tb | 2:86cef2afa648 | 56 | void CrossyChicken::process_input() { |
| el19tb | 3:648c9d5001be | 57 | //determine the input |
| el19tb | 3:648c9d5001be | 58 | if(gamepad.A_pressed()){ |
| el19tb | 3:648c9d5001be | 59 | moveChicken(1,0); |
| el19tb | 3:648c9d5001be | 60 | } else if(gamepad.X_pressed()){ |
| el19tb | 3:648c9d5001be | 61 | moveChicken(0,-1); |
| el19tb | 3:648c9d5001be | 62 | } else if(gamepad.B_pressed()){ |
| el19tb | 3:648c9d5001be | 63 | moveChicken(0,1); |
| el19tb | 3:648c9d5001be | 64 | } else if(gamepad.Y_pressed()){ |
| el19tb | 3:648c9d5001be | 65 | moveChicken(-1,0); |
| el19tb | 3:648c9d5001be | 66 | } |
| el19tb | 2:86cef2afa648 | 67 | } |
| el19tb | 2:86cef2afa648 | 68 | |
| el19tb | 3:648c9d5001be | 69 | //moves the chicken around the grid |
| el19tb | 3:648c9d5001be | 70 | void CrossyChicken::moveChicken(int xWay, int yWay){ |
| el19tb | 3:648c9d5001be | 71 | //increment the left side of the chicken by a value of the grid size |
| el19tb | 4:aae7f8d4ab78 | 72 | chicken.x += xWay * 4; |
| el19tb | 3:648c9d5001be | 73 | |
| el19tb | 3:648c9d5001be | 74 | //increment the top side by a value of grid sizw |
| el19tb | 4:aae7f8d4ab78 | 75 | chicken.y += yWay * 4; |
| el19tb | 3:648c9d5001be | 76 | |
| el19tb | 3:648c9d5001be | 77 | //display the new state of the chicken |
| el19tb | 3:648c9d5001be | 78 | graphics.showChicken(); |
| el19tb | 3:648c9d5001be | 79 | |
| el19tb | 3:648c9d5001be | 80 | wait_ms(30); |
| el19tb | 2:86cef2afa648 | 81 | } |
| el19tb | 2:86cef2afa648 | 82 | |
| el19tb | 3:648c9d5001be | 83 | void CrossyChicken::moveCar() { |
| el19tb | 4:aae7f8d4ab78 | 84 | std::vector<Car>::size_type t; |
| el19tb | 4:aae7f8d4ab78 | 85 | |
| el19tb | 4:aae7f8d4ab78 | 86 | |
| el19tb | 4:aae7f8d4ab78 | 87 | |
| el19tb | 4:aae7f8d4ab78 | 88 | //for(int t = 0; t != cars.size(); t++){ |
| el19tb | 4:aae7f8d4ab78 | 89 | //set the seperation of the first row |
| el19tb | 4:aae7f8d4ab78 | 90 | cars.at(0).setRow(4); |
| el19tb | 4:aae7f8d4ab78 | 91 | |
| el19tb | 4:aae7f8d4ab78 | 92 | Car *carptr = &cars[0]; |
| el19tb | 3:648c9d5001be | 93 | |
| el19tb | 4:aae7f8d4ab78 | 94 | carptr->speedMedium(2); |
| el19tb | 4:aae7f8d4ab78 | 95 | //carptr->left_car.x += 0.2; |
| el19tb | 4:aae7f8d4ab78 | 96 | |
| el19tb | 4:aae7f8d4ab78 | 97 | carptr->setSeperation(0); |
| el19tb | 4:aae7f8d4ab78 | 98 | |
| el19tb | 4:aae7f8d4ab78 | 99 | graphics.showCar(carptr); |
| el19tb | 4:aae7f8d4ab78 | 100 | //} |
| el19tb | 2:86cef2afa648 | 101 | } |