Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
I can…
Explain the significance of using a computer programming language.
Describe the basic template of the Monogame framework.
A series of steps (actions) performed in a specific order
Specifies
The ACTIONS to be executed
The ORDER in which these actions are to be executed
Example
Making a PBJ Sandwich
“Rise and Shine” Algorithm
Rise and ShineGet out of bed
Take off pajamas
Take a shower
Get dressed
Eat breakfast
Father School Supplies
Get a ride to school
A combination of English and computer language designed to present a program design
Shows the steps involved in solving a problem on the computer that combines an algorithm with partial computer code
**outline/rough draft of code….a form of pre-planning**
English = confusing language & not specific
Words with same spellings (lead – the verb vs lead – the metal)
Phrases with multiple meanings (“break a leg”)
Computer Languages invented
Straight forward
Each word/instruction has one role
Are…..
PRECISE (no ambiguity)
and
PERFECT (spelling and order)
Uses the C# programming language
Monogame – has many prebuilt program components
Sounds
Images
Game Pad
Keyboard
Text
Color
Game Loop
// single line comment
/* <comment>
*/
Benefits
Others can read and comprehend purpose of your code
Finding Errors
Remind you of the purpose of each set of code so in a day, a week, a month, a year, etc you will still remember its purpose
Requirements
You WILL BE required to comment ALL of your code
Programming Language used in Monogame
Has concepts similar to all programming languages
One new change (for students who have only taken Intro in VB) = all statements of code end in a semicolon “;”
Signifies the end of the line
No semicolon means the computer will assume that the line below is a continuation of the instruction from the preceding line
5 Methods in the Skeleton to be programmed
1. Initialize
2. LoadContent
3. UnloadContent
4. Update
5. Draw
Runs ONE time
Runs at Beginning of Game
Every time the game Runs
Ex: Reset scores
Reset number of lives
Reset Counter
Reset Vector, Rectangle
Called once per game
Load content into your game
3D models
Textures
Fonts
Music
Etc
Called once per game
Will unload unneeded content from the game
The main portion of code
Called frequently while game is running about 60 times per second
All components that must be updated here
Objects - Collision Detection
Game World - Audio
User Input - Variable Changes
Called frequently while game is running about 60 times per second
Game will consistently redraw itself
This method is ONLY for redrawing the screen, variables beyond those related to drawing should be handled and updated in the “Update” method
I can...
Explain the purpose of variables and objects.
Explain the differences between variables and objects.
Declare, Initialize, and Assign variables and objects.
Data Types: types of data (values) a program can hold
1.Variables
2.Objects
The name of a LOCATION in the computer’s RAM
Stores single piece of information
Mailbox
Has an address (location)
Holds one piece of information
byte – holds numeric values 0 – 255
int – holds a positive or negative integer value
double – holds any number (decimal)
char – holds a single character
string – holds line a text
bool – holds true or false value
Counter
Score
Health
Speed
Location
Time
Lives Remaining
The possibilities are endless!
1. Must START with a letter (upper or lower case)
2. Can include letters, numbers, and underscores (_)
3. Any length
4. Name is CASE SENSITIVE
5. Name should relate to purpose in program
1. high_score
2. _hello
3. red1
4. 1red
5. Player#1
6. game-time
7. Counter7
8. main_stage
9. playerOne
10.redIntensity!
1. high_score
2. _hello
3. red1
4. 1red
5. Player#1
6. game-time
7. Counter7
8. main_stage
9. playerOne
10.redIntensity!
All variables must be
1. Declared
2. Initialized
Defines the Variable – gives it a type and gives it a name
Type the type of variable
Identifier unique name for variable
<type> <identifier>;
Ex:
int score;
byte red;
Gives the variable a starting value
Identifier unique name for variable
“=“ sign
What should be saved in the variable location
<identifier> = <value>;
Ex:
score = 10;
red = 0;
Defines and gives the variable a value
Type
Identifier
“=“ sign
What should be saved in the variable location
<type> <identifier> = <value>;
Ex: int score = 10;
byte red = 0;
Declaration
Initialization
1. A integer variable called age that holds your age.
2. A Boolean variable called answer that holds the value true
3. A String variable called firstname that holds your first name
4. A double variable called score1 that will hold a value of 30.5
1. A integer variable called age that holds your age.
a) int age = 25;
b) age = 25;
c) int age = 25;
2. A Boolean variable called answer that holds the value true.
a) bool answer;
b) answer = true;
c) bool answer = true;
3. A String variable called firstname that holds your first name.
a) string firstName;
b) firstName = “Sarah”;
c) string firstName = “Sarah”;
4. A double variable called score1 that will hold a value of 30.5.
a) double score1;
b) score1 = 30.5;
c) double score1 = 30.05;
You can reassign the value of the variable at ANY time
REWRITES what is saved at that location
Original value = Erased
Ex: int score = 10;
score = 50;
score = 20;
**score will not remember that it was originally set to 10 or was set to 50 at some point it will only remember that it is currently set to 20.
Similar to variables
Objects group together similar characteristics together all related, called parameters
Store these characteristics together in memory
Ex:
Color object has a red value, a green value, and blue value
Rectangle object has an x location, y location, width, and height
Font object has a font family and size value
GamePad object has a value for each button identifying if each button is pressed or released
**there are MANY different types of objects – too many to list and you can create your own as well!**
Defines the Object
Type the type of Object (generally capitalized)
Identifier unique name for Object
<type> <identifier>;
Ex:
Color backgroundColor;
SpriteBatch sprite1;
**Object Types has a first letter than is capitalized**
Gives the object values
Identifier
“=“ sign
new
Type with parameter list (characteristics list) each object has a set order in which its characteristics should be entered
Ex: in Color - the first number is the red intensity, second number is the blue intensity, and third number is the green intensity
<identifier> = new <type>(<parameters list>);
Ex:
backgroundColor = new Color(10, 40, 100);
Initialize
Gives a singular initial value to start with
Variables
Instantiate
Creates an “instance” of a class creates an object
Objects have characteristics (properties) and actions (methods)
Defines the gives the object values
Type
Identifier
“=“ sign
new
Type with parameter list
<type> <identifier> = new <type>(<parameters list>);
Ex:
Color backgroundColor = new Color(10, 40, 100);
Declaration
Instantiation
Numerous Types
Many Built It
Color is one type
Too many others to mention
Can design our own objects as well
Mario Kart
Characters special item, image, sound, car, partner
Wasn’t built in – the programmers had to design the object instead
1. A Color object named favorite that contains 3 byte parameter values: 40, 100, 200
2. A Bug object named ladybug that contains 4 parameter of values: “red”, 7, 2.0, true
3. A Car object named CRV that contains parameter values: “987htg”, 2006
1. A Color object named favorite that contains 3 byte parameter values: 40, 100, 200
a) Color favorite;
b) favorite = new Color(40, 100, 200);
c) Color favorite = new Color(40, 100, 200);
2. A Bug object named ladybug that contains 4 parameter of values: “red”, 7, 2.0, true
a) Bug ladybug;
b) ladybug = new Bug(“red”, 7, 2.0, true);
c) Bug ladybug = new Bug(“red”, 7, 2.0, true);
2. A Car object named CRV that contains parameter values: “987htg”, 2006
a) Car crv;
b) crv = new Car(“987htg”, 2006);
c) Car crv = new Car(“987htg”, 2006);
What do you think is the difference between a local
and global variable/object?
Where you declare the variable/object affects where this variable/object can be used or changed
Defined in the Game1 Class at the top of the program
Can be used throughout entire program (in all methods)
Benefits: accessed from anywhere in program
Disadvantages: could be accidentally changed unintentionally in another method
Defined in method where being used
Can be used ONLY within the method where it is defined defined
Benefits: less likely to be accidentally changed
Disadvantages: cannot be accessed from elsewhere in the program if needed.
Local: term “bubbler”
Used LOCALLY in Wisconsin
Elsewhere unknown
Global: term “hello”
Known throughout globe as a greeting
Different cultures/societies recognize this term
Global variables – DECLARED for
the entire game at the beginning of
the program – can be used by ALL
methods
Local object – declared inside the draw method and can
therefore ONLY be used in the draw method
I can…
Explain the purpose of a class.
Define and provide examples of a method.
Label and locate variables, objects, classes, and methods.
Explain the connection between a class, object, and method.
Explain how classes and methods are like an office building.
A template for an object
Characteristics list
Methods (actions) the object can complete
Characteristics of Kodu
Health
Speed
Emotion
Invisible
Size
Color
Actions of a Kodu
Move
Shoot
Eat
Jump
Express Anger
Generic Kodu “Class” template from which all Kodu objects are created
All Kodu have the actions (methods) listed in the Kodu class that they can DO
All Kodu will have characteristics – but these characteristics will vary as each Kodu is different with different characteristics
Many already created and built into Monogame
Game1
GraphicsDevice
Color
SpriteBatch
GamePadState
Keyboard
….
Can build own as well
Numerous characteristics
We decide these – variables/objects globally defined
5 Methods (actions!)
Initialize
Load Content
Unload Content
Update
Draw
Action an object of the class can run
“Mini Program” with a specific task
Ex: In the GamePad class
Method called “GetState” this method will get the state of all the setting of the game pad (buttons, joystick, triggers, etc) and will send these states back to the program
Variables and Objects (the information) a method needs to know in order to complete the action and “do its job”
Ex: The “GetState” method from the GamePad class
Takes in one parameter the pad you want to find the state of
Without this information, the method will not know what to find the state of
Using the “dot” method
<class name>.<method name>(parameter list);
(generally how a method call will look in Monogame)
GamePad.GetState(Player.One);
Keyboard.IsKeyDown(keys.B);
GraphicsDevice.Clear(Color.green);
Keyboard.GetState(); Note, no parameters
Intersection.(Rectangle1, Rectangle2) Note, multiple parameters
Create a Moodlight program for Space Use
Will run the code to light a panel on the wall
Colors will appear to the astronauts in space as you program them to appear
Select Monogame Windows Project
Save as “MoodLight”
Hide all Comments
What happens? Why do you think this happens?
**under draw method**
GraphicsDevice.Clear(Color.CornflowerBlue);
Base.Draw(gameTime);
GraphicsDevice.Clear Draws the Blue Screen!
Base.Draw calls the Draw Method to run
GraphicsDevice.Clear(Color.CornflowerBlue);
o GraphicsDevice = class
o .Clear = calls the clear method in the GraphicsDevice class
o Color.CornflowerBlue = parameter taken in by the clear method takes in a Color object and clears the screen to the color provided
GraphicsDevice.Clear(Color.CornflowerBlue);
Color.CornflowerBlue = preprogrammed Color object
Try to change it to another color, what did you do?
Color object named: backgroundColor
- Only needs to be accessed by Draw
- Define LOCALLY in Draw
- Initialize so all red, green, blue values 0
3 Parameters – red, green, blue(ORDER MATTER)
protected override void Draw(GameTime gameTime)
{
Color backgroundColor = new Color(0, 0, 0);
GraphicsDevice.Clear(backgroundColor);
base.Draw(gameTime);
}
Color takes in 3 parameters – 3 BYTE values, in order representing the red intensity value, green intensity value, and blue intensity value
Alter red, green, blue intensities to change color
(range in values of 0 – 255)
Color(red, green, blue)
Try to create colors – record color combinations!
Red, Green, Blue, Yellow, Purple, Orange
Experiment!!!
What if we wanted to individually change each red, green, and blue intensity value?
Red, Green, Blue Intensity:
- Need to “update” in update method
- Use to “draw” background color
- Want to use in update AND draw method
- Define GLOBALLY
- Only take values 0-255 so “byte” type
After class of Game1 Defined…define the following:
byte red = 0;
byte green = 0;
byte blue = 0;
protected override void Draw(GameTime gameTime)
{
Color backgroundColor;
backgroundColor = new Color(red,
green, blue);
GraphicsDevice.Clear(backgroundColor);
base.Draw(gameTime);
}
protected override void Update(GameTime gameTime)
{
if(GamePad.GetState(…….)Exit();
red++;
green++;
blue++;
base.Update(gameTime);
}
Update makes changes to the game components
Draw Uses these updates to redraw the background
BOTH RUN 60 TIMES PER SECOND
Fill in the handout with pictures and Notes as you Go
Classes and Methods are just like how a business office would run!
Each class is ONE Office with employees and
resources
Each employee has a job or “Action” they are assigned to do.
Each employee in an office represents a Method because in
Method in a class has a very specific job to perform.
1) Each time Mr. Draw’s is “called” and his
phone rings, he is told to “do his job.”
2) Mr. Draw’s job first requires him to go
to the table, and find the values of the
three colors on the desk.
3) Next, he must call Ms. Clear (another
employee/method in the Graphic Device
office/class) and give her the values over
the phone so she can clear the screen to
the correct color.
Once done, Mr. Draw sits down and waits
for his next call.
NOTE: Mr. Draw represents
the Draw Method in our
current Moodlight project
we are working on.
**compare to our code**
protected override void Draw(GameTime gameTime)
{
Color backgroundColor;
backgroundColor = new Color(red, green, blue);
GraphicsDevice.Clear(backgroundColor);
base.Draw(gameTime);
}
Each time Mrs. Update is “called” and her
phone rings, he is told to “do her job.”
Mrs. Update’s job requires her to access
the 3 color values on the table, update the
values (in the case of our program, add
one to each value).
Once done, Mrs. Update sits down and
waits for her next call.
NOTE: Mrs. Update
represents the Update
Method in our current
Moodlight project we are
working on.
**compare to current
code**
protected override void Update(GameTime gameTime)
{
red++;
green++;
blue++;
base.Update(gameTime);
}
Special note about Mr. Draw and Mrs. Update: in our
Moodlight program, they are called to do their job every
sixtieth of a second.
The three values on the table: red, green, and blue are all values
that Mr. Draw, Mrs. Update, and any other employees in the office
can change or use. As a result, these represent the global
variables/objects of our program – the variables/objects that all
METHODS can access and change!
I can
Convert numbers between binary and decimal
Use basic mathematics in a programming language.
Define what the word “assignment” means in a programming language.
17093
17093
1s10s100s1000s10000s
17093
1s10s100s1000s10000s
100101102103104
10011
1s2s4s8s
2021222324
16s
11001101
1248163264128
Add together each power of 2 that is used to create the binary number!
128 + 62 + 8 + 4 + 1 = 205
11001101 is the same as 205 in our decimal number system
CONVERT THE FOLLOWING FROM BINARY INTO DECIMAL.
1) 10111
2) 10001
3) 111
4) 1111
5) 1101
6) 1001
7) 11001
8) 11111111
CHECK YOUR ANSWERS!
1) 10111
2) 10001
3) 111
4) 1111
5) 1101
6) 1001
7) 11001
8) 11111111
23
17
7
15
13
9
25
255
93
CONVERT THE FOLLOWING FROM DECIMAL INTO BINARY.
1) 17
2) 40
3) 64
4) 29
5) 108
6) 96
7) 72
8) 9
CHECK YOUR ANSWER
1) 17
2) 40
3) 64
4) 29
5) 108
6) 96
7) 72
8) 9
10001
101000
1000000
11101
1101100
1100000
1001000
1001
https://www.youtube.com/watch?v=Dnd28lQHquU
Run the Program we created yesterday
What do you notice happening?
Do you notice the program fades from black to white and then ABRUPTLY changes to black again?
red++;
// green++;
// blue++;
What do you notice?
Remember red, blue, and green are BYTE variables something special is happening here
To understand Byte’s we need to understand binary!
Bit = 1 single “1” or “0”
Byte = holds 8 bits!
Byte – biggest number:
1111 1111
Convert this into decimal!!
1111 1111 = 255 in decimal
When goes to 256
1 0000 0000
But our byte value only looks at “8” bits
So the value returns to 0 restarts the count!
**This is why your color abruptly changes back to the beginning in your program!**
Integer:
+: 4 + 5 = 9
-: 9 – 11 = -2
*: 2*3 = 6
Double:
+: 2.1 + 3.4 = 5.5
-: 5 – 2.5 = 2.5
*: 4*3.3 = 13.2
Some math behaves exactly
as you expect!
Integer:
/: 14/4 = 3
% 14%4 = 2
(% = called modulus)
Double:
/: 12.4/4 = 3.1
Some math does not behave
as expected!!
Integer Math
=
Integer Answer
/ AND % BOTH NEEDED!
/ = # of times number goes in
% = remainder after divide
Double Math (Decimal)
=
Decimal Answer
Can be decimal answer – so modulus not needed
Integer Math:
16/5 =
16%5 =
Double Math (Decimal):
16.0/5.0 =
1) 12.5/6 =
2) 16/4 =
3) 16/5 =
4) 16%5 =
5) 25/9 =
6) 25%9 =
7) 10%4 =
8) 16.0/6 =
9) 20%7 =
10) 62/8 =
1) 12.5/6 = 2.08333
2) 16/4 = 4
3) 16/5 = 3
4) 16%5 = 1
5) 25/9 = 2
6) 25%9 = 7
7) 10%4 = 2
8) 16.0/6 = 2.66667
9) 20%7 = 6
10) 62/8 = 7
P
E
M/D
%
A/S
*If two integers, assume integer mathematics*
1) 4*(3 – 6)^2 + 4
2) 0.5*6(18%4)^3 – 7 + 8
3) 16/4*5/10*6%7
4) 5*4 – 17%5 + 20.0/5*10 – 2^2
5) 48/(4-6)^3 – 10 - 36%5*2
1. num++;
2. num--;
3. num += 3;
4. num *= 2;
5. num /= 5;
6. num -= 6;
1. num++;
2. num--;
3. num +=3;
4. num *=2;
5. num /=5;
6. num -=6;
num = num + 1
num = num – 1
num = num + 3
num = num*2
num = num/5
num = num - 6
1. num++;
2. num--;
3. num += 3;
4. num *= 2;
5. num /= 5;
6. num -= 6;
num = num + 1
num = num – 1
num = num + 3
num = num*2
num = num/5
num = num - 6
11
9
13
20
2
4
When we store a value in a variable!
Symbol: =
THIS “=“ IS NOT LIKE THE EQUALS SIGN IN MATH….IT MEANS ASSIGNMENT!!!
Used to Assign (and rewrite) variable values
The value on the right is ASSIGNED to the value on the left
Ex: int number;
number = 4; assignment
number = 4*5 – 6; reassigned
14 = number; invalid
number + 5 = 6; invalid
int num1;
int num2;
num1 = 4 + 5;
print num1;
num2 = num1;
print num2;
num1 = num1 + 1;
print num1;
num2 = num1 +
num2;
print num2;
Trace the following
int x;
int y;
int z;
x = 3*4 – 2;
y = x – 6;
z = x + y
x = z*3
y = x + z
z = x – 10
x = y – 10
At the End of this program – what are the values of x, y, and z equal to?
I can…
Evaluate boolean expressions
Use conditional statements in game design
A graphical representation of an algorithm (or portion of an algorithm)
All actions are connected with arrows to show the flow of action
3 different control structures to represent different types of “flow”
1) Sequential 2) Selection 3) Repetition
Sequential (Linear) ONE flow
**basic programs**
Selection (Decision) Split at Decisions - multiple paths
Single selection: If…Then
Double selection: If…Else
Multiple Selection:
If…ElseIf…Else
Repetition (Looping) repeat code
Game Loop:
Update
&
Draw
Repeated 60 times a second!
(We won’t code looping – automatic in game design!)
An expression that evaluates to:
TRUE or FALSE
Used to make DECISIONS in code
Based on comparisons using relationship operators (next slide)
NOTE: EVALUATE TO TRUE OR FALSE!!!
!=!=
TRUE OR FALSE:
1) x – y > 0
2) 4*y == 30
3) y/x != z
4) 22% 5 > 1
5) x*z >= y*z
6) z < 2*2 – 6
7) y*z != 5*(-4)
8) 27%7 <= 5
TRUE OR FALSE:
1) x – y > 0 False
2) 4*y == 30 False
3) y/x != z True
4) 22% 5 > 1 True
5) x*z >= y*z True
6) z < 2*2 – 6 False
7) y*z != 5*(-4) False
8) 27%7 <= 5 False
Single Selection Statements:
If…Then
Flow Chart
If (<Boolean condition>)
{
<action 1>;
….
<action n>;
}
ARE THESE VALID LINES OF CODE?!?X = 4 Y = -3 Z = 2
1) If (x*z > =5)
2) If( x + y + z )
3) If( x*y = -12)
4) If(x*y != z)
5) If( x*y > 0)
{
x = y + 4;
}
6) If (z > 0)
{
z = x*y
}
ARE THESE VALID LINES OF CODE?!?X = 4 Y = -3 Z = 2
1) If (x*z > =5)
Valid
2) If( x + y + z )
Invalid
3) If( x*y = -12)
Invalid
4) If(x*y != z)
Valid
5) If( x*y > 0) Valid
{
x = y + 4;
}
6) If (z > 0) Invalid
{
z = x*y(;)
}
Now: Double Selection Statements
If……Else
Flow Chart:
If (<Boolean condition>)
{
<action(s) when True>;
}
Else
{
<action(s) when False>;
}
When MORE than 2 possibilities
If…If Else…Else Statements
Flow Chart (complicated…)
Will check conditions until finds FIRST condition it meets – then skips remainder of conditional.
If (<first boolean condition>)
{<first action(s)>;}
else if(<second boolean condition>)
{<second action(s)>;}
else if(<third boolean condition>)
…
else
{<nth action(s)>;}
If (r > 80)
{Print “B”;}
else if(r > 70)
{Print “C”;}
else if(r > 90)
{Print “A”;}
else
{Print “Need Improvement”;}
Data
a) r = 75
b) r = 55
c) r = 94
d) r = 82
e) r = 10
Count Up
Hit 255
Count Down
Hit 0
Come up with an algorithm for how we could “repeat this” for our mood light!
Use a new type of variable to track if counting up or down!
bool redCountUp
When redIntensity reaches 255 redCountUp set to false
When redIntensity reaches 0 redCountUp set to true
When redCountUp True increase redIntensity, otherwise decrease
After define and initialize byte variables:
bool redCountUp = true;
(can declare and initialize in the same line!)
protected override void Update(GameTime gameTime)
{
if(GamePad.GetState(…….)Exit();
red++; remove this line
green++;
blue++;
if(red == 255)
{
redCountUp = false;
}
if(red == 0)
{
redCountUp = true;
}
if(redCountUp == false)
{
red++;
}
Else
{
red--;
}
base.Update(gameTime);
}
I can…
Describe the game loop and trace through code mimicking the game loop.