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
Revision 3:648c9d5001be, committed 2020-05-04
- Comitter:
- el19tb
- Date:
- Mon May 04 03:55:03 2020 +0000
- Parent:
- 2:86cef2afa648
- Child:
- 4:aae7f8d4ab78
- Commit message:
- added a lane system with cars so the chicken can avoid
Changed in this revision
--- a/Chicken/Chicken.cpp Mon Apr 13 17:08:55 2020 +0000
+++ b/Chicken/Chicken.cpp Mon May 04 03:55:03 2020 +0000
@@ -1,7 +1,4 @@
#include "Chicken.h"
-
-//access to CrossyChicken object
-CrossyChicken chick;
Serial pc(USBTX, USBRX);
@@ -10,34 +7,10 @@
this->x = x;
this->y = y;
this->width = width;
-}
-
-//moves the chicken around the grid
-void Chicken::moveChicken(int xWay, int yWay){
- //increment the left side of the chicken by a value of the grid size
- leftSide += xWay * 8;
- //increment the top side by a value of grid sizw
- topSide += yWay * 8;
-
- //display the new state of the chicken
- show();
-}
-
-//make chicken square
-void Chicken::makeChicken(){
leftSide = x;
rightSide = width + x;
topSide = y;
bottomSide = height + y;
}
-//show the chicken to the LCD
-void Chicken::show(){
- chick.lcd.clear();
-
- //fill the chicken with black color
- chick.lcd.drawRect(leftSide, topSide, rightSide, bottomSide, FILL_BLACK);
- chick.lcd.refresh();
-}
-
--- a/Chicken/Chicken.h Mon Apr 13 17:08:55 2020 +0000
+++ b/Chicken/Chicken.h Mon May 04 03:55:03 2020 +0000
@@ -1,13 +1,14 @@
+//#include "SDFileSystem.h"
#ifndef CHICKEN_H
#define CHICKEN_H
-//#include "SDFileSystem.h"
+
#include "Gamepad.h"
#include "mbed.h"
#include "N5110.h"
#include "Square.h"
-#include "CrossyChicken.h"
+
-class Chicken : public Square {
+class Chicken{
public:
//constructor
Chicken();
@@ -25,8 +26,7 @@
void show();
void makeChicken();
- void process_input();
- void moveChicken(int xWay, int yWay);
+
};
-#endif
\ No newline at end of file
+#endif
--- a/CrossyChicken/CrossyChicken.cpp Mon Apr 13 17:08:55 2020 +0000
+++ b/CrossyChicken/CrossyChicken.cpp Mon May 04 03:55:03 2020 +0000
@@ -1,67 +1,93 @@
#include "CrossyChicken.h"
#include "Menu.h"
-#include "Chicken.h"
+#include "GraphicEngine.h"
+#include <vector>
+#include <stdio.h>
+#include <cstddef>
Gamepad gamepad;
+//create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water)
+//random
+//algoritmic way of generating lanes
+
//size of each each tile in the game
int grid = 8;
+int heightLcd = 48;
+int widthLcd = 84;
-//create a chicken object
-Chicken chicken(4,8,grid);
+//make one object of chicken
+//use this to move around the lcd
+Chicken chicken(6, 6, 6);
+Chicken *chickenptr= &chicken;
+
+//there will be multiple cars
+Car cars[4];
+
+//class that whill show objects
+GraphicEngine graphics(chickenptr);
//main function that starts the game
-void CrossyChicken::start(){
+void CrossyChicken::start(){
//game setup
- lcd.init();
- lcd.setContrast(0.4);
- lcd.backLightOn();
+ graphics.init();
+ graphics.contrast();
+ graphics.backLightOn();
gamepad.init();
-
+
//keep reading and processing user input
- while(1) {
- lcd.clear();
- chicken.show();
+ while(1) {
+ graphics.clear();
+ graphics.showChicken();
+ moveCar();
+ //move();
+ //check if they have interesected
+
+ graphics.showChicken();
process_input();
- lcd.refresh();
- wait_ms(10);
+ graphics.refresh();
+ wait_ms(100);
}
}
-
-void CrossyChicken::game(){
- chicken.makeChicken();
- chicken.show();
-}
-
//A moves right
//X moves upward
//B moves downward
//Y moves left
void CrossyChicken::process_input() {
- //make chicken then update its location based on input
- chicken.makeChicken();
-
- //determine the input
- while(1) {
- //lcd.clear();
- if(gamepad.A_pressed()){
- chicken.moveChicken(1,0);
- } else if(gamepad.X_pressed()){
- chicken.moveChicken(0,-1);
- } else if(gamepad.B_pressed()){
- chicken.moveChicken(0,1);
- } else if(gamepad.Y_pressed()){
- chicken.moveChicken(-1,0);
- }
-
- }
+ //determine the input
+ if(gamepad.A_pressed()){
+ moveChicken(1,0);
+ } else if(gamepad.X_pressed()){
+ moveChicken(0,-1);
+ } else if(gamepad.B_pressed()){
+ moveChicken(0,1);
+ } else if(gamepad.Y_pressed()){
+ moveChicken(-1,0);
+ }
}
-void CrossyChicken::clear(){
- lcd.clear();
+//moves the chicken around the grid
+void CrossyChicken::moveChicken(int xWay, int yWay){
+ //increment the left side of the chicken by a value of the grid size
+ chicken.leftSide += xWay * 8;
+
+ //increment the top side by a value of grid sizw
+ chicken.topSide += yWay * 8;
+
+ //display the new state of the chicken
+ graphics.showChicken();
+
+ wait_ms(30);
}
-void CrossyChicken::refresh(){
- lcd.refresh();
+void CrossyChicken::moveCar() {
+ for(int y = 0; y < 3; y++){
+ cars[y].update();
+ graphics.showCar(cars);
+
+ // if((cars[y].collision(chickenptr))){
+ // cars[y].stop();
+ //}
+ }
}
\ No newline at end of file
--- a/CrossyChicken/CrossyChicken.h Mon Apr 13 17:08:55 2020 +0000
+++ b/CrossyChicken/CrossyChicken.h Mon May 04 03:55:03 2020 +0000
@@ -1,8 +1,11 @@
#ifndef CROSSYCHICKEN_H
#define CROSSYCHICKEN_H
+
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
+#include "Chicken.h"
+#include "Car.h"
struct Input{
//UP,
@@ -20,8 +23,18 @@
void game();
void refresh();
- N5110 lcd;
-
+
+ void moveChicken(int xWay, int yWay);
+
+ void moveCar();
+ void move();
+
+ void setup();
+
+
+
+
+ private:
};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/GraphicEngine/GraphicEngine.cpp Mon May 04 03:55:03 2020 +0000
@@ -0,0 +1,51 @@
+#include "GraphicEngine.h"
+
+N5110 lcd;
+GraphicEngine::GraphicEngine(Chicken *chicken){
+ this->chick = chicken;
+}
+
+void GraphicEngine::init(){
+ lcd.init();
+}
+
+void GraphicEngine::showChicken(){
+ //fill the chicken with black color
+ lcd.drawRect(chick->leftSide, chick->topSide, chick->rightSide, chick->bottomSide, FILL_BLACK);
+}
+
+void GraphicEngine::showCar(Car cars[]){
+ //first car
+ lcd.drawRect(cars[0].left, 48 - 8*2, 8*2, 8, FILL_BLACK);
+
+ //second car
+ lcd.drawRect(cars[1].left + 40, 48 - 8*2, 8*2, 8, FILL_BLACK);
+
+ //first car
+ lcd.drawRect(cars[2].left, 48 - 8*2, 8*3, 8, FILL_BLACK);
+
+ //second car
+ lcd.drawRect(cars[3].left + 20, 48 - 8*3, 8, 8, FILL_BLACK);
+
+ if(cars[0].left > 70 && cars[1].left > 100){
+ cars[0].left = -cars[0].right;
+ cars[1].left = -cars[1].right;
+ //wait_ms(100);
+ }
+}
+
+void GraphicEngine::contrast(){
+ lcd.setContrast(0.4);
+}
+
+void GraphicEngine::clear(){
+ lcd.clear();
+}
+
+void GraphicEngine::refresh(){
+ lcd.refresh();
+}
+
+void GraphicEngine::backLightOn(){
+ lcd.backLightOn();
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/GraphicEngine/GraphicEngine.h Mon May 04 03:55:03 2020 +0000
@@ -0,0 +1,44 @@
+#ifndef GRAPHICENGINE_H
+#define GRAPHICENGINE_H
+
+#include "Car.h"
+#include "Chicken.h"
+#include "Square.h"
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+
+class GraphicEngine {
+
+ public:
+ Car *car1;
+ Chicken *chick;
+
+ GraphicEngine(Chicken *chicken);
+
+ N5110 lcd;
+
+ //initialize the LCD
+ void init();
+
+ //draws the chicken to the LCD screen
+ void showChicken();
+
+ //draws an individual car to the screen
+ void showCar(Car car[]);
+
+ void clear();
+
+ void refresh();
+
+ void contrast();
+
+ void backLightOn();
+
+};
+
+#endif
+
+
\ No newline at end of file
--- a/GridSquare/Square.cpp Mon Apr 13 17:08:55 2020 +0000
+++ b/GridSquare/Square.cpp Mon May 04 03:55:03 2020 +0000
@@ -18,25 +18,3 @@
this->height = height;
}
-/*
-* Function determines if current object and another object are colliding
-*@param Sqaure
-*/
-bool Square::collision(Square square){
- leftSide = x;
- rightSide = width + x;
- topSide = y;
- bottomSide = height + y;
-
- //set up the rectangle of the other square
- int otherLeft = square.x;
- int otherRight = square.x + square.width;
- int otherTop = square.y;
- int otherBottom = square.y + square.height;
-
- //return the inverse of the result
- return !(leftSide > otherRight || rightSide < otherLeft
- || bottomSide < otherTop || topSide > otherBottom);
-}
-
-
--- a/GridSquare/Square.h Mon Apr 13 17:08:55 2020 +0000
+++ b/GridSquare/Square.h Mon May 04 03:55:03 2020 +0000
@@ -18,9 +18,6 @@
//sets the dimensions of square based on the input
void setSquare();
-
- //determine if two squares are collided
- bool collision(Square square);
private:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Obstacles/Car.cpp Mon May 04 03:55:03 2020 +0000
@@ -0,0 +1,43 @@
+#include "Car.h"
+#include "CrossyChicken.h"
+
+Car::Car(){
+ this->x = 8;
+ this->y = 4;
+ this->width= 8;
+ this->height = 8;
+
+ left = x;
+ right = width + x;
+ top = y;
+ bottom = height + y;
+}
+
+void Car::update(){
+ left += 1.5;
+}
+
+void Car::stop(){
+ bottom = 40;
+
+}
+
+bool Car::collision(Chicken *chicken){
+ left = x;
+ right = width + x;
+ top = y;
+ bottom = height + y;
+
+ //set up the rectangle of the other square
+ int otherLeft = chicken->x;
+ int otherRight = chicken->x + chicken->width;
+ int otherTop = chicken->y;
+ int otherBottom = chicken->y + chicken->height;
+
+ //return the inverse of the result
+ return !(left >= otherRight || right <= otherLeft
+ || bottom <= otherTop || top >= otherBottom);
+}
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Obstacles/Car.h Mon May 04 03:55:03 2020 +0000
@@ -0,0 +1,33 @@
+#ifndef CAR_H
+#define CAR_H
+
+#include "Square.h"
+#include "Chicken.h"
+#include "Gamepad.h"
+#include "mbed.h"
+#include "N5110.h"
+
+
+class Car {
+ public:
+ Car();
+
+ void makeCar();
+ void moveCar(Car *car);
+ void update();
+ void showCar(N5110 &lcd);
+ bool collision(Chicken *chicken);
+ void stop();
+
+ float left;
+ float right;
+ float top;
+ float bottom;
+
+ int x;
+ int y;
+ int width;
+ int height;
+
+};
+#endif
\ No newline at end of file