4/19 - Variables

csharp-logo-1.png

Essential Question: How are variables used in creating all the different elements we expect in a game while coding C# in Microsoft Visual Studio?

Objective: I will be able to declare, initialize, and reassign variables in the C# language using the Microsoft Visual Studio IDE.

Word Wall:
C#
variables
initialize
assign
declare
variable declaration
data types
built-in types
string
integer
boolean - true or false


float - store floating-point values; less precise than a double; To declare a decimal value as a float, a suffix must be used.


double - used to store floating-point values (non-whole numbers)


unsigned integer - uint - unsigned integer" and is used to store positive, numeric values. It has the significant range of 0 to 4,294,967,295

long - has as massive range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

sbyte - has a small range of -128 to 127


short - has a medium range of -32,768 to 32,767


ulong - 0 to 18,446,744,073,709,551,615

 

byte - integral, numeric values. A byte can only contain values within a range of 0 to 255

ushort - store positive, integral values. A short has a medium range of 0 to 65,535

enum - short for enumeration, which consists of a set of named constants

char - store a single character (letter)

decimal - store floating-point values. A decimal has a small range, but a high precision of (-7.9 x 1028 to 7.9 x 1028) / (100 to 28) with an M suffix

signed
unsigned
type casting
implicit
explicit
variable manipulation
literal
assignment operator
initial value
code re-factoring
constant
default value
concatenation

Check this link Links to an external site. out for more information on C# variable types.

Introduction to lesson 

We will experiment using and working with variables to add and change different elements in our video game.

Arc 1 Intro to Programming with C# - Begin your game programming journey by learning the basic components of C# a language used commonly in game design.

Begin your game programming journey by learning the basic components of C# a language used commonly in game design.

 

To assign a variable:

int playerCoins;

To assign a number of variables on one line:

int x = 10, y = 20;

To convert an integer to a string:

string convertedString = myInteger.ToString();

Task:

1. Go to GG Interactive: Game Development in C# Module (Links to an external site.). For today's lesson click Variables Links to an external site.lesson.

2. In the resources tab, or the Video playlist (highly recommended for this part of learning GUI), learn about how to open files in Visual Studio.
 
Open Project > Find the .sln file inside the project you want to open

3. Get to know the Microsoft Visual Studio GUI.

 

4. Get to know the Microsoft Visual Studio GUI. Know that the exercise will guide you through your learning with Focus Areas. 

5. Complete the Exercise Links to an external site.In this exercise, we will declare and use some of the most common variable types. While there are many different data types, this exercise will only make use of integers, floating point values, and strings. Follow the steps below to get a very basic game scene rendering.

You should see all the file in the folder what you see in the GG Interactive Exercise.

Follow all the instructions of the Exercise. Enjoy playing the game. You will create a game similar to this game. Doesn't the final result look a lot like a game created in MIT Scratch?

EXERCISE - VARIABLES - CLASSWORK********************************************************************

Open Visual Studio and sign in with your Microsoft login. 

Downloadvariables.zip Download variables.zip Use Module 1 Step 3 as the title for this exercise. Save them in the location specified in this instruction and then click on Extract All.

Complete all the exercise steps & all the extra credit.

Exercise Steps

Steps

  1. Background Image - Use a string variable (by passing it to a method) to create a new background.
    1. First, let's see what the Exercise looks like without modification. Run the Exercise by clicking the Start button or pressing F5. We should see a blue background color and the text "Total Bubbles:".
    2. Next, open the variables.cs source file and scroll down to Focus Area 1.
    3. Declare a new string variable named _backgroundTexture and give it an initial value of "Graphics\\AsteroidBackground".
    4. Scroll down to Focus Area 2 and find this line of code:
      1. CreateBackground();
    5. Inside of the parenthesis, type the name of the string variable we just declared.
    6. Run the Exercise by clicking the Start button or pressing F5. Instead of a solid color, we should now see a space background image.

  2. Spawning Ships - Use an integer variable (by passing it to a method) to set the number of ships the game should create (i.e. spawn).
    1. Scroll back up to Focus Area 1.
    2. Declare a new integer variable named _shipCount and set its initial value to 10.
    3. Scroll down to Focus Area 2 and locate this line of code:
      1. CreateShips();
    4. Inside of the parenthesis, type the name of the integer variable we just declared.
    5. Run the Exercise by clicking the Start button or pressing F5. We should see several spaceships flying across the screen. What do you think would happen if you set the value to 5? If you try, don't forget to set it back to 10.

  3. Changing Backgrounds - Let's try creating a different background.
    1. Scroll back up to Focus Area 1.
    2. Change the value of _backgroundTexture to "Graphics\\bubblePopper_BackGround".
    3. Run the Exercise by clicking the Start button or pressing F5. The background should be completely different. You just declared a variable, set its value, and passed it a method (we did that by putting it between the parenthesis).

  4. Spawning Bubbles & Update Counter - Use variables to switch the game from using ships to bubbles and then display the total bubble count.
    1. Scroll back up to Focus Area 1.
    2. Declare two new integers variables named _bubbleCount and _totalBubbles but don't set their initial values (we will update the variable in the next step).
    3. Inside of Focus Area 2 and on the line after the CreateBackground method, set the value of both of the new integer variables to 100.
    4. Find the method CreateBubbles();
    5. Inside of the parenthesis, type out _bubbleCount to determine how many bubbles to create.
    6. Run the Exercise by clicking the Start button or pressing F5. Notice that we are still seeing ships and no bubbles.
    7. Scroll back up to Focus Area 1.
    8. Declare a new boolean variable named _isSpaceGame and set its initial (i.e. initializing) value to false.
    9. Scroll down to Focus Area 2 and locate this line of code: if (true)
    10. Inside of the parentheses, replace true with the boolean variable we just declared.
    11. Run the Exercise by clicking the Start button or pressing F5. We should now be seeing bubbles instead of ships, since the boolean variable is toggled to false.
    12. Next, we need to update the total number of bubbles created. Scroll down to Focus Area 3.
    13. Set the value of _totalBubbles equal to itself plus _bubbleCount (see Variable Manipulation topic in the reference material for an example).
    14. Scroll down to Focus Area 4 and locate the counter which use this line of code:
      1. _spriteBatch.DrawString(Font, "Total Bubbles: ", new Vector2(0.0f, 0.0f), Color.White);
    15. After "Total Bubbles: ", type out the plus symbol and the _totalBubbles variable to update the value of the text rendered to screen.
    16. Run the Exercise by clicking the Start button or pressing F5. The text on screen will now show the accurate number of bubbles created.

  5. Changing Bubbles - Use another variable to set bubble size and use a previous variable to update the total count.
    1. The bubbles are currently too large and too few. Scroll back up to Focus Area 1.
    2. Declare a new floating point variable named _bubbleSize and set its value to 2.5f.
    3. Scroll back down to Focus Areas 2 and change the value of _bubbleCount to be larger, such as 200.
    4. Locate this line of code:
      1. CreateBubbles(_bubbleCount);
    5. After _bubbleCount, add a comma and then _bubbleSize.
    6. Run the Exercise by clicking the Start button or pressing F5. We should now see many smaller bubbles. Watch the total count update every time a new batch of bubbles appear. Can you figure out what line of code is making the total update with every batch.

Exercise: 40 points

EXERCISE - TRICK SHOT- PROJECT************************************************

6. Complete the TRICK SHOT (Beta) exercise (40 points) - In this open ended exercise, you will demonstrate your understanding of declaring and using variables. You will modify the code to complete a mini-game of trying to knock down a target using a cannon ball.

Download and follow the directions: physics_variables.zip Download physics_variables.zip
Steps

  1. Make game operational.
      1. Run the Exercise by clicking the Start button or pressing F5. A dialog should popup notifying us there are errors. Click the dialog'sNo button.
      2. Go to the Error List tab that should be at the bottom of your Visual Studio screen. If it is missing, go to the View menu and selectError List.

    variables_exercise_two_error
    Figure 1 Error List

    1. The error indicates there is a type conversion issue. Double click on the error to jump to the line that has the issue, or find Focus Area 1.
    2. Correct the error by entering the correct type conversion for the CannonReadyCountdownTimer variable.
    3. Run the Exercise again. It should now correctly start up showing the cannon on the left, and two moving barriers on the right. We can now finish off the rest of the game.

  2. Build the Target's variables.
    1. We will now add the target to the game so we have something to shoot at. Go to Focus Area 2.
    2. Go to Focus Area 2. Declare a private float variable named TargetPositionX, and assign it a value of 60.0f.
    3. Just underneath, declare a another private float variable and name it TargetPositionY. Assign a value of -5.0f.
    4. Go to Focus Area 3. Declare a private float variable named TargetSize, and assign it a value of 5.0f.
    5. Go to Focus Area 4. Enter the following code to call the CreateTarget function as follows, and assign the results to the physicsBodyvariable (working with functions is in a future module):
      1. Body physicsBody = CreateTarget(TargetPositionX, TargetPositionY, TargetSize, TargetColor);
    6. On the following line but still within Focus Area 4, assign the value of the physicsBody variable to the Target variable.
    7. Run the Exercise again. There should now be a target on the right. Try rotating the cannon and firing the cannon ball to see if you can hit the target.

  3. Adjusting the cannon ball's impulse force.
    1. After playing the game in the last step, you likely noticed that it was impossible for the cannon ball to reach the target. It just wasn't being fired with enough force to clear the tops of the barriers. We'll adjust that now.
    2. Go to Focus Area 5. You'll see that the CannonBallImpulse variable is already being assign a value.
    3. Increase the CannonBallImpulse variable's value and try running the Exercise again. Continue to iterate on the game by increasing this variable's value until you feel you have enough force to reach the target. Increasing by a value of 50 each time may help you to find the correct balance of too little vs. too much.

  4. Other things to try. THESE MUST BE DONE AND ARE PART OF THE TOTAL POINTS
    1. Now that you can hit the target, try changing the target's size by modifying the TargetSize variable. Have you made the game harder or easier?
    2. Try changing both the cannon ball's impulse, and the target's size. How does this change the feel of the game?
    3. Can you get your timing right to put the cannon ball between the moving barriers and hit the target?

Extra Credit - MANDATORY - YOU MUST DO IT AND IT IS PART OF THE 50 POINTS. Extra Credit is only a GG Interactive label. These steps are not an option and must be included. Be prepared to do a code walk through the project.

  1. Change the target's color by modifying the values of the TargetColor variable.
  2. Change how quickly the cannon is ready after firing a cannon ball. This variable is found under the Cannon region.
  3. Change the speed and maximum distance of the barriers. These variables are found under the Barriers region.
  4. Add more targets to knock down.
  5. Add a counter on screen to track the number of cannon balls fired. Use this to see who can use the least number of balls to knock down the target.

Total points for PROJECT: 60 points

Total points for LESSON: 100 points

7. Read the next part of the Module: Exploring Visual Studio. (Links to an external site.) Since this module takes a download and you may not have a computer to hold all the application required (audio, video, graphics, interaction), we will push the Flipped Classroom way of doing things in this unit. Read and learn the content at home, do the projects in class.