QuestionQuestion

Transcribed TextTranscribed Text

Introduction to Programming Pass Task 8.2: Circle Moving in C Page 2 of 6 Instructions Here is a list of C functions and procedures available in C to help you with this program. Function / Procedure Does void open_graphics_window(title, width, height); Opens a new window, with indicated title and size void load_default_colors ( ); Loads the default colours such as COLOR_RED. void clear_screen ( ); Clears the screen to a color, e.g. COLOR_WHITE void delay ( time ); Delays for a number of milliseconds void process_events ( ); Listens for user input - required for any input int screen_width ( ); int screen_height ( ); The width and height of the window float mouse_x( ); float mouse_y( ); X or Y location of the mouse bool mouse_clicked(button) Was the mouse button clicked? Buttons are LEFT_BUTTON and RIGHT_BUTTON bool window_close_requested(); Has the user asked to close the Window? bool key_down(key); Is the key currently held down? Keys are in the format A_KEY, NUM_1_KEY, LEFT_KEY etc bool key_typed(key); Was the key typed? (Pressed then released) Introduction to Programming Pass Task 8.2: Circle Moving in C Page 3 of 6 1. Download the starter code for this task. 2. Extract the zip file to your code directory (e.g. Documents/Code) 3. Rename the Project Template folder to CharacterMoving2 4. Open a Terminal window and navigate to your CharacterMoving2 directory. 5. Write the code to implement a basic SwinGame program using the followingcode. 6. Switch back to the Terminal and compile and run your program. 7. As with Pascal, the window closes after 5 seconds when the program's instructionsend. Implement an event loop to allow you to control the life of the program. ■ The event loop will be located in Main, and will loop until the user closes the window. ■ Process Events needs to be called once each event loop to update SwinGame with the actions that have occurred since the last time through the loop. 8. Switch back to the Terminal and compile and run the program. It should now remain open until you close the window. 9. Create a constant named CIRCLE_RADIUS and set it to 90. Note: This contains the C/C++ version of SwinGame. --------------------------------- Procedure: Main --------------------------------- Steps: 1: Open a Graphics Window with title 'Circle Moving' that is 800x600 2: Do 3: Process Events 4: While Window Close is not Requested ( _______ _.,,,) 1 # <stdio.h> 2 # "SwinGame.h" 3 4 int main () 5 { 6 open_g ra phi cs_window ( "Circ le Movin g 2 - C/C++", 800, 600); 7 8 clear_screen (ColorWhite); 9 refresh_screen (60); 10 11 delay (5000); 12 13 0; 14 } Introduction to Programming Pass Task 8.2: Circle Moving in C Page 4 of 6 10. Alter Main to draw a circle on the screen, using variables for the circle's x and y location. 11. Switch to the Terminal, compile and run the program. You should be able to see a green circle in the centre of the screen. ■ The x and y variables in Main store all of the data for this game: the location of thecircle. As these are the only variables in the "game" (which is being run by the steps in the Main procedure), these are the only things that can change. --------------------------------- Procedure: Main --------------------------------- Local Variables: - x, y: Single data for the circle's location Steps: 1: Open a Graphics Window with title 'Circle Moving' that is 800x600 2: Assign x the value 400 3: Assign y the value 300 4: Do 5: Process Events 6: Clear the Screen to COLOR_WHITE 7: Fill a Circle using COLOR_GREEN, at location x,y with a radius CIRCLE_RADIUS 8: Refresh the Screen limiting it to 60 FPS 9: While Window Close is not Requested Note: To ensure a consistent game speed you can use RefreshScreen( 60 ) to limit the refresh rate to 60 frames per second (FPS) WARNING: C/C++ is case sensitive! So this means you really need to following the case recommendations. So make sure you use int main( ) in C for example. ( _______ ] ( J Introduction to Programming Pass Task 8.2: Circle Moving in C Page 5 of 6 The next step will involve using if statements to selectively run sections of your code. 12. Alter Main to update the x variable when the user is holding down the arrow keys. The following pseudocode shows the changes for moving left and right. Include the code tomove in all four directions. --------------------------------- Procedure: Main --------------------------------- Local Variables: - x, y: Single data for the circle's location Steps: 1: Open a Graphics Window with title 'Circle Moving' that is 800x600 2: Assign x the value 400 3: Assign y the value 300 4: Repeat 5: 6: 7: 8: 9: 10: 11: 12: Process Events if the LEFT_KEY Key is Down then Assign x, the value x - 1 if the RIGHT_KEY Key is Down then Assign x, the value x + 1 Clear the Screen to ColorWhite Fill a Circle using ColorGreen, at location x,y with a radius 90 Refresh the Screen limiting it to 60 FPS 13: Until Window Close is Requested 13. Switch back to the Terminal and compile and run the program. You should be able to move the circle using the left and right arrow keys. If you keep your finger on the one arrow key long enough, the circle will disappear off the edge of thescreen. Tip: C/C++ has operators to add/subtract o (_ ______ ne from a variable; use ++ and --. ) Introduction to Programming Pass Task 8.2: Circle Moving in C Page 6 of 6 14. Now, adjust the if statements so that the program will ensure that the circle remains on the screen. 15. Switch to the Terminal. Compile and run the program, and test that you cannot move the circle off the screen to the left or right. Compare your final program to your Pascal program… Notice how similar they are. If you can see that they are basically the same you are well on the way with your understanding of programming. Upload your completed code and a screen shot to Doubtfire. End Task Tip: To find the width of the current screen you can call the SwinGame function screen_width(). Similarly, C/C++ has a screen_height() function to give you the height of the screen.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdbool.h>
#include "SwinGame.h"

#define CIRCLE_RADIUS 90

int main()
{
    float x = 400;
    float y = 300;
    open_graphics_window("Circle Moving", 800, 600);

    do
    {...

By purchasing this solution you'll be able to access the following files:
Solution.c.

$33.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats