330 likes | 521 Views
Lab 10a – Pong. Write a C program to play a 2-player game of Pong, a two-dimensional sports game which simulates table tennis.
E N D
Lab 10a – Pong Write a C program to play a 2-player game of Pong, a two-dimensional sports game which simulates table tennis. Use the potentiometers to control the paddles moving vertically on each side of the screen. The paddles and walls reflect a ball back and forth at the correct speed and reflection angle. A point is scored when one side fails to return the ball to the other side. Show the score in real time on the LCD display. Divide the paddle into segments to change the ball's angle of return. For example, the center segments return the ball a 90° angle in relation to the paddle, while the outer segments return the ball at smaller angles. Make the ball accelerate the more it is returned back and forth between paddles; missing the ball resets the speed. Pong Lab
Learning Objectives • By completing the Pong Lab, a student will have • Implemented an event driven programming model. • Used the C library functions malloc and free to allocate and recover memory from the system heap. • Used scaled analog to digital converters (ADC) values in a time critical problem. • Used C struct's and pointers correctly in a C program. Pong Lab
Programming Paradigms • Imperative Programming • computation in terms of statements that change a program state • Functional Programming • computation as the evaluation of mathematical functions and avoids state and mutable data. • Procedural / Structured Programming • specifying the steps the program must take to reach the desired state • Object Oriented Programming (OOP) • uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. • Declarative Programming • expresses the logic of a computation without describing its control flow • Automata-based Programming • the program models a finite state machine or any other formal automata. • Event-driven Programming • the flow of the program is determined by events, i.e., sensor outputs, user actions (mouse clicks, key presses), messages from other programs or threads. Pong Lab
Event-driven Programming • Event-driven Programming • Simplifies program design/implementation • Programmed Systematic Decomposition • Event – something requiring action • sensor outputs (completion interrupts) • Internal generated events (timers) • user actions (mouse clicks, key presses) • messages from other programs or threads. • Event handler – unit of work • Main loop • constantly running main loop, or sleep w/interrupts (preferred) • Prioritized event selection. • Calls event handler. Pong Lab
Event Driven Programming EVENTS: Timers Switches Interrupts Programs while (1) { _disable_interrupts(); // disable to check sys_event if (sys_event) _enable_interrupt(); // enable if event pending else __bis_SR_register(LPM0_bits | GIE); // else sleep // I'm AWAKE!!! What needs service? if (sys_event & MOVE_BALL) // timer A event { sys_event&= ~MOVE_BALL; // clear TimerA event MOVE_BALL_event(ball); // update ball position } else if (sys_event & ADC_READ) // read ADC event { sys_event&= ~ADC_READ; // clear ADC event ADC_READ_event(rightPaddle); // process ADC read } else if (sys_event & NEW_GAME) // new game event { sys_event&= ~NEW_GAME; // clear new game event NEW_GAME_event(); // process new game } else ERROR2(SYS_ERR_EVENT); // unrecognized event } MOVE_BALL_event() { } ADC_READ_event() { } NEW_GAME_event() { } Pong Lab
Pong Events • What events might we consider? Event Action When? • SWITCH_1 New 2P Game P1IN (WD) • SWITCH_2 Game stats P1IN (WD) • SWITCH_4 New 1P Game P1IN (WD) • MOVE_BALL Ball movement TimerA • ADC_READ Read ADC 1/10 sec (WD) • LCD_UPDATE Current Score 1/2 sec (WD) • NEW_GAME Game Selection Start/Winner • START_GAME Setup Board Begin game • MISSED_BALL Raspberry Missed ball • END_GAME Report Winner • NEW_RALLY New ball Serve ball Pong Lab
Pong Event Handlers SWITCH_2 (Bonus) mode = STAT Displays stats enum MODE { IDLE, COUNT, PLAY, EOG, STAT }; enum GAME { PLAYER1, PLAYER2 }; TIMERA_ISR If (PLAY) MOVE_BALL WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 MOVE_BALL ADC_READ Update paddles TimerA ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep and increase ball speed TimerA SWITCH_4 SWITCH_1 LCD_UPDATE LCD_UPDATE If (PLAY) { Update scores Redraw center line } If (COUNT && count) { count--; Display (GO,1,2,3)[count] If (count == 0) { mode = IDLE START_GAME } SWITCH_4 (Bonus) • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME SWITCH_1 • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME NEW_RALLY new ball Start TimerA mode = PLAY MISSED_BALL MISSED_BALL Start NEW_GAME NEW_GAME NEW_RALLY START_GAME NEW_RALLY MISSED_BALL Raspberry. mode = IDLE Update scores. free ball Stop TimerA If (score < 5) NEW_RALLY else END_GAME NEW_GAME START_GAME Initialize all variables free balls/paddles (if any) Draw game new paddles NEW_RALLY END_GAME mode = EOG free paddles. Output final score and any appropriate reward NEW_GAME Splash Screen mode = IDLE END_GAME Pong Lab
0-159 0-159 y == 100 (y - 8) is a miss (y + 8) is a miss Pong struct’s PADDLE rightPaddle; rightPaddle.x= 158; rightPaddle.y= 100; drawPaddle(&rightPaddle); 1 3 lcd_rectangle(0, 158, 160, 2, 1); lcd_rectangle(79, 12, 2, 156, 1); typedefstruct { intx; inty; intold_x; intold_y; } BALL; typedefstruct { int x; inty; intold_y; } PADDLE; BALL ball; ball.x = 55; ball.y = 90; drawBall(&ball); PADDLE leftPaddle; leftPaddle.x= 1; leftPaddle.y= 20; drawPaddle(&leftPaddle); Make the ball accelerate each time is strikes a paddle. Missing the ball resets the speed. Pong Lab
Pong Paddle Divide the paddle into segments to change the ball's angle of return. dy = 2 or -2; dy = 1 or -1; 15 pixels dy = 0; dy = 1 or -1; Paddle’s x,y coordinates dy = 2 or -2; Pong Lab
Pong Behavior • Your Pong program begins by displaying some relevant Pong splash screen. Pressing Switch #1 starts the game. • After Switch #1 is pressed and before a ball is released, the characters "3", "2", "1", "GO" are displayed at one second intervals. • The left potentiometer moves the left paddle up (counter-clockwise) and down (clockwise), while the right paddle moves the right paddle up (clockwise) and down (counter-clockwise) in real time. • Your program detects when and where the ball strikes a paddle, at which time a tone is sounded and the ball changes direction. • The paddle is divide into 5 vertical sections, 3:4:1:4:3 = 15 bits. When the ball strikes the middle section (1 bit), the ball is reflected at a 90 degree angle (dy = 0). When the ball strikes an inter section (4 bits each), the ball reflects at a 45 degree angle of incidence (dy = 1 or -1). Finally, if the ball strikes an outer section (3 bits each), the ball is reflected at 22.5 degree angle of incidence (dy = 2 or -2). • The ball accelerates in speed as a rally progresses. A missed ball results in a raspberry sound and a new rally begins with the default speed. • A point is scored for the opposite side for each missed ball. Scores are displayed in the middle of the LCD screen during each rally. • The game ends when one side reaches 5 points. The winner is congratulated (some ditty/displayed message) and then your program prompts for a new game. Pong Lab
Pong Program Requirements • Your program is written in C and uses an event driven programming model to move the ball and paddles in real-time. • Your main program "sleeps" in low-power mode 0 (CPUOFF), waiting for asynchronous events. An event "wakes up" your program. After servicing the event, your program goes back to sleep waiting for the next event. (If there are any events pending, process them immediately - only go to sleep when there is nothing pending.) • Events are generated by Port 1 switches, TimerA, and Watchdog Timer as follows: • Service Switch #1 interrupt event by restarting a game. • Service TimerA event by moving the pong ball and checking for deflection off the walls, striking a paddle, or reaching the right or left of the screen. • Service one of the Watchdog Timer counter events by sampling the right and left potentiometers and moving the appropriate paddle. Remember, the range of your potentiometers is approximately 0 to 1023. The A/D values must be scaled (multiplied by 159/1023) such that the paddles can reach the top and bottom of the LCD (160 pixels). • Service another Watchdog Timer counter event to update the score and any erased lines on the LCD (1/2 second). • Add events as needed (new game event, start game event, end game event, missed ball event, rally event, etc.) Use event service routines to simplify your design. • Prioritize your event service routines by putting the most frequently serviced event routines first (Rate Monotomic Scheduling). • Make your interrupt and event service routines as simple and efficient as possible. Generally, the more cohesive events you define, the simpler your program becomes, since each event handler only deals with a small, independent task. DO NOT put any function calls or event service routines in your ISRs! Do all "work" in the event service routine and not in the ISR. Pong Lab
Step 1 – Validate Code • Create a "Project" for the MSP430F2274 using Code Composer Studio. • File -> New -> CCS Project • Enter your Project name. • Select MSP430F2274 Device Variant. • Under Advanced settings, select "legacy COFF" for Output format and "<automatic>" for both Linker command file and Runtime support library. • Under Project templates and examples, select "Empty Project". • Click "Finish". • If CCS creates a main.c file, delete it from your project. • Download and add to your project pong.c, pongEvents.c, and pong.h. These are the only files you will modify to complete the required parts of this lab. Also download and add to your project pongLib.c, pongImages.c, pongInterrupts.c, RBX430-1.c, RBX430-1.h, RBX430_lcd.c, and RBX430_lcd.h. • Build your project (Project->Build Project), download to your development board, and Run. Verify that you have a "working" program - - the ball moves back and forth across the display and the right paddle moves up and down as the potentiometer is turned. • If the LCD are too dim, uncomment the function call lcd_volume(376);. Pong Lab
Step 2 – Add Start Events • Modify the pong.c header comments with your name and a declaration that the completed assignment is your own work. • Add SWITCH_1 and START_GAME events to your event service loop in pong.c. Likewise add SWITCH_1 and START_GAME event handlers to pongEvents.c source code. (We now recognize 5 events: MOVE_BALL, ADC_READ, NEW_GAME, SWITCH_1, and START_GAME.) • Signal the START_GAME event from your SWITCH_1_event() handler. • Move start game and start rally code from the NEW_GAME event handler to the START_GAME event handler. Add code to count down for 4 seconds while displaying the characters "3", "2", "1", "GO“ before starting the rally. • Delete the switch while and sys_event statements from the NEW_GAME event handler. • Signal START_GAME from NEW_GAME event handler. Pressing switch #1 now should display the count down and then release a ball. Pong Lab
Step 3 – EDP • Your C program uses an event driven programming model to move the ball and paddles in real-time. Pong Lab
Step 4 – Event Handlers • Proceed with your implementation of pong event handlers. Pong Lab
Step 5 - Hints • Here are some hints for implementing various event handlers: a. Modify factory functions new_ball and new_paddle to malloc ball and paddle structures from the system heap. Add a second paddle to the START_GAME event handler. b. Add the movement of the second paddle to the ADC_READ event handler. Scale the ADC values from 0-1023 to 0-159 and then limit the values to 7-152 (paddle height is 15 bits). c. Add code to the MOVE_BALL event handler to check for a ball striking a paddle and reflecting the ball accordingly. Increase the ball speed on each hit. Signal MISSED_BALL when the ball moves past a paddle without touching it. d. The MISSED_BALL event handler should output a raspberry sound and update the final scores (could be handled by LCD_UPDATE.) Free the ball, change the mode to IDLE and signal NEW_RALLY if both scores are less that 5, else signal END_GAME. e. Program the LCD_UPDATE event. Output current scores as well as redraw the center line. f. Finish with the END_GAME event handler freeing the paddles, outputting some reward for the rally winner, and signaling NEW_GAME event. Pong Lab
Pong Requirements 1 point Your Pong program displays a splash screen and waits for Switch #1 to be pressed to start the game. Before a ball is released, your program counts down for 4 seconds while displaying the characters "3", "2", "1", "GO". 2 points Your program is an event driven programming model - the event service loop "sleeps" in low-power mode 0 (CPUOFF) while waiting for asynchronous events. An event "wakes up" your program and the associated event service routine is called. The event loop goes back to sleep awaiting the next event. 1 point Malloc is used to instantiate the ball and paddles from heap memory at the beginning of the game and free'd at the end of the game. (No memory leaks!) 2 points The right paddle moves the right paddle up (clockwise) and down (counter-clockwise). The left potentiometer moves the left paddle up (counter-clockwise) and down (clockwise) in real time. As the ball strikes a paddle, a beep tone is sounded. 2 points The paddle is divided into 5 vertical sections, 3:4:1:4:3 = 15 bits. When the ball strikes the middle section of the paddle (1 bit), the ball is reflected at a 90 degree angle (dy = 0). When the ball strikes an inner section of the paddle (4 bits each), the ball is reflected at a 45 degree angle of incidence (dy = 1 or -1). Finally, if the ball strikes an outer section of the paddle (3 bits), then the ball is reflected at a 22.5 degree angle of incidence (dy = 2 or -2). 1 point The ball accelerates in speed as a rally progresses. A missed ball results in a raspberry sound and a new rally begins with the default speed. 1 point Each missed ball by a paddle scores a point for the opposite side. The scores are displayed in the middle of the LCD screen after each rally. The game ends when a player reaches 5 points. After the winner is congratulated, the game starts again. Pong Lab
Pong Requirements In addition, the following bonus/deductions apply: +1 point Passed off with a TA at least one day early. (No timestamps please!) +1 point Union and function pointers are correctly used in your game implementation. +2 points Your program implements a 1 or 2 player game, selectable at the beginning of the game. When a single player game is selected, a vertical wall (line) is used for the right paddle. The right potentiometer moves the wall left or right (thus increasing the difficulty of the game). Each time the ball hits the wall, a score is incremented. The game ends after 5 missed balls. The number of missed balls and the score (accumulated wall hits) is displayed after each rally. +1 point Pressing Switch #2 clears the screen and displays Pong statistics including longest rally, number of aces, consecutive points, etc. +1 point Pressing Switch #3 pauses your game (with message). Pressing Switch #3 again resumes the game. -1 point For each school day late. (Timestamps may be used to verify completion time.) Pong Lab
Pong Event Handlers enum MODE { IDLE, COUNT, PLAY, STAT }; enum GAME { PLAYER1, PLAYER2 }; SWITCH_2 (Bonus) mode = STAT Displays stats WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ TIMERA_ISR If (mode == PLAY) MOVE_BALL SWITCH_2 MOVE_BALL TimerA ADC_READ Update paddles SWITCH_4 SWITCH_1 ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. SWITCH_1 • If (PLAY) free ball/paddles • game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) • If (PLAY) free ball/paddles • game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME TimerA LCD_UPDATE LCD_UPDATE If (PLAY) Update scores and redraw center line. If (COUNT) Display count and if (count- - == 0) NEW_RALLY NEW_RALLY NEW_RALLY new ball Start TimerA mode = PLAY Start START_GAME NEW_GAME NEW_GAME MISSED_BALL MISSED_BALL START_GAME START_GAME Initialize all variables Draw(game) new paddles count = 3 mode = COUNT NEW_GAME Splash Screen mode = IDLE NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) Stop TimerA If (score < 5) NEW_RALLY else END_GAME END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME END_GAME NEW_GAME Pong Lab
Pong Event Handlers enum MODE { IDLE, PLAY, STAT }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 (Bonus) mode = STAT Displays stats SWITCH_2 TIMERA_ISR If (mode == PLAY) MOVE_BALL ADC_READ TimerA MOVE_BALL SWITCH_4 SWITCH_1 ADC_READ Update paddles LCD_UPDATE MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. SWITCH_1 • If (PLAY) free ball/paddles • game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) • If (PLAY) free ball/paddles • game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME TimerA LCD_UPDATE If (PLAY) Update scores and redraw center line. NEW_RALLY new ball Start TimerA mode = PLAY START_GAME Start NEW_GAME MISSED_BALL NEW_GAME START_GAME LCD_UPDATE START_GAME Initialize all variables Draw(Game) new paddles Count down NEW_RALLY NEW_GAME Splash Screen mode = IDLE MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME NEW_RALLY END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME NEW_GAME MISSED_BALL Pong Lab
Pong Event Handlers enum MODE { IDLE, COUNT, PLAY, STAT }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ TIMERA_ISR If (mode == PLAY) MOVE_BALL SWITCH_2 (Bonus) mode = STAT Displays stats SWITCH_2 MOVE_BALL TimerA SWITCH_4 SWITCH_1 ADC_READ Update paddles MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. ADC_READ SWITCH_1 • If (PLAY) free ball/paddles • game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) • If (PLAY) free ball/paddles • game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME TimerA LCD_UPDATE LCD_UPDATE If (PLAY) Update scores and redraw center line. If (COUNT) Display count and if (count-- == 0) NEW_RALLY NEW_RALLY NEW_RALLY new ball Start TimerA mode = PLAY START_GAME Start NEW_GAME NEW_GAME MISSED_BALL MISSED_BALL START_GAME START_GAME Initialize all variables Draw(game) new paddles count = 3 mode = COUNT NEW_GAME Splash Screen mode = IDLE NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME END_GAME NEW_GAME Pong Lab
160 (y - 8) is a miss 160 y == 100 Pong struct’s PADDLE rightPaddle; rightPaddle.x= 157; rightPaddle.y= 100; drawPaddle(&rightPaddle); 1 3 typedefstruct { intx; inty; intold_x; intold_y; } BALL; typedefstruct { intx; inty; intold_y; } PADDLE; BALL ball; ball.x = 55; ball.y = 90; drawBall(&ball); PADDLE leftPaddle; leftPaddle.x= 0; leftPaddle.y= 20; drawPaddle(&leftPaddle); Make the ball accelerate each time is strikes a paddle. Missing the ball resets the speed. Pong Lab
Pong Event Handlers enum MODE { IDLE, PLAY }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 mode = IDLE Displays stats NEW_GAME TIMERA_ISR If (mode == PLAY) MOVE_BALL MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. ADC_READ Update paddles SWITCH_1 • game = 2PLAYER • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) game = 1PLAYER • If (IDLE) START_GAME • else NEW_GAME LCD_UPDATE If (PLAY) Update scores and redraw center line. NEW_RALLY new ball Start TimerA mode = PLAY NEW_GAME Splash Screen mode = IDLE START_GAME Initialize all variables Draw(Game) new paddles Count down NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME Pong Lab
Pong Event Handlers enum MODE { IDLE, PLAY, STAT }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 (Bonus) mode = STAT Displays stats SWITCH_2 TIMERA_ISR If (mode == PLAY) MOVE_BALL ADC_READ MOVE_BALL SWITCH_4 SWITCH_1 ADC_READ Update paddles LCD_UPDATE MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. SWITCH_1 • game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME LCD_UPDATE If (PLAY) Update scores and redraw center line. Start NEW_RALLY new ball Start TimerA mode = PLAY START_GAME NEW_GAME START_GAME MISSED_BALL NEW_GAME LCD_UPDATE NEW_GAME Splash Screen mode = IDLE START_GAME Initialize all variables Draw(Game) new paddles Count down NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME NEW_RALLY END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME NEW_GAME MISSED_BALL Pong Lab
Pong Event Handlers enum MODE { IDLE, PLAY, STAT }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 (Bonus) mode = STAT Displays stats SWITCH_2 TIMERA_ISR If (mode == PLAY) MOVE_BALL ADC_READ TimerA MOVE_BALL SWITCH_4 SWITCH_1 ADC_READ Update paddles LCD_UPDATE MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. SWITCH_1 • game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME TimerA LCD_UPDATE If (PLAY) Update scores and redraw center line. Start NEW_RALLY new ball Start TimerA mode = PLAY NEW_GAME START_GAME START_GAME NEW_GAME MISSED_BALL LCD_UPDATE NEW_GAME Splash Screen mode = IDLE START_GAME Initialize all variables Draw(Game) new paddles Count down NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME NEW_RALLY END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME NEW_GAME MISSED_BALL Pong Lab
Pong Event Handlers enum MODE { IDLE, PLAY }; enum GAME { PLAYER1, PLAYER2 }; WDT_ISR If (switch) SWITCH_X If (1 second && PLAY) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 mode = IDLE Displays stats NEW_GAME SWITCH_2 TIMERA_ISR If (mode == PLAY) MOVE_BALL ADC_READ SWITCH_1 SWITCH_4 MOVE_BALL ADC_READ Update paddles LCD_UPDATE MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep increase ball speed. SWITCH_1 • game = PLAYER1 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) game = PLAYER2 • If (IDLE) START_GAME • else NEW_GAME LCD_UPDATE If (PLAY) Update scores and redraw center line. NEW_GAME Start NEW_RALLY new ball Start TimerA mode = PLAY NEW_GAME START_GAME MISSED_BALL START_GAME NEW_GAME Splash Screen mode = IDLE LCD_UPDATE START_GAME Initialize all variables Draw(Game) new paddles Count down NEW_RALLY MISSED_BALL Raspberry. Update scores. free (ball) mode = IDLE If (score < 5) NEW_RALLY else END_GAME NEW_RALLY END_GAME mode = IDLE free paddles. Output final score and any appropriate reward NEW_GAME NEW_GAME MISSED_BALL Pong Lab
Pong Paddle Divide the paddle into segments to change the ball's angle of return. dy = 2 or -2; dy = 1 or -1; dy = 0; 15 pixels dy = 1 or -1; Paddles y coordinate dy = 2 or -2; Pong Lab
Pong Event Handlers enum MODE { IDLE, COUNT, PLAY, EOG, STAT }; enum GAME { PLAYER1, PLAYER2 }; SWITCH_2 (Bonus) mode = STAT Displays stats WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ TIMERA_ISR If (mode == PLAY) MOVE_BALL SWITCH_2 MOVE_BALL TimerA ADC_READ Update paddles SWITCH_4 SWITCH_1 ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep and increase ball speed. TimerA SWITCH_1 • If (PLAY) free ball/paddles • and game = PLAYER2 • If (IDLE) START_GAME else NEW_GAME SWITCH_4 (Bonus) • If (PLAY) free ball/paddles • and game = PLAYER1 • If (IDLE) START_GAME • else NEW_GAME LCD_UPDATE LCD_UPDATE If (PLAY) Update scores and redraw center line. If (COUNT) Display count and if (count- - == 0) clear LCD and NEW_RALLY NEW_RALLY NEW_RALLY new ball Start TimerA mode = PLAY Start START_GAME NEW_GAME MISSED_BALL MISSED_BALL NEW_GAME START_GAME START_GAME Initialize all variables Draw game new paddles count = 4 mode = COUNT NEW_GAME Splash Screen mode = IDLE NEW_RALLY MISSED_BALL Raspberry. mode = IDLE Update scores. free ball Stop TimerA If (score < 5) NEW_RALLY else END_GAME END_GAME mode = EOG free paddles. Output final score and any appropriate reward END_GAME Pong Lab
Pong Event Handlers SWITCH_2 (Bonus) mode = STAT Displays stats enum MODE { IDLE, COUNT, PLAY, EOG, STAT }; enum GAME { PLAYER1, PLAYER2 }; TIMERA_ISR If (PLAY) MOVE_BALL WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 MOVE_BALL ADC_READ Update paddles TimerA ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep and increase ball speed TimerA SWITCH_4 SWITCH_1 LCD_UPDATE SWITCH_4 (Bonus) • If (IDLE) game = PLAYER2 • count = 4, mode = COUNT • else NEW_GAME SWITCH_1 • If (IDLE) game = PLAYER2 • count = 4, mode = COUNT • else NEW_GAME LCD_UPDATE If (PLAY) Update scores and redraw center line. If (COUNT && count) { Display (GO,1,2,3)[count] If (count- -) START_GAME } NEW_RALLY new ball Start TimerA mode = PLAY MISSED_BALL MISSED_BALL Start NEW_GAME NEW_GAME START_GAME NEW_RALLY NEW_GAME NEW_RALLY START_GAME Initialize all variables Draw game new paddles NEW_RALLY MISSED_BALL Raspberry. mode = IDLE Update scores. free ball Stop TimerA If (score < 5) NEW_RALLY else END_GAME END_GAME mode = EOG free paddles. Output final score and any appropriate reward NEW_GAME Splash Screen • free ball/paddles mode = IDLE END_GAME Pong Lab
Pong Event Handlers SWITCH_2 (Bonus) mode = STAT Displays stats enum MODE { IDLE, COUNT, PLAY, EOG, STAT }; enum GAME { PLAYER1, PLAYER2 }; TIMERA_ISR If (PLAY) MOVE_BALL WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 MOVE_BALL ADC_READ Update paddles TimerA ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep and increase ball speed TimerA SWITCH_4 SWITCH_1 LCD_UPDATE SWITCH_4 (Bonus) • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME SWITCH_1 • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME LCD_UPDATE If (PLAY) { Update scores Redraw center line } If (COUNT && count) { Display (GO,1,2,3)[count] If (count- -) START_GAME } NEW_RALLY new ball Start TimerA mode = PLAY MISSED_BALL MISSED_BALL Start NEW_GAME NEW_GAME NEW_RALLY START_GAME NEW_GAME NEW_RALLY START_GAME Initialize all variables Draw game new paddles NEW_RALLY MISSED_BALL Raspberry. mode = IDLE Update scores. free ball Stop TimerA If (score < 5) NEW_RALLY else END_GAME END_GAME mode = EOG free paddles. Output final score and any appropriate reward NEW_GAME Splash Screen • free ball/paddles mode = IDLE END_GAME Pong Lab
Pong Event Handlers SWITCH_2 (Bonus) mode = STAT Displays stats enum MODE { IDLE, COUNT, PLAY, EOG, STAT }; enum GAME { PLAYER1, PLAYER2 }; TIMERA_ISR If (PLAY) MOVE_BALL WDT_ISR If (switch) SWITCH_X If (1 second && (PLAY || COUNT)) LCD_UPDATE If (1/5 second && PLAY) ADC_READ SWITCH_2 MOVE_BALL ADC_READ Update paddles TimerA ADC_READ MOVE_BALL Move and draw ball. If (missed) MISSED_BALL else if (paddle) Beep and increase ball speed TimerA SWITCH_4 SWITCH_1 LCD_UPDATE LCD_UPDATE If (PLAY) { Update scores Redraw center line } If (COUNT && count) { count--; Display (GO,1,2,3)[count] If (count == 0) { mode = IDLE START_GAME } SWITCH_4 (Bonus) • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME SWITCH_1 • If (IDLE) { • game = PLAYER2 • count = 4 • mode = COUNT • } else NEW_GAME NEW_RALLY new ball Start TimerA mode = PLAY MISSED_BALL MISSED_BALL Start NEW_GAME NEW_GAME NEW_RALLY START_GAME NEW_RALLY MISSED_BALL Raspberry. mode = IDLE Update scores. free ball Stop TimerA If (score < 5) NEW_RALLY else END_GAME NEW_GAME START_GAME Initialize all variables Draw game new paddles NEW_RALLY END_GAME mode = EOG free paddles. Output final score and any appropriate reward NEW_GAME Splash Screen • free ball/paddles mode = IDLE END_GAME Pong Lab