(5/5)

# Write an Active Processing program which will draw the simple angular “spaceship” shown at right (it’s like the one in the classic video game “Asteroids”).

INSTRUCTIONS TO CANDIDATES

Q1: ANGULAR SPACESHIP [6 MARKS]

Write an Active Processing program which will draw the simple angular “spaceship” shown at right (it’s like the one in the classic video game “Asteroids”).

You must have three variables that control the position (x,y) and angle (θ) of the spaceship, as shown in Figure 1. Give your variables better names than x, y, or theta.

Each time your program is run, it should pick random values for these three variables. While testing your program, you should also try it with some specific positions and angles.

You must have three constants that control the size and shape of the spaceship (d1, d2, ɸ), as shown in Figure 2. Experiment to find suitable values for these constants. Give them better names than d1, d2, or phi.

Define a function void calcPoints() which will use the six values described above (three variables and three constants) to calculate the coordinates of the other three points needed to draw the ship (the nose, and the ends of the two fins). Store the x and y coordinates of these three points in six suitably-named variables.

Define a function void drawShip() which will draw the spaceship any solid light colour that will show up well against a dark background. You can

use the built-in quad() command which will connect any foud points. It takes eight parameters.

Draw your ship against a dark background.

Q2: FLY THE SPACESHIP [8 MARKS]

Now add code that will allow you to fly the ship around the canvas, controlled by the keyboard.

The ship should fly at a constant speed. It can never speed up, slow down, or stop. Define a constant to control this speed.

When the user presses the D or L key on the keyboard, the ship should turn slowly to the right. When the user presses the A or J key on the keyboard, the ship should turn slowly to the left. The spaceship will always fly in the direction that it is pointing. When the user presses the S or K key on the keyboard, the ship should stop turning, and fly in a straight line. The user does not have to hold the key down. So, for example, after pressing D or L, the spaceship should slowly fly in a perfect clockwise circle, until some other key is pressed.

Define a state variable to control the rotation of the spaceship.

Define a suitable constant to define how fast the spaceship turns. Experiment to find a suitable value. The spaceship should fly in circles almost as big as the canvas (you may want to have a very large canvas, such as 1000x1000).

The spaceship should start in the centre of the canvas and should not be turning.

Define a function void moveShip() which will control the position and angle of the spaceship.

The spaceship should “wrap around” from any edge of the canvas to the opposite edge. For example, if it flies off the top of the canvas, it should reappear at the bottom. Do not use % to do this. Instead, use four separate IF statements to check for the four edges individually. This will be important in Q3 and Q4.

Define the special void keyPressed() function which will be called automatically whenever the user presses a key on the keyboard. You do not call this function yourself! Control the rotation of the spaceship if a, s, d, j, k or l is pressed. Ignore all other keys. You do not want the user to have to hold down the shift key, so check for lowercase letters (a) and not uppercase letters (A). the built-in variable key will tell you which key was pressed, and you can check its value like this: key==’a’. These are char variables and constants which will be covered more fully in Week 6.

Note: If your program does not respond to key presses, click once anywhere in the canvas. Only the “active” window will receive key presses, and sometimes, the canvas is not the active window until you click on it.

Q3: CREATE DIMENSIONS [6 MARKS]

In Q4, a game will be created that requires the spaceship to be

flown through narrow passages or “gates” from one “dimension” to another. Prepare for this be creating random dimensions, each with a passage or gate shown on the right.

Define a function void newDimension() which will create a new random dimension, storing all of the relevant data in a set of state variables (about 10 of them).

The dimension should have a random dark background colour. You can store a colour in a single int variable, using the built-in function color(r,g,b) to convert the

three separate values for red, green, and blue (all 0 to 255) into a single int value. You can endure that the colour is dark by generating random values that never get too large.

The passage to the next dimension should be a simple pair of white rectangles, close together, drawn on an edge of the canvas, as shown in the figure. Use suitable constants to define the shape of the rectangles, and how far apart they are. The spaceship should be able to fly between them, without

touching them, but it shouldn’t be too easy to do.

In the first dimension, when the program starts, the passage should be on either the top or bottom edge of the canvas (at random). In the next dimension, it should be on either the left or right edge (at random). The next time, it should again be on the top or bottom, and this alternation should repeat. You must use two boolean variables to control which edge the passage is on. One should control whether it is on the top/bottom or left/right, and this one should change every time newDimension() is called. The other one should control whether it’s on the top or left, or on the bottom or right. This one should be random.

Choose a random position for the passage, but not too close to any corner. The distance from the passage to any corner should never be smaller than the length of the passage (the long side of the rectangles). This is so that the spaceship will never fly through a passage straight into a wall in the next dimension. That wouldn’t be a very fair game.

Calculate and store all the information that you need to draw the rectangles once, when the dimension is created, not every frame when you draw them. Use an appropriate set of state variables.

Split the work into smaller functions to prevent newDimension() from getting too big.

Write a void drawPassage() function which will draw the two rectangles forming the passage

between dimensions. It should be very short because you’ve done all the calculations in advance… right?

Modify moveShip() so that it switches to a new dimension every time the ship flies off any edge of the screen. Make sure the passages are drawn correctly, and are generated correctly. Just while you’re testing Q3, you could make the ship fly faster, so that you see the new dimensions more quickly. But makes it slower again for Q4.

Make appropriate modifications to setup() and draw().

Q4: GAME ON! [6 MARKS]

Now make a game of it. To successfully fly from one dimension to the next, the spaceship must fly through the passage or gate to the edge of the canvas, without any corner of the ship touching any rectangle. You can see how the game should be run by playing the video posted with the assignment instructions.

Modify your moveShip() function so that the ship moves to a new dimension only if it leaves the canvas by flying through the passage. The ship must have flown off the correct edge of the canvas, and with an x or y coordinate that shows that the ship is inside the passage, and not on either side of it. Test your modifications before continuing with the rest of Q4.

Write a void checkCollision() function which will check to see if any of the three points of the ship (the nose or either of the two fins) is inside either of the two rectangles that form the passage. If so, it should set a boolean variable and terminate the game, displaying a “Game Over” message (see below). This will require doing six separate checks to see whether or not a point is inside a rectangle (3 points × 2 rectangles). To enable you to do this more easily, a function ptInRect(x, y, left, top, wide, high) has been provided which will determine whether or not the point (x,y) is defined as usual by the other four parameters. It returns a boolean result. Now aren’t you glad you pre-calculated and stored all the coordinates of the points of the ship and the rectangles?

A void showGameOverMessage() function has also been provided which will draw a suitable message in the centre of the canvas when the game is over. This uses material on text and Strings which will be covered in Week 6. You may modify the size or colour of the text if you wish.

Both of the provided functions are contained in the file A2SuppliedCode.pde. Cut and paste this code into your own file, at the bottom.

Use a boolean gameOver variable to control the end of the game. When this variable is set to true, then nothing should happen. Don’t move the ship, draw anything, or change anything. The game should be frozen.

(5/5)

## Related Questions

##### . Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

##### . The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

##### . Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

##### . SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

##### . Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Hire Me