Create the circle moving program using the new language and


Pass Task 8.2: Circle Moving in C

Overview

In Topic 3 you developed a Circle Moving Program using Pascal. To check your reference sheet, and to see how similar Pascal and C programs really are, you will now implement an- other version of this program using C.

Purpose: Start to learn a new language.

Task: Create the circle moving program using the new language, and your reference sheet.

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)

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 following code.

1524_code.jpg

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 instructions end. 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.

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 instructions end. 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 100.

10. Alter Main to draw a circle on the screen, using variables for the circle's x and y location.

---------------------------------
Procedure: Main
---------------------------------
Local Variables:
- x, y: Single data for the circle's location Steps:
1: Open a Graphics Window with title 'Character 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

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 the cir- cle. 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.

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 fol- lowing pseudocode shows the changes for moving left and right. Include the code to move 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 'Character Moving' that is 800x600
2: Assign x the value 400
3: Assign y the value 300
4: Repeat
5: Process Events

6: if the LEFT_KEY Key is Down then
7: Assign x, the value x - 1
8: if the RIGHT_KEY Key is Down then
9: Assign x, the value x + 1

10: Clear the Screen to ColorWhite
11: Fill a Circle using ColorGreen, at location x,y with a radius 100
12: 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 the screen.

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 pro- gramming.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Create the circle moving program using the new language and
Reference No:- TGS01401994

Expected delivery within 24 Hours