Lab: Pacman Game
Objective: Review homogeneous coordinates. Using these coordinates, and anonymous functions design a Pacman gameand create an animation as Pacman maneuvers the maze to gobble down the blue jewel.
Questions 1-2: Create an image of the Pacman character. Start by entering the coordinates for the Pacman image shown below. You will not draw the points. These are just to help you create the vectors for the Pacman figure.
i. To enter the points for your Pacman figure, start with the green point, and proceed counterclockwise through the yellow points and stop at the red point.
First, create a vector for the x-coordinates of each point.
ii. Now enter the y-coordinates for each point in the exact same order.
Note the y-value for the mouth is a half-integer.
iii. Later, we will need to use homogeneous coordinates to help translate the Pacman figure about a maze. Add the z-coordinates, all of which are 1. The number of 1's must be the same as the number of x's and y 's. Finish the assignment statement below.
iv. Be sure to define: pacman = [xcoords; ycoords; zcoords];
You will need this variable to move the Pacman around the maze later.
Now paste in an image of your Pacman character. Use the fill()command to create your plot. You will not need the z-coordinates at this stage. Draw your Pacman using yellow(instead of red). Do not draw the points! Label both axes and add a title to the plot.
Question 3:Set the 'LineWidth' to 4, and add a black eye centered at (2, 3.25) using the plot command.
We won't use this later in the lab, but it's a suggestion for future improvement.
Questions 4-5: Anonymous Functions for translate() and rotate()
Last week, you used standard function files, to define functions translate(dx,dy), rotate(angle_in_deg) and scale(k). This week, you will create the same functions, but using simple inline code to create each of these as an anonymous function in the same Matlab file you are working in.
Read more about anonymous functions here:
https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
Example:Create an anonymousfunctionto computetranslate(dx,dy).
This function should return the matrix:
Just enter then following line in your Matlab script for this lab. Do not use a new file.
translate = @(dx,dy)([1 0 dx;0 1 dy; 0 0 1]);
Question 5:Create an anonymousfunctionforrotate(d). This function should return the matrix R shown below. The angle d is in degrees. You must use cosd and sind, not cos and sin!
Question 6: The Pacman Maze
Create a figure showing this Pacman Maze.
As before, the 12 dots are just to help you see the
coordinates for each point. Do not draw the 12 dots!
i. To enter the 12 points for your Pacman Maze, start with the green point, and proceed counterclockwise(initially) through the yellow points and stop at the red point.
First, create a vector for the x-coordinates of each point.
ii. Now enter the y-coordinates for each point in the exact same order.
iii. Plot your maze using fill(). Set the color to cyan, and use a 'LineWidth' of 4. Add a title.
Questions 7-8: Place the Pacman at thestartof the maze, and a blue jewelat the end for it to gobble down.
Show the four "turning points" using a black asterisk for each.
i.Place the Pacman at the starting position. Recall you should have earlier defined:
pacman = [xcoords; ycoords; zcoords];
IMPORTANT: Use exactly the following command.
pacman_handle = fill(pacman(1,:),pacman(2,:),'y', 'LineWidth', 2);
Why do we need a handle to the Pacman image? Well, later on, we want to animate the Pacman as it moves around the maze. Each time it moves to a new position, we need to delete the image of its previous position. This handle will enable us to do that simply by using the code:
delete(pacman_handle)
ii.Place a blue jewelat the end of the maze. A good location is (14, 12.5).
Use the plot command with a diamond or 'd' for the marker. Set the 'MarkerFaceColor' to blue.
You should also use a handle for your jewel, so it can disappear when the Pacman eats it. Here's how:
jewel_handle = plot( Add arguments here.);
iii. As the Pacman moves from the start to the end of the maze, it encounters four turning points. Show each "turning point" using a black asterisk for each. Here are the x and y values for the four turning points.
tp1 = (22.5, 2.5), tp2 = (22.5, 22.5), tp3 = ( 2.5, 22.5), tp4 = ( 2.5, 12.5)
Questions 7-8: Replace this plot, with your new plot. Remember yours must have no dots! butshould show the turning points, the blue jewel and the yellow Pacman.
Questions9-10:You can see four asterisks added to the above maze. These are points where the Pacman is required to turn (rotate). Its mouth should always point in the direction it is moving.O-o!! We need to rotate the Pacman about these points, and not the origin. Our rotate command only works about the origin. To rotate counterclockwise by d degrees about a point (a,b) first translate by (-a,-b) to get to the origin, then rotate and finally translateback by (+a,b+), to get back to where you started.
The code block on the next page requires you have already done the following.
Check each condition carefully!
1. You have already defined the pacman coordinates using:
pacman = [xcoords; ycoords; zcoords];
2a. You defined pacman_handleusing exactly the following code:
pacman_handle = fill(pacman(1,:),pacman(2,:),'y', 'LineWidth', 2);
2b. Similarly, you have defined jewel_handle when you plotted the blue jewel.
3. You have defined and entered anonymous functions for rotate(d) and translate(dx,dy).
By testing if the Pacman has arrived at a turning point, we can automate the Pacman to navigate through the entire maze. Using a for loop, we move the pacman "forward" 0.5 units on each iteration with the help of your translate() command. You will also need the rotate() command, but just at each of the four turning points.
In the code on the next page, the first two turning points are given. Also the end of the game, with the blue jewel gobbled down has been provided. Your task is to add two if blocks to handle the third and fourth turning points.
Start by pasting the code on the next pageat the end of your script for today's lab, and test that it runs. If not, that means one of the above conditions has not been met exactly. Go back and check all the conditions.
Note the starter codebreaks after reaching the second turning point. This is so it will stop before reaching the incomplete code. Change that break to a continue right after you run the code a few times.
The code on this page is in a very small font to preserve its formatting. You might want to try reading it after pasting it into the end of your Matlab file for this week's lab.
That's it! Hope you enjoyed the game!
When your lab is complete, be sure to submit it as a PDF by the due date - this Thursday before 11:59PM. You have one more day, to submit the lab (but with a small penalty), and then the window closes for good and your grade will be zero. Only one submission is allowed per student.
Attachment:- Attachments.rar