Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

March, 2009

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    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  If you want these materials more easily available. So I have uploaded them to Skydrive at the links below.

    How To Create A Pong Game in XNA 


    • 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
            //position of paddle
            public Point pos;
            public int speed;
            //constructor - position
            public Paddle(int x, int y)
                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;
            Texture2D t_paddle1, t_paddle2, t_ball;
            SpriteBatch spriteBatch;
            Paddle paddle1;
            Paddle paddle2;
            Ball ball;
            Random rand = new Random();
            KeyboardState currentState;
            GamePadState currentPad;

    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
                if (ball.pos.Y > (Window.ClientBounds.Height - 10 - t_ball.Height))
                    ball.v_speed *= -1;
                if (ball.pos.Y < 10)
                    ball.v_speed *= -1;

    We also need an UpdatePaddles method to control the paddles.

         void UpdatePaddles()
                //get keyboard keys
                currentState = Keyboard.GetState();
                currentPad = GamePad.GetState(PlayerIndex.One);
                //check for up and down arrow keys
                if (currentPad.Triggers.Left > 0.0)
                       paddle1.pos.Y -= paddle1.speed;
                if (currentPad.Triggers.Right > 0.0)
                        paddle1.pos.Y += paddle1.speed;        
                //paddle to move according to ball
                if (paddle2.pos.Y + (t_paddle2.Height / 2) > ball.pos.Y)
                    paddle2.pos.Y -= paddle2.speed;
                else if (paddle2.pos.Y + (t_paddle2.Height / 2) < ball.pos.Y)
                    paddle2.pos.Y += paddle2.speed;
                //check boundaries
                if (paddle1.pos.Y <= 10) paddle1.pos.Y = 10;
                if (paddle2.pos.Y <= 10) paddle2.pos.Y = 10;
                if (paddle1.pos.Y + t_paddle1.Height >= Window.ClientBounds.Height - 10) paddle1.pos.Y = Window.ClientBounds.Height - t_paddle1.Height - 10;
                if (paddle2.pos.Y + t_paddle2.Height >= Window.ClientBounds.Height - 10) paddle2.pos.Y = Window.ClientBounds.Height - t_paddle2.Height - 10;

    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()
                // TODO: Add your initialization logic here

    In the LoadContent method we have to add some calls to load the images that we have already added to the project.

            protected override void LoadContent()
                // Create a new SpriteBatch, which can be used to draw textures.
                t_paddle1 = Texture2D.FromFile(graphics.GraphicsDevice, @"content\paddle.png");
                t_paddle2 = Texture2D.FromFile(graphics.GraphicsDevice, @"content\paddle.png");
                t_ball = Texture2D.FromFile(graphics.GraphicsDevice, @"content\ball.png");
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
                // TODO: use this.Content to load your game content here

    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
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    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

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    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.

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Ada Lovelace Day – Women in Technology


    This is my Ada Lovelace Day post as promised in January. Today well over a thousand bloggers are posting posts about women in technology who they admire. The goal is the present role models to young women and girls as well as to highlight and honor the fact that technology is not just a man’s thing. I thought about a good many women to write about. Grace Hopper of course but I’ve written about her several times. Chris Stephenson who is the Executive Director of the Computer Science Teachers Association was another who came to mind. A number of computer science teacher came to mind as well. Several of those are listed in the list of CS teacher blogs on the side of this blog. Several textbook authors came to mind - Beth Brown who writes for Lawrenceville Press, Diane Zak who writes for Cengage Course Technology, Maria Litvin who writes for her own company, and of course the prolific Nell Dale. And there are a couple of women I work with in my current job – Diane Curtis and Hilary Pike – who I have great respect for. Just an awful lot of choices.

    But in the end I decided to write about Thelma Thompson – my wife.  The very first gift I ever gave her was a stack of punch cards – with pink trim. Geeky isn’t it? She still married me. She was taking a programming course at the time and we were not even going steadily yet. Later after we were married she spent some time programming bank teller machines. In machine language. She actually had to break bits off of little plastic “combs” that fit into a rotary drum in order to program these devices. Imagine examining a comb to debug your code? I’m not sure if she is a good debugger because her programs always seemed to work the first time out. She taught me the value of careful planning and desk checking of code before trying it out. And of documentation!

    She spent part of her career writing documentation and training users in some of the earliest person computers back when a “personal computer” went for around $10,000 and small businesses were first starting to see that they could use computers. She has never been afraid of new technology but always jumps at the chance to learn new things and use them to solve both old and new problems. Yes she was and is a bit of a pioneer.

    Today she teaches in a middle school and has taught in both elementary and high schools. She teaches with technology and she teaches how to use technology. She uses tools like Alice to introduce middle school students to programming and computer science concepts. She teaches students how to really use Internet search engines intelligently. She introduces more than the default in computer applications like Word and PowerPoint. And she models good practice in the things she does and the ways she uses technology. Besides teaching students she also teaches teachers and administrators how to use technology in their own practice – leading by example. She’s still a pioneer.

    Lots of people point to the Fran Allen's (first woman Turing winner), the Grace Hoppers and the Ada Lovelace's of computer and technology history as heroes. And heroes they are. But the day to day heroes of today are teachers who insure that the heroes of the future learn about the creations of the heroes of the past. Thank you to all of you who are teaching students today!

Page 1 of 7 (20 items) 12345»