Computer Science Teacher

# March, 2009

• #### Pong – A Simple hands on lab for XNA Game Studio

This hands on exercise is based on a demo by Sam Stokes called Pong in Five Minutes. I appreciate Sam’s help in this effort. You can watch Sam’s demo in a video and get the code and other files used in this lab from http://blogs.msdn.com/devschool/archive/2008/02/03/pong-in-5-minutes.aspx  If you want these materials more easily available. So I have uploaded them to Skydrive at the links below.

Includes:

• Step by step instructions
• Code Snippits
• graphical content

Open a new Windows Game Project from the XNA Section for the C# language in Visual Studio 2008.

Select Content from the Solution Explorer and choose the Add Existing Item option for the Project menu.

Navigate to where you have saved the image files and add them to the project.

Set the Copy to Output Directory property of both images to “Copy Always”

Now we are going to add two classes to do the work of the game. We will add a ball class and a paddle class. First add the Ball class by selecting Add Class from the Project menu. This opens a window where we can name the class Ball.

Enter the following code for the Ball class.

```       class Ball
{
//position of ball
public Point pos;
public int h_speed, v_speed;

//constructor - position
public Ball(int x, int y)
{
pos = new Point(x, y);

Random rand = new Random();
h_speed = rand.Next(3, 7);
if (rand.Next(0, 2) == 0) h_speed *= -1;

rand = new Random();
v_speed = rand.Next(3, 7);
if (rand.Next(0, 2) == 0) v_speed *= -1;
}
}```

Now create the Paddle class the same way and enter the following code for the paddle.

```    class Paddle
{
public Point pos;
public int speed;

//constructor - position
{
pos = new Point(x, y);
speed = 3;
}
}```

Add a call to the Microsoft.XNA.Framework libraries to the top of the the Ball and Paddle class files so that these libraries will be available.

Return to the Game class and define the variables and objects we need for the game.

```   public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

Ball ball;

Random rand = new Random();
KeyboardState currentState;

Now let’s create an UpdateBall class to move the ball around the screen.

```
void UpdateBall()
{
//update positions
ball.pos.X += ball.h_speed;
ball.pos.Y += ball.v_speed;

//check for boundaries
//bottom
if (ball.pos.Y > (Window.ClientBounds.Height - 10 - t_ball.Height))
ball.v_speed *= -1;

//top
if (ball.pos.Y < 10)
ball.v_speed *= -1;
}```

```     void UpdatePaddles()
{
//get keyboard keys
currentState = Keyboard.GetState();

//check for up and down arrow keys

//paddle to move according to ball

//check boundaries

}```

In the Initialize method that was created for us automatically we have to add a call to ResetGame to start our game.

```        protected override void Initialize()
{
ResetGame();
base.Initialize();
}```

```        protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.

t_ball = Texture2D.FromFile(graphics.GraphicsDevice, @"content\ball.png");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

}```

Lastly we need to add code to the Update method to call the methods that make the game run.

```        protected override void Update(GameTime gameTime)
{
// Allows the game to exit
this.Exit();
UpdateBall();
CheckCollisions();

base.Update(gameTime);
}```

Now we can run the game and use the up and down arrow buttons to control the left paddle.

Note that you can use the Escape key to exit the program as well as the back key on an Xbox 360 Controller.

Get XNA Information at http://creators.xna.com

• #### Where are all the high school computer science students

Interesting post, actually several, at the CSTA blog lately One is called Where Are All the High School Computer Science Students- and it brings up some interesting things about CS education. I would call them misconceptions.

Computer Science is hard – Say what? Compared to what? Compared to calculus? I don’t think so. Compared to sinking a three-point shot with a six foot six defender guarding you during a basketball game? I don’t think so. Computer science does require that one actually thinks of course and perhaps we have too many students who are not used to that. But is that the problem of computer science? I don’t think so.

OK I’m having a little fun here. Computer Science is harder than some other subjects. Like, well I can’t think of a good example. I mean high school English would be pretty hard if students hadn’t already had 8 years of prior English training. Picture teaching HS English to someone who could not read or write in any language. You think that would be an easy course? And yet we do that with computer science. In some schools AP CS is the first computer course. Think about teaching AP Calc to someone who couldn’t add or subtract for a comparison.

Computer Science is boring – Again compared to what? I find it endlessly fascinating. But than I read history books for fun too and I’ve heard more than a few students call history boring. Perhaps the students who are finding computer science boring are just not paying attention? Or perhaps their teachers are working too hard at making the course boring. Perhaps the teachers would rather be teaching something else? Again, not the fault of the subject.

There are not jobs in computer science – Have you seen a list of open jobs for English majors lately? How about history majors? Math majors without computer science? Take a look at this list of top 50 occupations sometime. Two of the top 10 are computer science related. Elementary school teacher is more in demand but believe it or not the computer science jobs tend to pay more in case money is an issue for your students. :-)

So should we just wait and let students learn computer science in college? Think about that a minute. If having their first exposure in high school is “hard” wouldn’t having their first exposure in college be worse? I think so. Time to start teaching about computer science younger. Middle school is not too soon. Hopefully its not too late either.

I’ll be at SIGCSE this week and one of the things Microsoft will be showing is Kodu which is a game/tool/program to introduce some CS concepts to younger students. It will be interesting to see what discussions develop there.