ASSIGNMENT 2: Floats, Trig, Booleans & Conditionals (Units 1-7 & 9-11) DEPARTMENT AND COURSE NUMBER: COMP1010 COURSE TITLE: Introduction to Computer Science 1 TERM: Winter 2020 Assignment 2 Dut DATE: FEBRUARY 14, 2020 BY 4:30 PM Notes: Name your sketch using your name and the assignment number.cactly 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 UM Learn After the due date and time assignments may be submitted but will lose of marks per hour late or portion thereof. The date and time of the last file wubmitted to the mark for the entire assignment You may submit your program multiple times, but cely the most recent version will be marked. These assignments are your chance to learn the material for the cums Code word independently We use software to compare all submitted assignments to each other, and pursue academic dishonestly vigorously Q1: Angular spaceship (6 marks] 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 (dl, d2, and 6), 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 valucs 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 & parameters. Draw your ship against a dark background ASSIGNMENT 2 DEPARTMENT AND COURSE NUMBER: COMP 1010 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 Dor L key on the keyboard, the ship should turn slowly to the right. When the user presses the A or Jkey 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 Sor 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 Dor 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 tums. 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 nor call this function yourself! Control the rotation of the spaceship if a,s. d. j. korl 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 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(C,,b) to convert three separate values for red, green, and 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 alteration 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 leftright, 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 comer. The distance from the passage to any comer 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 of 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! [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 channel to the edge of the canvas, without any comer of the ship touching any rectangle. You can see how the game should 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 of the correct edge of the canvas, and with an xor 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. 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 ASSIGNMENT 2 DEPARTMENT AND COURSE NUMBER: COMP 1010 "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 pt InRect(x,y,left, top, 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 showGameOverllessage() 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 Weck 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. [Programming Standards are worth 5 marks] 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. ASSIGNMENT 2: Floats, Trig, Booleans & Conditionals (Units 1-7 & 9-11) DEPARTMENT AND COURSE NUMBER: COMP1010 COURSE TITLE: Introduction to Computer Science 1 TERM: Winter 2020 Assignment 2 Dut DATE: FEBRUARY 14, 2020 BY 4:30 PM Notes: Name your sketch using your name and the assignment number.cactly 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 UM Learn After the due date and time assignments may be submitted but will lose of marks per hour late or portion thereof. The date and time of the last file wubmitted to the mark for the entire assignment You may submit your program multiple times, but cely the most recent version will be marked. These assignments are your chance to learn the material for the cums Code word independently We use software to compare all submitted assignments to each other, and pursue academic dishonestly vigorously Q1: Angular spaceship (6 marks] 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 (dl, d2, and 6), 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 valucs 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 & parameters. Draw your ship against a dark background ASSIGNMENT 2 DEPARTMENT AND COURSE NUMBER: COMP 1010 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 Dor L key on the keyboard, the ship should turn slowly to the right. When the user presses the A or Jkey 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 Sor 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 Dor 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 tums. 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 nor call this function yourself! Control the rotation of the spaceship if a,s. d. j. korl 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 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(C,,b) to convert three separate values for red, green, and 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 alteration 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 leftright, 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 comer. The distance from the passage to any comer 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 of 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! [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 channel to the edge of the canvas, without any comer of the ship touching any rectangle. You can see how the game should 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 of the correct edge of the canvas, and with an xor 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. 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 ASSIGNMENT 2 DEPARTMENT AND COURSE NUMBER: COMP 1010 "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 pt InRect(x,y,left, top, 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 showGameOverllessage() 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 Weck 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. [Programming Standards are worth 5 marks] 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