ELEC2645 (2015/16) / Mbed 2 deprecated HarryPetrovicPong

Dependencies:   N5110 mbed

Fork of 2645_Physics_Engine_Example by Craig Evans

Committer:
HarryPetrovic1
Date:
Thu May 05 14:18:21 2016 +0000
Revision:
1:6632d8423c65
Pong

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HarryPetrovic1 1:6632d8423c65 1 #include "mbed.h"
HarryPetrovic1 1:6632d8423c65 2 #include "N5110.h"
HarryPetrovic1 1:6632d8423c65 3 #include "data.h"
HarryPetrovic1 1:6632d8423c65 4
HarryPetrovic1 1:6632d8423c65 5 /**@file functions.h
HarryPetrovic1 1:6632d8423c65 6 @brief File containing functions and actions within the game
HarryPetrovic1 1:6632d8423c65 7 @author Harry Petrovic
HarryPetrovic1 1:6632d8423c65 8 @date May 2016
HarryPetrovic1 1:6632d8423c65 9 */
HarryPetrovic1 1:6632d8423c65 10
HarryPetrovic1 1:6632d8423c65 11
HarryPetrovic1 1:6632d8423c65 12
HarryPetrovic1 1:6632d8423c65 13 /**initialises display so that you can view images upon LCD screen*/
HarryPetrovic1 1:6632d8423c65 14
HarryPetrovic1 1:6632d8423c65 15 void init_display();
HarryPetrovic1 1:6632d8423c65 16
HarryPetrovic1 1:6632d8423c65 17 /** sets the initial position, velocity and acceleration when the game starts and when the computer scores
HarryPetrovic1 1:6632d8423c65 18
HarryPetrovic1 1:6632d8423c65 19 @param pos.x - initial x position
HarryPetrovic1 1:6632d8423c65 20
HarryPetrovic1 1:6632d8423c65 21 @param pos.y - initial y position
HarryPetrovic1 1:6632d8423c65 22
HarryPetrovic1 1:6632d8423c65 23 @param vel.x - initial x direction velocity
HarryPetrovic1 1:6632d8423c65 24
HarryPetrovic1 1:6632d8423c65 25 @param vel.y -initial y direction velocity
HarryPetrovic1 1:6632d8423c65 26
HarryPetrovic1 1:6632d8423c65 27 @param acc.x - initial x direction acceleration, starts it off moving towards the right
HarryPetrovic1 1:6632d8423c65 28
HarryPetrovic1 1:6632d8423c65 29 @param acc.y -initial y direction acceleration, starts it off moving towards ground
HarryPetrovic1 1:6632d8423c65 30 */
HarryPetrovic1 1:6632d8423c65 31
HarryPetrovic1 1:6632d8423c65 32
HarryPetrovic1 1:6632d8423c65 33 void init_ball();
HarryPetrovic1 1:6632d8423c65 34
HarryPetrovic1 1:6632d8423c65 35
HarryPetrovic1 1:6632d8423c65 36 /** sets the initial position, velocity and acceleration when the player scores
HarryPetrovic1 1:6632d8423c65 37
HarryPetrovic1 1:6632d8423c65 38 @param pos.x - initial x position
HarryPetrovic1 1:6632d8423c65 39
HarryPetrovic1 1:6632d8423c65 40 @param pos.y - initial y position
HarryPetrovic1 1:6632d8423c65 41
HarryPetrovic1 1:6632d8423c65 42 @param vel.x - initial x direction velocity
HarryPetrovic1 1:6632d8423c65 43
HarryPetrovic1 1:6632d8423c65 44 @param vel.y -initial y direction velocity
HarryPetrovic1 1:6632d8423c65 45
HarryPetrovic1 1:6632d8423c65 46 @param acc.x - initial x direction acceleration, starts it off moving towards the left
HarryPetrovic1 1:6632d8423c65 47
HarryPetrovic1 1:6632d8423c65 48 @param acc.y -initial y direction acceleration, starts it off moving towards ground
HarryPetrovic1 1:6632d8423c65 49 */
HarryPetrovic1 1:6632d8423c65 50
HarryPetrovic1 1:6632d8423c65 51 void player_Score_Position();
HarryPetrovic1 1:6632d8423c65 52
HarryPetrovic1 1:6632d8423c65 53 void game_timer_isr();
HarryPetrovic1 1:6632d8423c65 54
HarryPetrovic1 1:6632d8423c65 55
HarryPetrovic1 1:6632d8423c65 56 /** Draws the ball and updates the screen, called from the Nokia 5110 library
HarryPetrovic1 1:6632d8423c65 57 taken from "2645_Physics_Engine_Example"
HarryPetrovic1 1:6632d8423c65 58
HarryPetrovic1 1:6632d8423c65 59 @param pos.x -the position of the ball is drawn, corresponding to the calculated position.
HarryPetrovic1 1:6632d8423c65 60
HarryPetrovic1 1:6632d8423c65 61 @param pos.y-the position of the ball is drawn, corresponding to the calculated position.
HarryPetrovic1 1:6632d8423c65 62
HarryPetrovic1 1:6632d8423c65 63 @param radius - deffined as 2
HarryPetrovic1 1:6632d8423c65 64
HarryPetrovic1 1:6632d8423c65 65 @param fill - set as 0.5 so that it is just an outline
HarryPetrovic1 1:6632d8423c65 66
HarryPetrovic1 1:6632d8423c65 67
HarryPetrovic1 1:6632d8423c65 68 */
HarryPetrovic1 1:6632d8423c65 69 void redraw_screen();
HarryPetrovic1 1:6632d8423c65 70
HarryPetrovic1 1:6632d8423c65 71
HarryPetrovic1 1:6632d8423c65 72 /** Calculates the new position (pos+vel) and velocity (vel+acc) of the ball
HarryPetrovic1 1:6632d8423c65 73 taken from 2645_Physics_Engine_Example and subsequently adapted
HarryPetrovic1 1:6632d8423c65 74
HarryPetrovic1 1:6632d8423c65 75 @param vel.x - The current x velocity of ball
HarryPetrovic1 1:6632d8423c65 76
HarryPetrovic1 1:6632d8423c65 77 @param vel.y - The current y velocity of ball
HarryPetrovic1 1:6632d8423c65 78
HarryPetrovic1 1:6632d8423c65 79 @param acc.x - The current x acceleration of ball
HarryPetrovic1 1:6632d8423c65 80
HarryPetrovic1 1:6632d8423c65 81 @param acc.y - The current y acceleration of ball
HarryPetrovic1 1:6632d8423c65 82
HarryPetrovic1 1:6632d8423c65 83 @param pos.x The current x position of ball
HarryPetrovic1 1:6632d8423c65 84
HarryPetrovic1 1:6632d8423c65 85 @param pos.y - The current y position of ball
HarryPetrovic1 1:6632d8423c65 86
HarryPetrovic1 1:6632d8423c65 87 @returns vel.x - New calculated x velocity of ball
HarryPetrovic1 1:6632d8423c65 88
HarryPetrovic1 1:6632d8423c65 89 @returns vel.y - New calculated y velocity of ball
HarryPetrovic1 1:6632d8423c65 90
HarryPetrovic1 1:6632d8423c65 91 @returns pos.x - New calculated x position of ball
HarryPetrovic1 1:6632d8423c65 92
HarryPetrovic1 1:6632d8423c65 93 @returns pos.y- New calculated y position of ball
HarryPetrovic1 1:6632d8423c65 94
HarryPetrovic1 1:6632d8423c65 95 */
HarryPetrovic1 1:6632d8423c65 96
HarryPetrovic1 1:6632d8423c65 97
HarryPetrovic1 1:6632d8423c65 98 void update_physics_engine();
HarryPetrovic1 1:6632d8423c65 99
HarryPetrovic1 1:6632d8423c65 100 /** Checks whether or not the ball has come into contact with the paddles, roof or the floor
HarryPetrovic1 1:6632d8423c65 101 taken from 2645_Physics_Engine_Example and subsequently adapted
HarryPetrovic1 1:6632d8423c65 102
HarryPetrovic1 1:6632d8423c65 103 @param pos.x - The current x position of ball
HarryPetrovic1 1:6632d8423c65 104
HarryPetrovic1 1:6632d8423c65 105 @param pos.y - The current x position of ball
HarryPetrovic1 1:6632d8423c65 106
HarryPetrovic1 1:6632d8423c65 107 @param yc - The position of the paddle
HarryPetrovic1 1:6632d8423c65 108
HarryPetrovic1 1:6632d8423c65 109 @param yp - the position of user paddle
HarryPetrovic1 1:6632d8423c65 110
HarryPetrovic1 1:6632d8423c65 111 @return pos.x - if contact has been made new position is given
HarryPetrovic1 1:6632d8423c65 112
HarryPetrovic1 1:6632d8423c65 113 @return pos.y - if contact has been made new position is given
HarryPetrovic1 1:6632d8423c65 114
HarryPetrovic1 1:6632d8423c65 115
HarryPetrovic1 1:6632d8423c65 116 @return vel.x - if contact has been made velocity inverted
HarryPetrovic1 1:6632d8423c65 117
HarryPetrovic1 1:6632d8423c65 118 @return vel.y if contact has been made velocity inverted
HarryPetrovic1 1:6632d8423c65 119
HarryPetrovic1 1:6632d8423c65 120 @return acc.x - if contact has been made acceleration inverted
HarryPetrovic1 1:6632d8423c65 121
HarryPetrovic1 1:6632d8423c65 122 @return acc.y if contact has been made acceleration inverted
HarryPetrovic1 1:6632d8423c65 123
HarryPetrovic1 1:6632d8423c65 124
HarryPetrovic1 1:6632d8423c65 125 */
HarryPetrovic1 1:6632d8423c65 126 void check_collisions();
HarryPetrovic1 1:6632d8423c65 127
HarryPetrovic1 1:6632d8423c65 128
HarryPetrovic1 1:6632d8423c65 129 /** sets up the use of the button
HarryPetrovic1 1:6632d8423c65 130 */
HarryPetrovic1 1:6632d8423c65 131
HarryPetrovic1 1:6632d8423c65 132 void init_K64F();
HarryPetrovic1 1:6632d8423c65 133
HarryPetrovic1 1:6632d8423c65 134
HarryPetrovic1 1:6632d8423c65 135 /** Appears at the start of the game and allows user to choose difficulty
HarryPetrovic1 1:6632d8423c65 136
HarryPetrovic1 1:6632d8423c65 137 @param yin - used to navigate up and down the menu
HarryPetrovic1 1:6632d8423c65 138
HarryPetrovic1 1:6632d8423c65 139 @param difficulty - value of 1,2,3 Easy, Med, Hard, sets value in moveComputerPaddle
HarryPetrovic1 1:6632d8423c65 140
HarryPetrovic1 1:6632d8423c65 141 */
HarryPetrovic1 1:6632d8423c65 142 void menu();
HarryPetrovic1 1:6632d8423c65 143
HarryPetrovic1 1:6632d8423c65 144
HarryPetrovic1 1:6632d8423c65 145 void clearCells();
HarryPetrovic1 1:6632d8423c65 146 void refreshCells();
HarryPetrovic1 1:6632d8423c65 147
HarryPetrovic1 1:6632d8423c65 148 /** allows the user to move their paddle with the joystick
HarryPetrovic1 1:6632d8423c65 149
HarryPetrovic1 1:6632d8423c65 150 @param yin - gives a reading between 0-1. if below 0.4 yp-- occurs if 0.6 yp++. If inbetween yp = yp
HarryPetrovic1 1:6632d8423c65 151
HarryPetrovic1 1:6632d8423c65 152 @returns yp - The co-ordinate of the first pixel within the paddle, moves according to yin value
HarryPetrovic1 1:6632d8423c65 153
HarryPetrovic1 1:6632d8423c65 154 */
HarryPetrovic1 1:6632d8423c65 155 void moveUserPaddle();
HarryPetrovic1 1:6632d8423c65 156
HarryPetrovic1 1:6632d8423c65 157 /** Moves the computer paddle
HarryPetrovic1 1:6632d8423c65 158
HarryPetrovic1 1:6632d8423c65 159
HarryPetrovic1 1:6632d8423c65 160 @param pos.y - value is read
HarryPetrovic1 1:6632d8423c65 161 @param difficulty - value that is used to set how far off the pace the paddle is
HarryPetrovic1 1:6632d8423c65 162
HarryPetrovic1 1:6632d8423c65 163 @returns yc - returned from pos.y-difficulty
HarryPetrovic1 1:6632d8423c65 164 */
HarryPetrovic1 1:6632d8423c65 165 void moveComputerPaddle();
HarryPetrovic1 1:6632d8423c65 166
HarryPetrovic1 1:6632d8423c65 167 /**prevents user paddle moving off screen by forcing position of yp
HarryPetrovic1 1:6632d8423c65 168
HarryPetrovic1 1:6632d8423c65 169 @param yp - the position of the paddle
HarryPetrovic1 1:6632d8423c65 170
HarryPetrovic1 1:6632d8423c65 171 */
HarryPetrovic1 1:6632d8423c65 172 void paddleLimits();
HarryPetrovic1 1:6632d8423c65 173
HarryPetrovic1 1:6632d8423c65 174
HarryPetrovic1 1:6632d8423c65 175 /**prevents computer paddle moving off screen by forcing position of yp
HarryPetrovic1 1:6632d8423c65 176
HarryPetrovic1 1:6632d8423c65 177 @param yc - the position of the paddle
HarryPetrovic1 1:6632d8423c65 178
HarryPetrovic1 1:6632d8423c65 179 */
HarryPetrovic1 1:6632d8423c65 180 void ComputerpaddleLimits();
HarryPetrovic1 1:6632d8423c65 181
HarryPetrovic1 1:6632d8423c65 182 /** Determines which player has scored, if any
HarryPetrovic1 1:6632d8423c65 183
HarryPetrovic1 1:6632d8423c65 184 @param pos.x - The position of the ball
HarryPetrovic1 1:6632d8423c65 185
HarryPetrovic1 1:6632d8423c65 186 @return computerScore - ++ if ball touches left hand side
HarryPetrovic1 1:6632d8423c65 187
HarryPetrovic1 1:6632d8423c65 188 @return playerScore - ++ if ball touches right hand side
HarryPetrovic1 1:6632d8423c65 189 */
HarryPetrovic1 1:6632d8423c65 190
HarryPetrovic1 1:6632d8423c65 191 void Scoring();
HarryPetrovic1 1:6632d8423c65 192
HarryPetrovic1 1:6632d8423c65 193 /** Resets ball to a position, depending on who scored
HarryPetrovic1 1:6632d8423c65 194
HarryPetrovic1 1:6632d8423c65 195 @param pos.x - if hits wall the ball will return to default position depending on who scored
HarryPetrovic1 1:6632d8423c65 196
HarryPetrovic1 1:6632d8423c65 197 */
HarryPetrovic1 1:6632d8423c65 198 void resetBall();
HarryPetrovic1 1:6632d8423c65 199
HarryPetrovic1 1:6632d8423c65 200 /**
HarryPetrovic1 1:6632d8423c65 201
HarryPetrovic1 1:6632d8423c65 202 @param - playerScore -the current score of player
HarryPetrovic1 1:6632d8423c65 203
HarryPetrovic1 1:6632d8423c65 204 @return - message stating victory if playerScore > 10
HarryPetrovic1 1:6632d8423c65 205
HarryPetrovic1 1:6632d8423c65 206
HarryPetrovic1 1:6632d8423c65 207 */
HarryPetrovic1 1:6632d8423c65 208 void victory();
HarryPetrovic1 1:6632d8423c65 209 void redraw_screen();
HarryPetrovic1 1:6632d8423c65 210
HarryPetrovic1 1:6632d8423c65 211 /**
HarryPetrovic1 1:6632d8423c65 212
HarryPetrovic1 1:6632d8423c65 213 @param - computerScore -the current score of computer
HarryPetrovic1 1:6632d8423c65 214
HarryPetrovic1 1:6632d8423c65 215 @return - message stating defeat if playerScore > 10
HarryPetrovic1 1:6632d8423c65 216
HarryPetrovic1 1:6632d8423c65 217
HarryPetrovic1 1:6632d8423c65 218 */
HarryPetrovic1 1:6632d8423c65 219
HarryPetrovic1 1:6632d8423c65 220 void loss();
HarryPetrovic1 1:6632d8423c65 221
HarryPetrovic1 1:6632d8423c65 222
HarryPetrovic1 1:6632d8423c65 223
HarryPetrovic1 1:6632d8423c65 224
HarryPetrovic1 1:6632d8423c65 225
HarryPetrovic1 1:6632d8423c65 226 void init_K64F()
HarryPetrovic1 1:6632d8423c65 227 {
HarryPetrovic1 1:6632d8423c65 228 select.mode(PullDown);
HarryPetrovic1 1:6632d8423c65 229 }
HarryPetrovic1 1:6632d8423c65 230
HarryPetrovic1 1:6632d8423c65 231 void menu()
HarryPetrovic1 1:6632d8423c65 232 {
HarryPetrovic1 1:6632d8423c65 233 while(select) {
HarryPetrovic1 1:6632d8423c65 234 lcd.printString("EASY", 20,1);
HarryPetrovic1 1:6632d8423c65 235 lcd.printString("MED", 20,2);
HarryPetrovic1 1:6632d8423c65 236 lcd.printString("HARD", 20,3);
HarryPetrovic1 1:6632d8423c65 237 if(difficulty == 1) {
HarryPetrovic1 1:6632d8423c65 238
HarryPetrovic1 1:6632d8423c65 239 } else if(difficulty == 2) {
HarryPetrovic1 1:6632d8423c65 240 } else if(difficulty ==3) {
HarryPetrovic1 1:6632d8423c65 241 }
HarryPetrovic1 1:6632d8423c65 242 if(yin < 0.6f) {
HarryPetrovic1 1:6632d8423c65 243 difficulty++;
HarryPetrovic1 1:6632d8423c65 244 } else if(yin >0.4f) {
HarryPetrovic1 1:6632d8423c65 245 difficulty--;
HarryPetrovic1 1:6632d8423c65 246 }
HarryPetrovic1 1:6632d8423c65 247 wait(0.1);
HarryPetrovic1 1:6632d8423c65 248 }
HarryPetrovic1 1:6632d8423c65 249 }
HarryPetrovic1 1:6632d8423c65 250
HarryPetrovic1 1:6632d8423c65 251
HarryPetrovic1 1:6632d8423c65 252
HarryPetrovic1 1:6632d8423c65 253 void clearCells() //loop and clear
HarryPetrovic1 1:6632d8423c65 254 {
HarryPetrovic1 1:6632d8423c65 255 for (int i = 0; i < nx ; i++) {
HarryPetrovic1 1:6632d8423c65 256 for (int j = 0; j < ny ; j++) {
HarryPetrovic1 1:6632d8423c65 257 lcd.clearPixel(i,j);
HarryPetrovic1 1:6632d8423c65 258 }
HarryPetrovic1 1:6632d8423c65 259 }
HarryPetrovic1 1:6632d8423c65 260 lcd.refresh(); //refresh
HarryPetrovic1 1:6632d8423c65 261 }
HarryPetrovic1 1:6632d8423c65 262
HarryPetrovic1 1:6632d8423c65 263 void refreshCells()
HarryPetrovic1 1:6632d8423c65 264 {
HarryPetrovic1 1:6632d8423c65 265 for (int i = 0; i< 84 ; i++) {
HarryPetrovic1 1:6632d8423c65 266 for (int j = 0; j < 48 ; j++) {
HarryPetrovic1 1:6632d8423c65 267 if (Array[i][j]) {
HarryPetrovic1 1:6632d8423c65 268 lcd.setPixel(i,j);
HarryPetrovic1 1:6632d8423c65 269 }
HarryPetrovic1 1:6632d8423c65 270
HarryPetrovic1 1:6632d8423c65 271 else {
HarryPetrovic1 1:6632d8423c65 272 lcd.clearPixel(i,j);
HarryPetrovic1 1:6632d8423c65 273 }
HarryPetrovic1 1:6632d8423c65 274
HarryPetrovic1 1:6632d8423c65 275
HarryPetrovic1 1:6632d8423c65 276
HarryPetrovic1 1:6632d8423c65 277 }
HarryPetrovic1 1:6632d8423c65 278
HarryPetrovic1 1:6632d8423c65 279 }
HarryPetrovic1 1:6632d8423c65 280
HarryPetrovic1 1:6632d8423c65 281 }
HarryPetrovic1 1:6632d8423c65 282
HarryPetrovic1 1:6632d8423c65 283
HarryPetrovic1 1:6632d8423c65 284
HarryPetrovic1 1:6632d8423c65 285 void moveUserPaddle() //used to control paddle
HarryPetrovic1 1:6632d8423c65 286
HarryPetrovic1 1:6632d8423c65 287 {
HarryPetrovic1 1:6632d8423c65 288 if(yin > 0.6f) { //if potentimeter is greater than 0.6 value the paddle moves up
HarryPetrovic1 1:6632d8423c65 289 yp= yp-3;
HarryPetrovic1 1:6632d8423c65 290 }
HarryPetrovic1 1:6632d8423c65 291
HarryPetrovic1 1:6632d8423c65 292 else if(yin < 0.4f) { //as above but the paddle will move down
HarryPetrovic1 1:6632d8423c65 293 yp = yp+3;
HarryPetrovic1 1:6632d8423c65 294 }
HarryPetrovic1 1:6632d8423c65 295 }
HarryPetrovic1 1:6632d8423c65 296
HarryPetrovic1 1:6632d8423c65 297 void moveComputerPaddle()
HarryPetrovic1 1:6632d8423c65 298
HarryPetrovic1 1:6632d8423c65 299 {
HarryPetrovic1 1:6632d8423c65 300 yc=pos.y-difficulty;
HarryPetrovic1 1:6632d8423c65 301 }
HarryPetrovic1 1:6632d8423c65 302
HarryPetrovic1 1:6632d8423c65 303
HarryPetrovic1 1:6632d8423c65 304 void paddleLimits() //makes it so that the paddles remain upon the screen
HarryPetrovic1 1:6632d8423c65 305
HarryPetrovic1 1:6632d8423c65 306 {
HarryPetrovic1 1:6632d8423c65 307 if (yp <= 0) { //stops paddle disapearing above the screen
HarryPetrovic1 1:6632d8423c65 308
HarryPetrovic1 1:6632d8423c65 309 yp=0;
HarryPetrovic1 1:6632d8423c65 310 }
HarryPetrovic1 1:6632d8423c65 311
HarryPetrovic1 1:6632d8423c65 312 else if (yp >= 38) { //stops paddle disapearing bellow the screen, value of 40 must be used because of 8 pixel long paddle
HarryPetrovic1 1:6632d8423c65 313 yp=40;
HarryPetrovic1 1:6632d8423c65 314 }
HarryPetrovic1 1:6632d8423c65 315 }
HarryPetrovic1 1:6632d8423c65 316
HarryPetrovic1 1:6632d8423c65 317 void ComputerpaddleLimits() //makes it so that the paddles remain upon the screen
HarryPetrovic1 1:6632d8423c65 318
HarryPetrovic1 1:6632d8423c65 319 {
HarryPetrovic1 1:6632d8423c65 320 if (yc <= 0) { //stops paddle disapearing above the screen
HarryPetrovic1 1:6632d8423c65 321
HarryPetrovic1 1:6632d8423c65 322 yc=0;
HarryPetrovic1 1:6632d8423c65 323 }
HarryPetrovic1 1:6632d8423c65 324
HarryPetrovic1 1:6632d8423c65 325 else if (yc >= 38) { //stops paddle disapearing bellow the screen, value of 40 must be used because of 8 pixel long paddle
HarryPetrovic1 1:6632d8423c65 326 yc=38;
HarryPetrovic1 1:6632d8423c65 327 }
HarryPetrovic1 1:6632d8423c65 328 }
HarryPetrovic1 1:6632d8423c65 329
HarryPetrovic1 1:6632d8423c65 330 void Scoring()
HarryPetrovic1 1:6632d8423c65 331
HarryPetrovic1 1:6632d8423c65 332 {
HarryPetrovic1 1:6632d8423c65 333
HarryPetrovic1 1:6632d8423c65 334 if (pos.x - BALLRADIUS < 0) {
HarryPetrovic1 1:6632d8423c65 335
HarryPetrovic1 1:6632d8423c65 336 computerScore = computerScore++;
HarryPetrovic1 1:6632d8423c65 337
HarryPetrovic1 1:6632d8423c65 338 } else if (pos.x + BALLRADIUS > 83) {
HarryPetrovic1 1:6632d8423c65 339
HarryPetrovic1 1:6632d8423c65 340 playerScore = playerScore++;
HarryPetrovic1 1:6632d8423c65 341 }
HarryPetrovic1 1:6632d8423c65 342
HarryPetrovic1 1:6632d8423c65 343
HarryPetrovic1 1:6632d8423c65 344 }
HarryPetrovic1 1:6632d8423c65 345
HarryPetrovic1 1:6632d8423c65 346 void resetBall()
HarryPetrovic1 1:6632d8423c65 347 {
HarryPetrovic1 1:6632d8423c65 348
HarryPetrovic1 1:6632d8423c65 349 if (pos.x - BALLRADIUS < 0) {
HarryPetrovic1 1:6632d8423c65 350
HarryPetrovic1 1:6632d8423c65 351 wait(0.5);
HarryPetrovic1 1:6632d8423c65 352 init_ball();
HarryPetrovic1 1:6632d8423c65 353 } else if (pos.x + BALLRADIUS > 83) {
HarryPetrovic1 1:6632d8423c65 354
HarryPetrovic1 1:6632d8423c65 355 wait (0.5);
HarryPetrovic1 1:6632d8423c65 356 player_Score_Position();
HarryPetrovic1 1:6632d8423c65 357 }
HarryPetrovic1 1:6632d8423c65 358
HarryPetrovic1 1:6632d8423c65 359
HarryPetrovic1 1:6632d8423c65 360 }
HarryPetrovic1 1:6632d8423c65 361
HarryPetrovic1 1:6632d8423c65 362 void victory()
HarryPetrovic1 1:6632d8423c65 363 {
HarryPetrovic1 1:6632d8423c65 364
HarryPetrovic1 1:6632d8423c65 365 if (playerScore >=10)
HarryPetrovic1 1:6632d8423c65 366
HarryPetrovic1 1:6632d8423c65 367 {
HarryPetrovic1 1:6632d8423c65 368 refreshCells();
HarryPetrovic1 1:6632d8423c65 369 lcd.init();
HarryPetrovic1 1:6632d8423c65 370 lcd.printString("Welldone,",15,0); //display intro & title to game
HarryPetrovic1 1:6632d8423c65 371 lcd.printString("You've Won!",10,1);
HarryPetrovic1 1:6632d8423c65 372 lcd.printString("Is your name",5,2);
HarryPetrovic1 1:6632d8423c65 373 lcd.printString("Roger Federer?",1,3);
HarryPetrovic1 1:6632d8423c65 374 wait (5);
HarryPetrovic1 1:6632d8423c65 375 lcd.refresh();
HarryPetrovic1 1:6632d8423c65 376
HarryPetrovic1 1:6632d8423c65 377 }
HarryPetrovic1 1:6632d8423c65 378
HarryPetrovic1 1:6632d8423c65 379 }
HarryPetrovic1 1:6632d8423c65 380
HarryPetrovic1 1:6632d8423c65 381 void loss()
HarryPetrovic1 1:6632d8423c65 382 {
HarryPetrovic1 1:6632d8423c65 383
HarryPetrovic1 1:6632d8423c65 384 if (computerScore >=10)
HarryPetrovic1 1:6632d8423c65 385
HarryPetrovic1 1:6632d8423c65 386 {
HarryPetrovic1 1:6632d8423c65 387 refreshCells();
HarryPetrovic1 1:6632d8423c65 388 lcd.init();
HarryPetrovic1 1:6632d8423c65 389 lcd.printString("Unlucky Pal",10,0); //display intro & title to game
HarryPetrovic1 1:6632d8423c65 390 lcd.printString("You Lose, you",5,1);
HarryPetrovic1 1:6632d8423c65 391 lcd.printString("absoloute mug",5,2);
HarryPetrovic1 1:6632d8423c65 392 wait (5);
HarryPetrovic1 1:6632d8423c65 393 lcd.refresh();
HarryPetrovic1 1:6632d8423c65 394
HarryPetrovic1 1:6632d8423c65 395 }
HarryPetrovic1 1:6632d8423c65 396
HarryPetrovic1 1:6632d8423c65 397
HarryPetrovic1 1:6632d8423c65 398
HarryPetrovic1 1:6632d8423c65 399
HarryPetrovic1 1:6632d8423c65 400 }
HarryPetrovic1 1:6632d8423c65 401
HarryPetrovic1 1:6632d8423c65 402 void redraw_screen()
HarryPetrovic1 1:6632d8423c65 403 {
HarryPetrovic1 1:6632d8423c65 404
HarryPetrovic1 1:6632d8423c65 405 lcd.drawCircle(pos.x,pos.y,BALLRADIUS,0.5);
HarryPetrovic1 1:6632d8423c65 406 lcd.refresh();
HarryPetrovic1 1:6632d8423c65 407 }
HarryPetrovic1 1:6632d8423c65 408
HarryPetrovic1 1:6632d8423c65 409
HarryPetrovic1 1:6632d8423c65 410 void check_collisions()
HarryPetrovic1 1:6632d8423c65 411 {
HarryPetrovic1 1:6632d8423c65 412 // see if ball has hit the floor (subtract the radius since the position is the centre of the ball)
HarryPetrovic1 1:6632d8423c65 413 if ( pos.y >= 47 - BALLRADIUS ) {
HarryPetrovic1 1:6632d8423c65 414 pos.y = 47 - BALLRADIUS; // need to force this or else ball can end up going 'underground'
HarryPetrovic1 1:6632d8423c65 415 vel.y = -0.6 * vel.y; // y velocity is reflected and dampened
HarryPetrovic1 1:6632d8423c65 416 acc.y = -acc.y;
HarryPetrovic1 1:6632d8423c65 417 // y accleration is still gravity
HarryPetrovic1 1:6632d8423c65 418 }
HarryPetrovic1 1:6632d8423c65 419
HarryPetrovic1 1:6632d8423c65 420 if ( pos.y <= 5 - BALLRADIUS ) {
HarryPetrovic1 1:6632d8423c65 421 pos.y = 5 - BALLRADIUS; // need to force this or else ball can end up going 'underground'
HarryPetrovic1 1:6632d8423c65 422 vel.y = -0.6 * vel.y; // y velocity is reflected and dampened
HarryPetrovic1 1:6632d8423c65 423 acc.y = -acc.y;
HarryPetrovic1 1:6632d8423c65 424 // y accleration is still gravity
HarryPetrovic1 1:6632d8423c65 425 }
HarryPetrovic1 1:6632d8423c65 426
HarryPetrovic1 1:6632d8423c65 427 // has ball gone off the right-hand side?
HarryPetrovic1 1:6632d8423c65 428 if (( pos.x >= 83 - BALLRADIUS ) && (pos.y > yc) && (pos.y < yc + 12)&&(pos.y > yc - 3)) {
HarryPetrovic1 1:6632d8423c65 429 pos.x = 83 - BALLRADIUS; // need to force this or else ball can end up going off screen
HarryPetrovic1 1:6632d8423c65 430 vel.x = -0.6 * vel.x; // reflect and damp velocity
HarryPetrovic1 1:6632d8423c65 431 acc.x = -acc.x; // reflect accleration
HarryPetrovic1 1:6632d8423c65 432 }
HarryPetrovic1 1:6632d8423c65 433
HarryPetrovic1 1:6632d8423c65 434 // what about the left?
HarryPetrovic1 1:6632d8423c65 435 if (( pos.x <= BALLRADIUS ) && (pos.y > yp) && (pos.y < yp + 12)&&(pos.y > yp - 3)) {
HarryPetrovic1 1:6632d8423c65 436 pos.x = BALLRADIUS; // need to force this or else ball can end up going off screen
HarryPetrovic1 1:6632d8423c65 437 vel.x = -0.6 * vel.x; // reflect and damp velocity
HarryPetrovic1 1:6632d8423c65 438 acc.x = -acc.x; // reflect accleration
HarryPetrovic1 1:6632d8423c65 439 }
HarryPetrovic1 1:6632d8423c65 440
HarryPetrovic1 1:6632d8423c65 441 }
HarryPetrovic1 1:6632d8423c65 442
HarryPetrovic1 1:6632d8423c65 443 void update_physics_engine()
HarryPetrovic1 1:6632d8423c65 444 {
HarryPetrovic1 1:6632d8423c65 445 // from Newton's Laws
HarryPetrovic1 1:6632d8423c65 446
HarryPetrovic1 1:6632d8423c65 447 acc.x = 1.0F*acc.x; // reduce a little due to air friction
HarryPetrovic1 1:6632d8423c65 448
HarryPetrovic1 1:6632d8423c65 449 // calc new velocity (assume 'unit' time)
HarryPetrovic1 1:6632d8423c65 450 vel.x = vel.x + acc.x; // * g_gt;
HarryPetrovic1 1:6632d8423c65 451 vel.y = vel.y + acc.y; // * g_gt;
HarryPetrovic1 1:6632d8423c65 452
HarryPetrovic1 1:6632d8423c65 453 // calc new position (assume 'unit' time)
HarryPetrovic1 1:6632d8423c65 454 pos.x = (pos.x + vel.x) * 1;
HarryPetrovic1 1:6632d8423c65 455 pos.y = (pos.y + vel.y) * 1;
HarryPetrovic1 1:6632d8423c65 456
HarryPetrovic1 1:6632d8423c65 457 // should really multiply the above by the time-step,
HarryPetrovic1 1:6632d8423c65 458 // but since the pixel can only be a integer value,
HarryPetrovic1 1:6632d8423c65 459 // it makes the motion a little 'jumpy'.
HarryPetrovic1 1:6632d8423c65 460
HarryPetrovic1 1:6632d8423c65 461 }
HarryPetrovic1 1:6632d8423c65 462
HarryPetrovic1 1:6632d8423c65 463 void init_ball()
HarryPetrovic1 1:6632d8423c65 464 {
HarryPetrovic1 1:6632d8423c65 465 pos.x = 3;
HarryPetrovic1 1:6632d8423c65 466 pos.y = 4;
HarryPetrovic1 1:6632d8423c65 467 vel.x = 0.0;
HarryPetrovic1 1:6632d8423c65 468 vel.y = 0.0;
HarryPetrovic1 1:6632d8423c65 469 acc.x = 0.5;
HarryPetrovic1 1:6632d8423c65 470 acc.y = 0.075;
HarryPetrovic1 1:6632d8423c65 471 }
HarryPetrovic1 1:6632d8423c65 472
HarryPetrovic1 1:6632d8423c65 473 void player_Score_Position()
HarryPetrovic1 1:6632d8423c65 474 {
HarryPetrovic1 1:6632d8423c65 475 // initial position (top-left)
HarryPetrovic1 1:6632d8423c65 476 pos.x = 80;
HarryPetrovic1 1:6632d8423c65 477 pos.y = 4;
HarryPetrovic1 1:6632d8423c65 478 // initial velocity - still
HarryPetrovic1 1:6632d8423c65 479 vel.x = 0.0;
HarryPetrovic1 1:6632d8423c65 480 vel.y = 0.0;
HarryPetrovic1 1:6632d8423c65 481 // initial acceleration - gravity and a bit of push to right
HarryPetrovic1 1:6632d8423c65 482 acc.x = -0.5;
HarryPetrovic1 1:6632d8423c65 483 acc.y = 0.075; // +ve so ball accelerates to bottom of display (top of screen is y=0, bottom is y=47)
HarryPetrovic1 1:6632d8423c65 484 // should be 9.8, but can play with value to get a 'nice' ball movement
HarryPetrovic1 1:6632d8423c65 485 }
HarryPetrovic1 1:6632d8423c65 486
HarryPetrovic1 1:6632d8423c65 487 void init_display()
HarryPetrovic1 1:6632d8423c65 488 {
HarryPetrovic1 1:6632d8423c65 489 lcd.init();
HarryPetrovic1 1:6632d8423c65 490 lcd.normalMode(); // normal colour mode
HarryPetrovic1 1:6632d8423c65 491 lcd.setBrightness(0.5); // put LED backlight on 50%
HarryPetrovic1 1:6632d8423c65 492 }