diff --git a/examples/source/main.cpp b/examples/source/main.cpp index 017a486bcf3bcd0ccea0331c68cbff924ba599b3..4c82e31766135776a3485f4688bb242cb38c5310 100644 --- a/examples/source/main.cpp +++ b/examples/source/main.cpp @@ -1,122 +1,138 @@ -/** +/* + * File: main.cpp * Author: Terence Gonsalves * Date: 08/01/2019 - * Desc: Basic Micro:bit example taken from BBC examples. - * Copyright: University of West of England 2017 + * Desc: Simple Pong Game + * Copyright: University of West of England 2019 */ + + #include "MicroBit.h" MicroBit uBit; -int paddle = 2; -int ball_y = 2; -int ball_x = 1; -int ball_x_velocity = 1; -int ball_y_velocity = 1; -bool gamerunning = false; +//variables assigned +int vertical_axis = 2; +int horizontal_axis = 1; +int left_paddle = 2; +int horizontal_motion = 1; +int vertical_motion = 1; +bool loading_game = false; -void MoveUp(MicroBitEvent) //function to move player paddle up. +//implementation to move down. +void MoveDown(MicroBitEvent) { - gamerunning = true; + loading_game = true; - if (paddle > 0) + if (left_paddle < 4) { - uBit.display.image.setPixelValue(0,paddle,0); - paddle -= 1; - uBit.display.image.setPixelValue(0,paddle,255); + uBit.display.image.setPixelValue(0,left_paddle,0); + left_paddle += 1; + uBit.display.image.setPixelValue(0,left_paddle,255); } - } -void MoveDown(MicroBitEvent) //function to move player paddle down. +//implementation to move up. +void MoveUp(MicroBitEvent) { - gamerunning = true; + loading_game = true; - if (paddle < 4) + if (left_paddle > 0) { - uBit.display.image.setPixelValue(0,paddle,0); - paddle += 1; - uBit.display.image.setPixelValue(0,paddle,255); + uBit.display.image.setPixelValue(0,left_paddle,0); + left_paddle -= 1; + uBit.display.image.setPixelValue(0,left_paddle,255); } } +//function displays message void gameover() { - gamerunning = false; + loading_game = false; uBit.display.clear(); uBit.display.print("GAME OVER"); } -void wall_switch() //if ball hits wall switch direction +//switch function for wall when ball hits +void wall_switch() { - ball_x_velocity = -1 * ball_x_velocity; + horizontal_motion = -1 * horizontal_motion; } -void paddle_switch() //if ball hits paddle switch direction +//if ball hits bottom of display switch direction +void bottom_switch() { - ball_x_velocity = -1 * ball_x_velocity; + vertical_motion = -1 * vertical_motion; } -void bottom_switch() // if ball hits bottom of display switch direction +//switch function for left_paddle when ball hits +void left_paddle_switch() { - ball_y_velocity = -1 * ball_y_velocity; + horizontal_motion = -1 * horizontal_motion; } -int main() +int main() { + uBit.init(); - uBit.init(); - - //Place inital paddle and ball locations onto display. - uBit.display.image.setPixelValue(0,paddle,255); - uBit.display.image.setPixelValue(ball_x,ball_y,255); + //Place inital left_paddle and ball locations onto display. + uBit.display.image.setPixelValue(0,left_paddle,255); + uBit.display.image.setPixelValue(horizontal_axis,vertical_axis,255); - //Wait for user to press button to trigger function to start pong game. - uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, MoveUp); - uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, MoveDown); + //Wait for user to press button to trigger function to start pong game. + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, MoveUp); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, MoveDown); - while(1) + while(1) { - if (gamerunning == true) - { + if (loading_game == true) - if (ball_x < 0) { - gameover(); - } + if (horizontal_axis < 0) - else - { - if (ball_x == 4) { - wall_switch(); - } - if (ball_x == 1 && ball_y == paddle) - { - paddle_switch(); + gameover(); } - if (ball_y == 0 || ball_y == 4) + else { - bottom_switch(); - } + + if (horizontal_axis == 4) + { + wall_switch(); + } + + if (horizontal_axis == 1 && vertical_axis == left_paddle) + { + left_paddle_switch(); + } + + if (vertical_axis == 0 || vertical_axis == 4) + { + bottom_switch(); + } + //move ball along display, by turning led off and on. - uBit.display.image.setPixelValue(ball_x,ball_y,0);//off + + //turns off + uBit.display.image.setPixelValue(horizontal_axis,vertical_axis,0); //Give ball velocity - ball_x = ball_x + ball_x_velocity; - ball_y = ball_y + ball_y_velocity; - uBit.display.image.setPixelValue(ball_x,ball_y,255);//on + horizontal_axis = horizontal_axis + horizontal_motion; + vertical_axis = vertical_axis + vertical_motion; + + //turns on + uBit.display.image.setPixelValue(horizontal_axis,vertical_axis,255); } } - uBit.sleep(400); //place sleep to yield control back to scheduler + //sleep to yield control back to scheduler + uBit.sleep(400); } release_fiber(); - -}; + };