B CUSP 110B – Digital Thinking

Homework 4: XNA And the SoccerBall!

 

 

 

Goal: The purpose of this exercise is to get your hands dirty with C# programming, experiencing with the edit/compile/run cycle.

 

Abstract: You will modify the 1.MovingABallAround project (click to download, unzip the project) to include more supported actions. All of your code will go into the same ClassExample.cs file.

 

Example solution: here is a sample solution for this homework. Down from this link, unzip the file (make sure you unzip the file and that you are not opening a zip file!!), and double click on ClassExample.exe to run the solution.

 

Button press and attributes on circle: We will experiment with the following attributes on the circle

 

·         Label: text string show on top of the circle

·         LabelColor: color of the text string shown on the circle

·         Center: center position of the circle

·         Radius: radius of the circle (next time)

·         Visible: visibility of the circle

 

Question 4a: Label on circle: type in the following two lines of code inside the body of InitializeWorld() function:

 

myCircle.LabelColor = Color.Red;

myCircle.Label = "BigCircle";

 

Compile and run your program. What do you see? Type in your answer as comments to the program, right next to your code. Remember, we comment our program by begin with “//” and typing useful information, e.g.,:

 

// this is a line of comment, ignored by the computer

 

So, your answer to 4a, will look something like:

 

myCircle.LabelColor = Color.Red;

myCircle.Label = "BigCircle";

// Answer to 4a: the above two lines of code …

// (you answer comes here)

// Your answer should not be more than 1-2 sentences long.

 

 

 

Question 4b: Understanding button press: When you compile/run the code that is given to you, you will observe that if you type “K” on the keyboard, the circle disappears. Typing “K” again makes the circle re-appear. This behavior is implemented by:

 

if (GamePad.ButtonAClicked())

{

myCircle.Visible = !myCircle.Visible;

PlayACue("Squirt");

}

 

Notice that in the above code, “ButtonA” refers to the “A” button on the Xbox 360 game pad. That button is mapped to the “K” key on your keyboard. Comment the above code (refer to 4a on how to put “comments” in your program) to explain the behavior you have observed. Once again your answer should not be more than 2-3 sentences long.

 

 

Question 4c: Working with button press.  Refer to 4a and 4b carefully, now, we want to control the circle’s Label and LabelColor with the “X” and “Y” buttons on a GamePad (mapped to J and I keys).

 

When user press the J-Key on the keyboard (or X-Button on the Xbox 360 controller):

·         Label: “J-Key”

·         LabelColor: Purple

 

When user press the I-Key on the keyboard (or Y-Button on the Xbox 360 controller):

·         Label: “I-Key”

·         LabelColor: Green

 

Now, implement the above functionality! Hint, you will need to use: GamePad.ButtonXClicked() and GamePad.ButtonYClicked() functions. Your solution will include two extra “if” statements testing for if the corresponding button is clicked, looking something like:

 

if (GamePad.ButtonXClicked())

{

… do something

}

 

if (GamePad.ButtonYClicked())

{

… do something else

}

 

 

 

Question 4d: (Extra) Change the speed of the circle movement: we can use math operators to change the circle’s parameters. For example, in UpdateWorld() function, we changed the circle’s center position by:

 

myCircle.Center += GamePad.ThumbSticks.Right;

 

Here, we use the RightThumbStick (maps to the arrow keys) value to change the center position. You are told the ThumbStick gives a value between +1 and -1. The “+=” operator adds the “1” or “-1” to the X/Y values of the circle center and thereby moving it around. We notice the movement of the circle is rather slow. Now, we can further slowdown the movement of the circle by scaling the ThumbStick values. For example if we replace the above line with:

 

myCircle.Center += GamePad.ThumbSticks.Right * 0.1f;

 

The “0.1f” factor will scale ThumbStick values to between “0.1” and “-0.1” and thereby will slowdown the movement of the circle even more! Try it: by typing in the above and compile/run.

 

Now, to complete 4d, modify this line such that the circle’s movement is 5x the original speed.

 

 

To Turn In

Make sure you do the following:

1.      Type in your name into ClassExample.cs as comments in the beginning of the file.

2.      Print out: your ClassExample.cs. (REMEMBER!! Your name!!) PLEASE print in Landscape Orientation.

3.      Submission: Bring your printout to class on Wednesday, and submit your ClassExample.cs to Homework4 submission area.

Wrap Up

In this assignment you familiarized yourself with the Visual Studio development environment; learned how read and understand C# program; and modified a C# program to implement specific behavior.