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 1:09a835e6d063, committed 2020-06-02
- Comitter:
- Psy1990
- Date:
- Tue Jun 02 21:33:00 2020 +0000
- Parent:
- 0:7423345f87c5
- Child:
- 2:c6772c5ab69d
- Commit message:
- For some reason my original project wasn't working so had to make a new one. Even though all the files were exactly the same (I coppied and pasted several times) the first project wouldn't do anything :( This project works for now
Changed in this revision
--- a/Ball/Ball-test.h Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-#ifndef BALL_TEST_H
-#define BALL_TEST_H
-
-/**
- * \brief Check that Ball object goes to correct position when moved
- *
- * \returns true if all the tests passed
- */
-bool Ball_test_movement()
-{
- // Initialise Ball object with a size of 2, and speed of 1
- Ball ball;
- ball.init(2, 1);
-
- // Set the position to 5, 5
- Vector2D initial_pos = {5, 5};
- ball.set_pos(initial_pos);
-
- // Read the position
- Vector2D read_pos_1 = ball.get_pos();
- printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
-
- // Set the velocity to -2, 3
- Vector2D velocity = {-2, 3};
- ball.set_velocity(velocity);
-
- // Update the position
- ball.update();
-
- // Read the position
- Vector2D read_pos_2 = ball.get_pos();
- printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
-
- // Now check that both the positions are as expected
- bool success_flag = true;
-
- // Fail the test if the initial position is wrong
- if (read_pos_1.x != 5 || read_pos_1.y != 5) {
- success_flag = false;
- }
-
- // Fail the test if the final position is wrong
- if (read_pos_2.x != 3 || read_pos_2.y != 8) {
- success_flag = false;
- }
-
- return success_flag;
-}
-#endif
\ No newline at end of file
--- a/Ball/Ball.cpp Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#include "Ball.h"
-
-Ball::Ball()
-{
-
-}
-
-Ball::~Ball()
-{
-
-}
-
-void Ball::init(int size,int speed)
-{
- _size = size;
-
- _x = WIDTH/2 - _size/2;
- _y = HEIGHT/2 - _size/2;
-
- srand(time(NULL));
- int direction = rand() % 4; // randomise initial direction.
-
- // 4 possibilities. Get random modulo and set velocities accordingly
- if (direction == 0) {
- _velocity.x = speed;
- _velocity.y = speed;
- } else if (direction == 1) {
- _velocity.x = speed;
- _velocity.y = -speed;
- } else if (direction == 2) {
- _velocity.x = speed;
- _velocity.y = speed;
- } else {
- _velocity.x = -speed;
- _velocity.y = -speed;
- }
-}
-
-void Ball::draw(N5110 &lcd)
-{
- lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
-}
-
-void Ball::update()
-{
- _x += _velocity.x;
- _y += _velocity.y;
-}
-
-void Ball::set_velocity(Vector2D v)
-{
- _velocity.x = v.x;
- _velocity.y = v.y;
-}
-
-Vector2D Ball::get_velocity()
-{
- Vector2D v = {_velocity.x,_velocity.y};
- return v;
-}
-
-Vector2D Ball::get_pos()
-{
- Vector2D p = {_x,_y};
- return p;
-}
-
-void Ball::set_pos(Vector2D p)
-{
- _x = p.x;
- _y = p.y;
-}
\ No newline at end of file
--- a/Ball/Ball.h Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-#ifndef BALL_H
-#define BALL_H
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-#include "Paddle.h"
-
-/** Ball Class
-@author Dr Craig A. Evans, University of Leeds
-@brief Controls the ball in the Pong game
-@date Febraury 2017
-*/
-class Ball
-{
-
-public:
- Ball();
- ~Ball();
- void init(int size,int speed);
- void draw(N5110 &lcd);
- void update();
- /// accessors and mutators
- void set_velocity(Vector2D v);
- Vector2D get_velocity();
- Vector2D get_pos();
- void set_pos(Vector2D p);
-
-private:
-
- Vector2D _velocity;
- int _size;
- int _x;
- int _y;
-};
-#endif
\ No newline at end of file
--- a/Paddle/Paddle.cpp Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-#include "Paddle.h"
-
-// nothing doing in the constructor and destructor
-Paddle::Paddle()
-{
-
-}
-
-Paddle::~Paddle()
-{
-
-}
-
-void Paddle::init(int x,int height,int width)
-{
- _x = x; // x value on screen is fixed
- _y = HEIGHT/2 - height/2; // y depends on height of screen and height of paddle
- _height = height;
- _width = width;
- _speed = 1; // default speed
- _score = 0; // start score from zero
-
-}
-
-void Paddle::draw(N5110 &lcd)
-{
- // draw paddle in screen buffer.
- lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
-}
-
-void Paddle::update(Direction d,float mag)
-{
- _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
-
- // update y value depending on direction of movement
- // North is decrement as origin is at the top-left so decreasing moves up
- if (d == N) {
- _y-=_speed;
- } else if (d == S) {
- _y+=_speed;
- }
-
- // check the y origin to ensure that the paddle doesn't go off screen
- if (_y < 1) {
- _y = 1;
- }
- if (_y > HEIGHT - _height - 1) {
- _y = HEIGHT - _height - 1;
- }
-}
-
-void Paddle::add_score()
-{
- _score++;
-}
-int Paddle::get_score()
-{
- return _score;
-}
-
-Vector2D Paddle::get_pos() {
- Vector2D p = {_x,_y};
- return p;
-}
\ No newline at end of file
--- a/Paddle/Paddle.h Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-#ifndef PADDLE_H
-#define PADDLE_H
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-
-class Paddle
-{
-public:
-
- Paddle();
- ~Paddle();
- void init(int x,int height,int width);
- void draw(N5110 &lcd);
- void update(Direction d,float mag);
- void add_score();
- int get_score();
- Vector2D get_pos();
-
-private:
-
- int _height;
- int _width;
- int _x;
- int _y;
- int _speed;
- int _score;
-
-};
-#endif
\ No newline at end of file
--- a/PongEngine/PongEngine.cpp Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,174 +0,0 @@
-#include "PongEngine.h"
-
-PongEngine::PongEngine()
-{
-
-}
-
-PongEngine::~PongEngine()
-{
-
-}
-
-void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
-{
- // initialise the game parameters
- _paddle_width = paddle_width;
- _paddle_height = paddle_height;
- _ball_size = ball_size;
- _speed = speed;
-
- // x position on screen - WIDTH is defined in N5110.h
- _p1x = GAP;
- _p2x = WIDTH - GAP - _paddle_width;
-
- // puts paddles and ball in middle
- _p1.init(_p1x,_paddle_height,_paddle_width);
- _p2.init(_p2x,_paddle_height,_paddle_width);
- _ball.init(_ball_size,_speed);
-}
-
-void PongEngine::read_input(Gamepad &pad)
-{
- _d = pad.get_direction();
- _mag = pad.get_mag();
-}
-
-void PongEngine::draw(N5110 &lcd)
-{
- // draw the elements in the LCD buffer
- // pitch
- lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
- //score
- print_scores(lcd);
- // paddles
- _p1.draw(lcd);
- _p2.draw(lcd);
- // ball
- _ball.draw(lcd);
-}
-
-void PongEngine::update(Gamepad &pad)
-{
- check_goal(pad);
- // important to update paddles and ball before checking collisions so can
- // correct for it before updating the display
- _p1.update(_d,_mag);
- _p2.update(_d,_mag);
- _ball.update();
-
- check_wall_collision(pad);
- check_paddle_collisions(pad);
-}
-
-void PongEngine::check_wall_collision(Gamepad &pad)
-{
- // read current ball attributes
- Vector2D ball_pos = _ball.get_pos();
- Vector2D ball_velocity = _ball.get_velocity();
-
- // check if hit top wall
- if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
- ball_pos.y = 1; // bounce off ceiling without going off screen
- ball_velocity.y = -ball_velocity.y;
- // audio feedback
- pad.tone(750.0,0.1);
- }
- // check if hit bottom wall
- else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47
- // hit bottom
- ball_pos.y = (HEIGHT-1) - _ball_size; // stops ball going off screen
- ball_velocity.y = -ball_velocity.y;
- // audio feedback
- pad.tone(750.0,0.1);
- }
-
- // update ball parameters
- _ball.set_velocity(ball_velocity);
- _ball.set_pos(ball_pos);
-}
-
-void PongEngine::check_paddle_collisions(Gamepad &pad)
-{
- // read current ball attributes
- Vector2D ball_pos = _ball.get_pos();
- Vector2D ball_velocity = _ball.get_velocity();
-
- // check p1 first
- Vector2D p1_pos = _p1.get_pos();
-
- // see if ball has hit the paddle by checking for overlaps
- if (
- (ball_pos.y >= p1_pos.y) && //top
- (ball_pos.y <= p1_pos.y + _paddle_height) && //bottom
- (ball_pos.x >= _p1x) && //left
- (ball_pos.x <= _p1x + _paddle_width) //right
- ) {
- // if it has, fix position and reflect x velocity
- ball_pos.x = _p1x + _paddle_width;
- ball_velocity.x = -ball_velocity.x;
- // audio feedback
- pad.tone(1000.0,0.1);
- }
-
- // check p2 next
- Vector2D p2_pos = _p2.get_pos();
-
- // see if ball has hit the paddle by checking for overlaps
- if (
- (ball_pos.y >= p2_pos.y) && //top
- (ball_pos.y <= p2_pos.y + _paddle_height) && //bottom
- (ball_pos.x + _ball_size >= _p2x) && //left
- (ball_pos.x + _ball_size <= _p2x + _paddle_width) //right
- ) {
- // if it has, fix position and reflect x velocity
- ball_pos.x = _p2x - _ball_size;
- ball_velocity.x = -ball_velocity.x;
- // audio feedback
- pad.tone(1000.0,0.1);
- }
-
- // write new attributes
- _ball.set_velocity(ball_velocity);
- _ball.set_pos(ball_pos);
-}
-
-void PongEngine::check_goal(Gamepad &pad)
-{
- Vector2D ball_pos = _ball.get_pos();
- // P2 has scored
- if (ball_pos.x + _ball_size < 0) {
- _p2.add_score();
- _ball.init(_ball_size,_speed);
- pad.tone(1500.0,0.5);
- pad.leds_on();
- wait(0.5);
- pad.leds_off();
- }
-
- // P1 has scored
- if (ball_pos.x > WIDTH) {
- _p1.add_score();
- _ball.init(_ball_size,_speed);
- pad.tone(1500.0,0.5);
- pad.leds_on();
- wait(0.5);
- pad.leds_off();
- }
-}
-
-void PongEngine::print_scores(N5110 &lcd)
-{
- // get scores from paddles
- int p1_score = _p1.get_score();
- int p2_score = _p2.get_score();
-
- // print to LCD i
- char buffer1[14];
- sprintf(buffer1,"%2d",p1_score);
- lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
- char buffer2[14];
- sprintf(buffer2,"%2d",p2_score);
- lcd.printString(buffer2,WIDTH/2 + 4,1);
-}
\ No newline at end of file
--- a/PongEngine/PongEngine.h Fri Jan 31 12:32:38 2020 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-#ifndef PONGENGINE_H
-#define PONGENGINE_H
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-#include "Ball.h"
-#include "Paddle.h"
-
-// gap from edge of screen
-#define GAP 2
-
-class PongEngine
-{
-
-public:
- PongEngine();
- ~PongEngine();
-
- void init(int paddle_width,int paddle_height,int ball_size,int speed);
- void read_input(Gamepad &pad);
- void update(Gamepad &pad);
- void draw(N5110 &lcd);
-
-private:
-
- void check_wall_collision(Gamepad &pad);
- void check_paddle_collisions(Gamepad &pad);
- void check_goal(Gamepad &pad);
- void print_scores(N5110 &lcd);
-
- Paddle _p1;
- Paddle _p2;
-
- int _paddle_width;
- int _paddle_height;
- int _ball_size;
- int _speed;
-
- // x positions of the paddles
- int _p1x;
- int _p2x;
-
- Ball _ball;
-
- Direction _d;
- float _mag;
-
-};
-
-#endif
\ No newline at end of file
--- a/main.cpp Fri Jan 31 12:32:38 2020 +0000
+++ b/main.cpp Tue Jun 02 21:33:00 2020 +0000
@@ -1,17 +1,21 @@
+/*
+ELEC2645 Embedded Systems Project
+School of Electronic & Electrical Engineering
+University of Leeds
+2019/20
+
+Name: Simon Thackray Atkinson
+Username: el18s2a
+Student ID Number: 201255483
+Date: 05/03/2020
+*/
+
///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
-#include "PongEngine.h"
-#ifdef WITH_TESTING
-# include "tests.h"
-#endif
-#define PADDLE_WIDTH 2
-#define PADDLE_HEIGHT 8
-#define BALL_SIZE 2
-#define BALL_SPEED 3
/////////////// structs /////////////////
struct UserInput {
@@ -21,76 +25,51 @@
/////////////// objects ///////////////
N5110 lcd;
Gamepad pad;
-PongEngine pong;
+
-///////////// prototypes ///////////////
-void init();
-void update_game(UserInput input);
-void render();
+
void welcome();
///////////// functions ////////////////
+
int main()
{
-#ifdef WITH_TESTING
- int number_of_failures = run_all_tests();
-
- if(number_of_failures > 0) return number_of_failures;
-#endif
-
- int fps = 6; // frames per second
-
- init(); // initialise and then display welcome screen...
- welcome(); // waiting for the user to start
-
- render(); // first draw the initial frame
- wait(1.0f/fps); // and wait for one frame period
-
-
- // game loop - read input, update the game state and render the display
- while (1) {
- pong.read_input(pad);
- pong.update(pad);
- render();
- wait(1.0f/fps);
- }
-}
+ // first need to initialise display
+ lcd.init();
-// initialies all classes and libraries
-void init()
-{
- // need to initialise LCD and Gamepad
- lcd.init();
- pad.init();
-
- // initialise the game with correct ball and paddle sizes
- pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
-
-}
-
-// this function draws each frame on the LCD
-void render()
-{
- // clear screen, re-draw and refresh
- lcd.clear();
- pong.draw(lcd);
- lcd.refresh();
-}
-
-// simple splash screen displayed on start-up
-void welcome() {
-
- lcd.printString(" Pong! ",0,1);
+ while(1) {
+ // Splash Screen Info
+ lcd.clear(); // we need to clear the screen first
+ lcd.printString(" Author ",0,1);
+ lcd.printString("Simon Atkinson",0,2);
+ lcd.printString(" 201255483 ",0,3);
+ lcd.printString(" Uni of Leeds ",0,4);
+ lcd.refresh(); // need to refresh display after setting pixels or writing strings
+ wait(1.0); // we don't want this screen on long!
+
+ // main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun!
+ lcd.clear();
+ lcd.printString(" Welcome to ",0,1);
+ lcd.printString(" Snake! ",0,2);
lcd.printString(" Press Start ",0,4);
lcd.refresh();
-
- // wait flashing LEDs until start button is pressed
- while ( pad.start_pressed() == false) {
- lcd.setContrast( pad.read_pot1());
- pad.leds_on();
- wait(0.1);
- pad.leds_off();
- wait(0.1);
- }
-
-}
\ No newline at end of file
+ wait(45.0);
+ lcd.clear();
+
+ // Easter Egg time!
+ lcd.printString(" Do you need ",0,1);
+ lcd.printString(" more time? ",0,2);
+ lcd.printString(" Grandpa! ",0,3);
+ lcd.refresh();
+ wait(5.0);
+
+ //returns back to normal welcome screen
+ lcd.clear();
+ lcd.printString(" Welcome to ",0,1);
+ lcd.printString(" Snake! ",0,2);
+ lcd.printString(" Press Start ",0,4);
+ lcd.refresh();
+ break; //stops the loop
+
+}
+}