Starting from:
$35

$29

Assignment 2 Solution

Notes:

    • Name your sketch using your name, and the assignment number, exactly as in this example: LastnameFirstnameA2. You will submit only one program in this assignment.

    • Your program must run upon download to receive any marks.
    • Submit one pde file only. Do not submit any other types of files.

    • Assignments must follow the programming standards document published on the course website on UMLearn.

    • After the due date and time assignments may be submitted but will lose 2% of marks per hour late or portion thereof. The date and time of the last file submitted controls the mark for the entire assignment.

    • You may submit your program multiple times, but only the most recent version will be marked.

    • These assignments are your chance to learn the material for the exams. Code your assignments independently. We use software to compare all submitted assignments to each other, and pursue academic dishonestly vigorously.

Q1: Angular spaceship

You should be able to do this question after Week 4 in the course. It covers floating point, trigonometry, and functions.


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, and ɸ), as shown in Figure 2. Experiment to find suitable values for these constants. Give them better names.

    • 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 in the correct position and at the correct angle. Give your 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 four points. It takes 8 parameters.

    • Draw your ship against a dark background.




1
ASSIGNMENT 2
DEPARTMENT AND COURSE NUMBER: COMP 1010


Q2: Fly the spaceship

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

    • 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 Questions 3 and 4.

    • 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

In Question 4, a game will be created that requires the spaceship to be flown through narrow passages or channels from one “dimension” to another. Prepare for this by creating random dimensions, each with a passage or channel as 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 three separate values for red, green, and

2
ASSIGNMENT 2
DEPARTMENT AND COURSE NUMBER: COMP 1010


blue (all 0 to 255) into a single int value. You can ensure 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 diagram. 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.

Add additional code to draw your dimensions, and test the way that they are generated:

    • 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 Question 3, you could make the ship fly faster, so that you see the new dimensions more quickly. But make it slower again for Question 4.

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

Q4: Game On!

Now make a game of it. To successfully fly from one dimension to the next, the spaceship must fly through the passage or channel to the edge of the canvas, without any corner of the ship touching any rectangle. You can try the game by running the SampleSolutionA2Q4 application inside the A2SampleApplication folder posted on the website, as a zip file. (This is for 64-bit Windows only, with Java installed. Sorry, Mac users. Use a lab computer if you do not have a suitable computer of your own.)

    • 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 modification before continuing with the rest of Question 4.


3
ASSIGNMENT 2
DEPARTMENT AND COURSE NUMBER: COMP 1010


    • 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 6 separate checks to see whether or not a point is inside a rectangle (3 points times 2 rectangles). To enable you to do this more easily, a function ptInRect(x,y,top,left,wide,high) has been provided which will determine whether or not the point (x,y) is inside the rectangle 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 SuppliedCode.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.

Hand In:

Hand in one pde file only, containing the answer to the highest question you completed, which will contain all the code for the earlier questions. It must run without errors, or you will lose all of the marks for the test run.

Make sure your file is named exactly as specified in the instructions at the beginning of the assignment.

The marker will run your program, and may change the canvas size and the constants specified in the assignment. It should still work if any constants are changed in a reasonable way.


























4

More products