讲解program语言编程、Python,c++程序调试、Java编程讲解
- 首页 >> Java编程 Key Mappings
Overview
You’ll be completing the infrastructure needed to run a game of snake that allows the player to switch
the controls of the game between various predefined key mappings.
Starter Code
Download the starter project, HW4_KeyMappings_Starter.zip.
See this video for a view of the final game.
Getting Started
First, take a little time to read through all the provided code. Draw yourself a class diagram about how
things fit together. Take notes on where the compiler errors and TODO comments are.
Classes
Do not modify these except where marked with TODO comments!
- Game1: All of the fields and methods you’ll need are defined for you. You’ll be completing a bit of
setup and adding implementations for Update() and Draw()
- ControlsManager: Loads control schemes and manages them in two data structures which you’ll
define. Also draws buttons for each scheme and detects scheme changes. Much of this is done for
you already. You’ll be filling in the gaps. (Similar to how you might be asked to complete code
started for you on co-op. ;) )
Do not modify these at all!
- Snake: Runs a game of Snake using the control mapping given to it.
- ShapeBatch: A static class to support the drawing of geometric shapes.
IGME-106 Spring 2021 Page 1
Content
Do not modify these at all and do not add new content!
- font: Use this for the button text
- titleFont: For instructions
- ControlSchemes.txt: Contains the control scheme information that will be loaded when the game
starts. The file is opened and parsed for you in the ControlsManager. Your job will be to save the
data to the appropriate data structure.
- Each line is formatted as: Scheme name, Direction, Key
IGME-106 Spring 2021 Page 2
Fixing the compiler errors
As provided, the ControlsManager class won’t compile. That’s okay. You’re going to fix it!
Let’s walk through it in order...
Direction enum
Nothing to worry about here.
This defines the four possible directions that can be mapped to keys in a control scheme. The
ControlsManager doesn’t actually care how each direction key is handled, just which key indicates
which direction for each scheme.
ControlsUpdateDelegate
In order to notify observers that the player has selected a different controls scheme, the
ControlsManager needs to define the signature it expects methods to have.
For this assignment, there’s only one observer: an instance of the Snake class.
Define a public delegate, ControlsUpdateDelegate, that will match the signature of Snake’s
SetControls method.
ControlsManager Class
Most of the structure of the ControlsManager class is already defined for you. However, as this is an
assignment focused on dictionaries, I want you to define the two fields needed to store control scheme
information.
Yes, two fields. We want to be able to lookup scheme information in two ways:
1. schemes
○ Given a scheme name (a string), get the Dictionary that maps Directions → Keys for that
scheme.
○ Yes, this is a Dictionary that maps strings to other Dictionary objects! You’ve done things
like this before with Lists of Lists, etc. Take your time and think through it carefully!
2. buttons
○ Given a button (a Rectangle), get the scheme name tied to that button.
The provided code won’t compile unless you define these fields using the exact names given and with
the appropriate types! Do not modify code elsewhere in order to make your fields compile!
Once you add the ControlsUpdateDelegate + the schemes and buttons fields, everything should
compile. It won’t do much, but you’re a whole lot closer now!
IGME-106 Spring 2021 Page 3
Loading the control schemes
Next, complete the ControlsManager constructor so that:
1. The fields are initialized properly with empty Dictionary objects that match their types.
2. For each line read in from the file:
a. If this is a new scheme, add new entry into the schemes dictionary
b. For all lines, add a mapping from key type -> dir for the scheme for this line
For example, for the line: WASD,Up,W
1. Add a new empty dictionary tied to the scheme string “WASD” into schemes (because this
happens to be the first line for the WASD scheme)
2. Add an entry into the WASD dictionary mapping Keys.W → Direction.Up
a. Local variables for the three elements in the line have already been created for you!
Once the schemes data structure is populated correctly, the remaining code in the constructor will fill
the buttons structure for you.
Make sure everything compiles and set a breakpoint in the constructor. You should be able to use the
debugger to verify that all of the data structures are not populated correctly!
IGME-106 Spring 2021 Page 4
ControlManager Methods
public void Update()
If the left mouse button was clicked, check each rectangle key in the buttons dictionary to see if it was
the one clicked by seeing if the current mouse position is in that rectangle.
If the button was clicked:
- Use buttons to look up the associated scheme name
- Update CurrentScheme
- Trigger the OnControlsUpdate event (if it has methods subscribed to it!) and pass in a
reference to the correct controls dictionary by looking it up using the schemes field.
private string SchemeInfo(string scheme)
For DrawButtonText to correctly overlay text onto the buttons, it calls SchemeInfo. Your job here is to
look up the controls dictionary and return a string built using each of the key mappings.
For example:
Make sure these compile and try adding temporary calls to them in Game1’s Update() method. You
won’t be able to see much, but if you use the debugger, you should be able to verify that everything
works correctly before moving on!
Comment out any temporary code once you are done testing!!!
IGME-106 Spring 2021 Page 5
Finish Game1
Fields & Content
All of the fields that you will need have already been defined and all content is already being loaded.
Review the code to see what is available to you, but do not add, remove, or modify anything!
Initialize()
Initialize is largely done. The window size is configured and the Snake and ControlsManager objects are
created for you.
Now that you have a working delegate type for the control manager’s OnControlsUpdate event,
subscribe the snake object’s SetControls method to it.
Update()
You won’t be implementing a full FSM for this assignment, but the behavior in Update() does vary
depending on the state of the program.
By default, and any time the player loses, the snake’s IsAlive property will be false.
If the snake is alive (i.e. the game is running), tell the object to update itself.
If the snake is not alive AND if Space was pressed AND is the current control scheme isn’t null, start
playing by calling Reset() on the snake.
Otherwise, tell the controls manager to check for updates.
Draw()
Like Update(), what needs to be drawn depends on if the game is actively running.
1. Always draw the snake in the background
2. If the snake isn't alive, draw the controls selection buttons (and then their text) & instructions
○ You do NOT have to draw the buttons yourself. Call the appropriate methods on the
controls manager to do it for you!
○ Remember that you'll need to use ShapeBatch.Begin/End or _spriteBatch.Begin/End
appropriately around different types of drawing methods!
IGME-106 Spring 2021 Page 6
Grading
Your grade for this homework assignment will be out of 100 points based on the rubric below.
ControlsManager
schemes field defined correctly 6
buttons field defined correctly 6
Constructor
Initializes schemes and buttons correctly 6
If the line has a new scheme, adds a new entry to the schemes dictionary 6
For all lines, adds a mapping from key type -> dir for the scheme for this line 6
Update()
Checks each button rectangle for mouse clicks 6
On a click in a button, updates the current scheme 6
Looks up the associated control dictionary correctly 6
Uses the OnControlsUpdate event correctly 6
SchemeInfo
Looks up the associated control dictionary correctly 6
Build a string representing the given controls scheme 6
Game1
Initialize() Subscribes the snake to the controls manager event 4
Update()
If the snake is alive, it's told to update 5
If the snake is not alive, determines correctly if the snake should be reset. 5
Otherwise, tells the controls manager to check for updates. 5
Draw()
Always draws the snake in the background 5
If the snake isn't alive, tells the controls manager to draw the buttons and their text. 5
If the snake isn't alive, draws instruction text. 5
Possible Deductions
Compilation or Run-time error -10
Naming conventions not followed -10
Noticeable lack of comments -10
Inconsistent or inappropriate use of whitespace & alignment -10
GitHub issues (e.g. no release made, link not submitted to myCourses, etc.) -10
Late submission (based on the time the release is made in GitHub) -10/day
IGME-106 Spring 2021 Page 7
Overview
You’ll be completing the infrastructure needed to run a game of snake that allows the player to switch
the controls of the game between various predefined key mappings.
Starter Code
Download the starter project, HW4_KeyMappings_Starter.zip.
See this video for a view of the final game.
Getting Started
First, take a little time to read through all the provided code. Draw yourself a class diagram about how
things fit together. Take notes on where the compiler errors and TODO comments are.
Classes
Do not modify these except where marked with TODO comments!
- Game1: All of the fields and methods you’ll need are defined for you. You’ll be completing a bit of
setup and adding implementations for Update() and Draw()
- ControlsManager: Loads control schemes and manages them in two data structures which you’ll
define. Also draws buttons for each scheme and detects scheme changes. Much of this is done for
you already. You’ll be filling in the gaps. (Similar to how you might be asked to complete code
started for you on co-op. ;) )
Do not modify these at all!
- Snake: Runs a game of Snake using the control mapping given to it.
- ShapeBatch: A static class to support the drawing of geometric shapes.
IGME-106 Spring 2021 Page 1
Content
Do not modify these at all and do not add new content!
- font: Use this for the button text
- titleFont: For instructions
- ControlSchemes.txt: Contains the control scheme information that will be loaded when the game
starts. The file is opened and parsed for you in the ControlsManager. Your job will be to save the
data to the appropriate data structure.
- Each line is formatted as: Scheme name, Direction, Key
IGME-106 Spring 2021 Page 2
Fixing the compiler errors
As provided, the ControlsManager class won’t compile. That’s okay. You’re going to fix it!
Let’s walk through it in order...
Direction enum
Nothing to worry about here.
This defines the four possible directions that can be mapped to keys in a control scheme. The
ControlsManager doesn’t actually care how each direction key is handled, just which key indicates
which direction for each scheme.
ControlsUpdateDelegate
In order to notify observers that the player has selected a different controls scheme, the
ControlsManager needs to define the signature it expects methods to have.
For this assignment, there’s only one observer: an instance of the Snake class.
Define a public delegate, ControlsUpdateDelegate, that will match the signature of Snake’s
SetControls method.
ControlsManager Class
Most of the structure of the ControlsManager class is already defined for you. However, as this is an
assignment focused on dictionaries, I want you to define the two fields needed to store control scheme
information.
Yes, two fields. We want to be able to lookup scheme information in two ways:
1. schemes
○ Given a scheme name (a string), get the Dictionary that maps Directions → Keys for that
scheme.
○ Yes, this is a Dictionary that maps strings to other Dictionary objects! You’ve done things
like this before with Lists of Lists, etc. Take your time and think through it carefully!
2. buttons
○ Given a button (a Rectangle), get the scheme name tied to that button.
The provided code won’t compile unless you define these fields using the exact names given and with
the appropriate types! Do not modify code elsewhere in order to make your fields compile!
Once you add the ControlsUpdateDelegate + the schemes and buttons fields, everything should
compile. It won’t do much, but you’re a whole lot closer now!
IGME-106 Spring 2021 Page 3
Loading the control schemes
Next, complete the ControlsManager constructor so that:
1. The fields are initialized properly with empty Dictionary objects that match their types.
2. For each line read in from the file:
a. If this is a new scheme, add new entry into the schemes dictionary
b. For all lines, add a mapping from key type -> dir for the scheme for this line
For example, for the line: WASD,Up,W
1. Add a new empty dictionary tied to the scheme string “WASD” into schemes (because this
happens to be the first line for the WASD scheme)
2. Add an entry into the WASD dictionary mapping Keys.W → Direction.Up
a. Local variables for the three elements in the line have already been created for you!
Once the schemes data structure is populated correctly, the remaining code in the constructor will fill
the buttons structure for you.
Make sure everything compiles and set a breakpoint in the constructor. You should be able to use the
debugger to verify that all of the data structures are not populated correctly!
IGME-106 Spring 2021 Page 4
ControlManager Methods
public void Update()
If the left mouse button was clicked, check each rectangle key in the buttons dictionary to see if it was
the one clicked by seeing if the current mouse position is in that rectangle.
If the button was clicked:
- Use buttons to look up the associated scheme name
- Update CurrentScheme
- Trigger the OnControlsUpdate event (if it has methods subscribed to it!) and pass in a
reference to the correct controls dictionary by looking it up using the schemes field.
private string SchemeInfo(string scheme)
For DrawButtonText to correctly overlay text onto the buttons, it calls SchemeInfo. Your job here is to
look up the controls dictionary and return a string built using each of the key mappings.
For example:
Make sure these compile and try adding temporary calls to them in Game1’s Update() method. You
won’t be able to see much, but if you use the debugger, you should be able to verify that everything
works correctly before moving on!
Comment out any temporary code once you are done testing!!!
IGME-106 Spring 2021 Page 5
Finish Game1
Fields & Content
All of the fields that you will need have already been defined and all content is already being loaded.
Review the code to see what is available to you, but do not add, remove, or modify anything!
Initialize()
Initialize is largely done. The window size is configured and the Snake and ControlsManager objects are
created for you.
Now that you have a working delegate type for the control manager’s OnControlsUpdate event,
subscribe the snake object’s SetControls method to it.
Update()
You won’t be implementing a full FSM for this assignment, but the behavior in Update() does vary
depending on the state of the program.
By default, and any time the player loses, the snake’s IsAlive property will be false.
If the snake is alive (i.e. the game is running), tell the object to update itself.
If the snake is not alive AND if Space was pressed AND is the current control scheme isn’t null, start
playing by calling Reset() on the snake.
Otherwise, tell the controls manager to check for updates.
Draw()
Like Update(), what needs to be drawn depends on if the game is actively running.
1. Always draw the snake in the background
2. If the snake isn't alive, draw the controls selection buttons (and then their text) & instructions
○ You do NOT have to draw the buttons yourself. Call the appropriate methods on the
controls manager to do it for you!
○ Remember that you'll need to use ShapeBatch.Begin/End or _spriteBatch.Begin/End
appropriately around different types of drawing methods!
IGME-106 Spring 2021 Page 6
Grading
Your grade for this homework assignment will be out of 100 points based on the rubric below.
ControlsManager
schemes field defined correctly 6
buttons field defined correctly 6
Constructor
Initializes schemes and buttons correctly 6
If the line has a new scheme, adds a new entry to the schemes dictionary 6
For all lines, adds a mapping from key type -> dir for the scheme for this line 6
Update()
Checks each button rectangle for mouse clicks 6
On a click in a button, updates the current scheme 6
Looks up the associated control dictionary correctly 6
Uses the OnControlsUpdate event correctly 6
SchemeInfo
Looks up the associated control dictionary correctly 6
Build a string representing the given controls scheme 6
Game1
Initialize() Subscribes the snake to the controls manager event 4
Update()
If the snake is alive, it's told to update 5
If the snake is not alive, determines correctly if the snake should be reset. 5
Otherwise, tells the controls manager to check for updates. 5
Draw()
Always draws the snake in the background 5
If the snake isn't alive, tells the controls manager to draw the buttons and their text. 5
If the snake isn't alive, draws instruction text. 5
Possible Deductions
Compilation or Run-time error -10
Naming conventions not followed -10
Noticeable lack of comments -10
Inconsistent or inappropriate use of whitespace & alignment -10
GitHub issues (e.g. no release made, link not submitted to myCourses, etc.) -10
Late submission (based on the time the release is made in GitHub) -10/day
IGME-106 Spring 2021 Page 7