Pong

Susan Gold
Susan Goldeducators, artist, activisit em Susan Gold
Pong
One of the first games. Tennis for Two and Space War were earlier, but they didn't have a score.
This is the first game and where possible, simplifications will be made. The goal is to develop game
construction principles that can be scaled. Rather than jump to what is the best solution, the code starts
off simple and will be improved on.
Focus on controls, core mechanic, playfield, design, and programming.

Objectives
   ● developing a plan: features, iterations, refactoring, scaling
   ● setting up a 3D camera
   ● drawing the court, paddles, ball in 3D
   ● setting up a 2D camera and drawing the heads up display in 2D
   ● handling player input: keyboard and joystick
   ● collisions with the playfield, reflections
   ● collisions with paddles, point versus rectangle
   ● dynamically created objects, pointers, clean coding
   ● game controller class follows the singleton pattern
   ● recording and playing sounds

Introduction
These are the notes that go along with the presentation.
There are lots of game development concepts to learn from even the simplest game. And pong is a
simple game. This development doesn't attempt to re-create a fun version of pong.

Development Plan: 1000 Features
The agile practice of creating a list of features you want in your game seems to work best. Write out a
list and give each feature an unique rank from 1000 to 0 -- 1000 being the most important to your
game. Generally devote more time and energy to the higher ranking features.
The feature list should include only things that can be seen, heard, felt, or otherwise tested by playing
the game and not include work under the hood.
       basic court, ball, paddles are drawn
   •
       ball collisions with the walls
   •
       input from keyboard
   •
       input from joysticks
   •
       ball collisions with the player paddles
   •
score displayed
   •
       game waits for start button, plays, ends
   •
       ball speeds up, English on the ball
   •

This list is meant to get the big things out on the table and also give you ideas for a development plan.

Development Plan: Iteration
Take a step back and think about the development process and your goals.
Game development lends itself well to an iterative approach. Build something small and work to
improve it in steps. Consistent forward progress depends on choosing the size and direction of your
iterative steps.
Games are built by combining a number of individual technologies. The set of technologies you must
pull together depend on the game, the engine, and the development environment.
Iteration steps should be small and able to be completed in one sitting. In the beginning of a project,
find steps that result in visual changes. Start by getting the main game objects drawn.
       a hello world window
   •
       overhead camera and drawing the court
   •
       draw the ball
   •
       draw the paddles
   •

Next, do one step with all the different tech your game needs. The goal is to write code that can be
improved on. The first part of a project focuses on a kind of breadth-first development. Keep your
steps small -- aim for something you will complete in one sitting.
       player input and connect it to the paddles
   •
       collisions of the ball with the court
   •
       collisions of the ball with the player paddles
   •
       playing of a sound
   •
       2D heads up display of score
   •

It's important not to wait too long before tackling this next stage. Setup your game to run in an attract
mode loop, play the game, and exit without any memory leaks.
       write a game controller that manages state transitions including the following
   •
       display a title screen
   •
       wait for start button to start the game
   •
       start and play the game until someone wins
   •
       display the game over screen
   •
       testing for clean, balanced coding
   •
       loop until the player exits
   •
       exit without any memory leaks
   •

After the first pass of your game elements and technology is completed, you will likely have some
messy code. Now is a good time to look at the overall structure of your game and decide if you can
improve it. Also, look at each system, improving the names usually leads to better code.
improve the structure
     •
         improve the names, define and follow a coherent approach
     •

Now, focus on the game itself. Iterate on the elements that stick out the most. Eventually, your focus
will change and instead of fixing things that are problems, you'll be improving things and looking for
ways to make things more interesting.
         find the worst thing, iterate on it until it's not the worst thing
     •
         find things that you can make better
     •

Realize that you will never run out of things to work on, but you will run out of time.

Developing a Plan: Refactoring
Plan on refactoring your code from the start. Rather than work to write perfect code, write good code
that can be refactored. This means that you should write code that with distinct names. Set yourself up
to be able to search and rename sections of code. Also, write code that groups a feature together.
Refactor the sections when they get too big or the logic starts getting tangled or they clearly belong in
their own function.
It seems that most code is refactored two or three times during a project. However, don't be over
anxious to refactor code, you should wait to refactor until the old code becomes unbearable.

The Game Loop
The beginning of the game controller class. Independent update and draw. Keep features orthogonal.

Class pointers and references
Pointers are an important part of game programming. Get used to using them.

Common Identifier Space
Start all of your identifiers at one. Reserve zero for defaults and uninitialized tests. Define one set of
identifiers. Use these for game ids, return codes, other uses.
enum {
   // int, unique ids for any use in the game
   // rather than have ids all over the place
   GAMEID_TMP=1, // reserved
   GAMEID_GAME, // game instance

     // game modes
     GAMEID_MODE_ATTRACT,
     GAMEID_MODE_GAME,

     // return codes
     GAMEID_BALL_REFLECT,
     GAMEID_BALL_OFFLEFT,
     GAMEID_BALL_OFFRIGHT,
};
Function construction
Initialize. Body. Finalize and exit. One return.
Initialization section must initialize all the objects. Design your objects so zeroing them initializes
them.
Return negative values for errors.
int function()
{
   int r;

    /////////////
    // initialize

    ///////
    // body

   r=0; // normal exit code, success
BAIL:
   /////////////////////
   // finalize, clean up

   return r;
} // function()

Free what you allocate
Clean up after yourself. Write your final code so that it is path independent. If non-zero, free and zero.

Public Classes
Projects go through a few phases. In the beginning, setup your code for flexibility. The beginning of a
project is marked by changes in direction and structure. One way to facilitate this is with classes with
all the elements marked public. Once you understand the problem space and your solution, you will
refactor your code to move fields into the private or protected. Until then, it will only slow you down.
Protecting fields becomes important when you scale your project up and add programmers. Be sure to
set aside time for this step.
Feature based accessors, not straight-to-vars. Peter Bennett.

Allocating and Freeing Objects
In C, memory allocated and freed with the malloc() and free() functions and objects must be setup by
the programmer. In C++, the new operator calls malloc() to allocate memory and calls the constructor
function to setup the object. Likewise, the delete operator calls the destructor function to cleanup the
object and then calls free() to release the memory.
Memory is one of your primary resources. Game objects are defined by classes and take up memory
when they are created.
QE Base Classes
Derive from qe base classes so all the allocations run through the engine. Objects derived from qe base
classes zero their memory when allocated.
Derive from qeUpdateBase for objects that are managed by the game engine. Normally, you will
override the update() and draw() functions with your own code.
Derive from qe for simple, non-managed objects.

Bracketing Resources
Bullet-proof your resource allocation and freeing. Build a structure that guarantees that you free all the
resources you allocate. This is accomplished with a 3-part structure: initialization, use, and
finalization.
The initialization is code that is guaranteed to run early in the game, and finalization code is guaranteed
to run before you exit. Initialization code should simply zero all resource fields. The finalization code,
which is also guaranteed to run, checks resource fields, frees any non-zero resource, and then zeros that
field. There must be only one owner of each resource and one initialization and finalization section of
code for each resource field.
The code that runs in-between the initialization and finalization must follow the simple rules to test the
resource field before each use and set the field if the resource is successfully created. This structure
allows you to bracket your resource usage and provides the necessary structure to write code that
guarantees balanced resource usage.
       initialization (guaranteed): zeros
   •
       finalization (guaranteed): tests, frees & zeros
   •
       usage: tests, allocates & sets, uses
   •

Game Controller Class
This class manages the game.
Load all the initial content including images and sounds.
Modes should be avoided because they promote structural discontinuity and ultimately complicate your
program greatly. However, modes do make prototyping easier and novice programmers should use
modes until a modeless structure resolves itself.
Start off in attract mode. When the start button is pressed, drop into game mode.
Attract mode waits for the start button and then drops into game mode. Display the last scores.
Game mode runs the game until the game is over. Then drops back into attract mode.

Matrices
A matrix is a set of numbers that define any combination of movements, rotations, and scaling of
objects in a 3D world. The matrices used here are 12 element matrices. The first 3 entries are the X,
Y, and Z positions. The next 9 entries define a simple rotation and scaling matrix. Taken together, the
12 elements are all you need for most game operations and qe provides a set of functions to operate on
the raw 12 element array of floats.
Simple 3D Camera
For now, it's best to use the code and experiment with it as you go.
Cameras are a software metaphor implemented with matrices. The camera matrix transforms objects
from world space into camera space. In other words, it treats the camera as the center of the world, and
moves and rotates everything in the world in front of the camera. In OpenGL, this matrix is called the
MODELVIEW matrix.
There are two matrices in OpenGL. You can think of the MODELVIEW matrix as the way you
position the camera, and the PROJECTION matrix as the camera's lens.
OpenGL's PROJECTION matrix transforms objects from camera space into clip coordinates. The
perspective division (divide by z) then transforms clip coordinates into normalized device coordinates.
These coordinates are then mapped through the viewport into a window.

BRK(), Code to Continue
This macro expands to into an interrupt call that breaks the program execution and 'wakes up' the
debugger at that point. If the debugger is not running, the macro has no effect. Write your code to
detect errors and continue running. Players don't want to see a dialog box asking them if they want to
send an error report to Microsoft. Even in extreme cases, do your best to continue running.
That said, you must detect and handle all errors. When you run into an error, report it, and write code
to do the best it can to continue running -- this may mean an object is drawn without a texture or may
even be missing.

Drawing The Court
The court is managed by the world class. A solid rectangle is drawn for the court.

// JFL 14 Aug 07
// JFL 15 Mar 09
int World::draw(void)
{
   glColor3f(0,0,1); // set color to blue
   glPolygonMode(GL_FRONT,GL_FILL); // draw filled polygons
   glBegin(GL_QUADS); // draw quads counter-clockwise from camera's view
      glVertex3f(this->xyzMin[0],this->xyzMin[1],this->xyzMin[2]);
      glVertex3f(this->xyzMin[0],this->xyzMin[1],this->xyzMax[2]);
      glVertex3f(this->xyzMax[0],this->xyzMin[1],this->xyzMax[2]);
      glVertex3f(this->xyzMax[0],this->xyzMin[1],this->xyzMin[2]);
   glEnd();
   return 0;
} // World::draw()

Reset Flags Once
Flags can be used to signal a request from many possible places. Handle the flag in one place. Test the
flag, clear it, call the handler. Clear the flag in only one section of code.
000ZY Coordinates
This choice is largely arbitrary, but should be something you're comfortable with. One unit = one foot
is the standard I use. This depends on the scale of your game, but should be determined before you
start coding.

Velocities
Variable frame rate systems must multiply the velocity by the amount of time elapsed since the last
loop. Keep track of the time since the last update.
   float t;

   // find time since last update
   t=this->timeOfLastUpdate;
   this->timeOfLastUpdate=qeTimeFrame();
   t=this->timeOfLastUpdate-t; // delta

   // xyz += vel*t
   this->xyz[0]+=this->vel[0]*t;
   this->xyz[1]+=this->vel[1]*t;
   this->xyz[2]+=this->vel[2]*t;

This update method -- moving by adding the velocity times the elapsed time -- is called Euler
integration. The timeOfLastUpdate variable must be reset when the object is reset.

3D World, Camera
The world is quot;in 3Dquot;. However, the camera is fixed above it and looking down. This gives the
appearance of a 2D game.
   //////////////////////
   // Camera class fields
   float fovyHalfRad; // field of view angle in y direction in radians
   float nearClip; // near clipping plane
   float farClip; // far clipping plane
   float winWidth; // in pixels
   float winHeight; // in pixels
   float winWDivH; // window aspect ratio
   float nearHeight; // height of window at near plane
   float mat12[12]; // camera matrix

   ///////////////////////
   // Camera setup -- once
   this->nearClip = 1;
   this->farClip = 500;
   this->fovyHalfRad = 0.5*((63*PI)/180.0); // 0.5*(degrees->radians)
   this->nearHeight = this->nearClip * MathTanf(this->fovyHalfRad);

   // camera matrix transforms from world space into camera space
   SET3(pos,0,CAMERA_Y,0); // position of camera
   SET3(at,0,0,0); // where camera is looking at
   SET3(up,0,0,-1); // the camera's up direction
   qeCamLookAtM12f(this->mat12,pos,at,up); // compute camera mat
/////////////////////////////////////////////////////
   // Camera matrices -- before you draw with the camera
   if(qeGetWindowSize(&this->winWidth,&this->winHeight)<0)
      bret(-2);
   this->winWDivH=this->winWidth/this->winHeight;

   // set the PROJECTION matrix (the camera lens)
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   float yy = this->nearHeight,xx=this->nearHeight*this->winWDivH;
   glFrustum(-xx,xx,-yy,yy,this->nearClip,this->farClip);

   // set the MODELVIEW matrix (position and orientation of the camera)
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   qeGLM12f(this->mat12); // set matrix

2D Gameplay
The keyboard and joystick are used for the player's up and down.

Keyboard as Buttons Input
By default, the keyboard acts like a set of buttons to the game engine. QE uses button counts which are
incremented when the buttons are pressed and released. When the button is down, the count is odd,
and when the button is up, the count is even. The bottom bit is set for odd numbers and can easily be
tested with a bitwise test.
       // up-down-left-right arrow mapping
       if(1&qeInpButton(QEINPBUTTON_UP))
          /* up button is pressed */;
       else if(1&qeInpButton(QEINPBUTTON_DOWN))
          /* down button is pressed */;

Joystick Input
Joystick positions are returned as floating point values. Some care should be taken to drop out the
values when the joystick is near the center.
   // get player 0 joystick value -1..0..1
   stick=qeInpJoyAxisf(0,QEJOYAXIS_LEFT_Y);

#define STICK_DEADZONE 0.2

   // enforce stick deadzone & re-normalize
   if(stick>STICK_DEADZONE)
      stick=(stick-STICK_DEADZONE)/(1.0-STICK_DEADZONE);
   else if(stick<-STICK_DEADZONE)
      stick=(stick+STICK_DEADZONE)/(1.0-STICK_DEADZONE);
   else // in deadzone, zero
      stick=0;

Sounds
Record sounds into .wav files. Most sound effects should be recorded in mono at a low resolution.
Trigger the sounds in the game.
// setup sound quot;bumpquot; on channel 1
    if((r=qeSndNew(quot;bumpquot;,M_SNDNEW_CH_1,0,quot;art/sounds/pongbump.wavquot;))<0)
       BRK();

    // trigger the sound
    qeSndPlay(quot;bumpquot;);

Use Restart Functions
Use restart rather than start functions. Names matter. Start functions usually hide assumptions
regarding the state of the game.

Drawing 2D: Camera and Image
Use the 32 bit image of the digits. The image has been created so there are 8 digits on the first row and
2 on the second row. 0 1 2 3 4 5 6 7 and 8 9.
    // register image
    if(qeImgNew(quot;icons1quot;,0,quot;art/images/icons1.tgaquot;)<0)
       BRK(); // make sure path is correct

    qeTexBind(quot;icons1quot;);

2D images are drawn the same way other polygons are drawn. However, the camera and draw modes
have to be setup correctly.
    qefnSysCamFlat(); // ortho: (0,0) top left (1,1) bottom right

    // setup texture mode to blend
    glPolygonMode(GL_FRONT,GL_FILL);
    glEnable(GL_TEXTURE_2D);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_ADD);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

Drawing 2D: UVs and Vertices
As always, vertex coordinates are specified in the space defined by the camera matrices and define the
structure of the object. UV coordinates define how the image maps from texture space to the polygon.
UVs are specified in normalized image space.
Ways to get the images to stretch or be drawn close to one to one from the image.
To find UVs, start with the image coordinates and divide by the image size.

        // find image coordinates for a positive integer n
        // icons in our image are 32x32 and there are 8 across horizontally
        // these coordinates are in pixel coordinates of the image
        // top left of the image 0,0
        u0=(n&7)*GAME_ICON_WIDTH;
        v0=(n>>3)*GAME_ICON_HEIGHT;

        // map image coordinates into uv space
        u0/=imgWidth;
        v0/=imgHeight;
Name Your Objects
Debugging is easier when object have string names. When debugging, checking the names of objects
is a good way to verify system and game integrity. Also, you can sometimes trap on on the name.

Recomendados

NDC 2011 영웅전 런칭팀 박영준 por
NDC 2011 영웅전 런칭팀 박영준NDC 2011 영웅전 런칭팀 박영준
NDC 2011 영웅전 런칭팀 박영준영준 박
2.6K visualizações87 slides
Tic tac toe c++ programing por
Tic tac toe c++ programingTic tac toe c++ programing
Tic tac toe c++ programingKrishna Agarwal
3.1K visualizações29 slides
Game project Final presentation por
Game project Final presentationGame project Final presentation
Game project Final presentationgemmalunney
14.4K visualizações20 slides
Car racing game for android por
Car racing game for androidCar racing game for android
Car racing game for androidravijot singh
2.8K visualizações25 slides
game project presentation por
game project presentationgame project presentation
game project presentationKavi Kumar
1.2K visualizações16 slides
First-person Shooters por
First-person ShootersFirst-person Shooters
First-person ShootersMinh Múi Mít
2K visualizações39 slides

Mais conteúdo relacionado

Mais procurados

Study material ip class 12th por
Study material ip class 12thStudy material ip class 12th
Study material ip class 12thanimesh dwivedi
168 visualizações123 slides
BGA Studio - Focus on BGA Game state machine por
BGA Studio - Focus on BGA Game state machine BGA Studio - Focus on BGA Game state machine
BGA Studio - Focus on BGA Game state machine Board Game Arena
16.2K visualizações23 slides
16.03.24 sos project 컨셉 기획서_남진우 por
16.03.24 sos project 컨셉 기획서_남진우16.03.24 sos project 컨셉 기획서_남진우
16.03.24 sos project 컨셉 기획서_남진우Nam Jinwoo
2K visualizações45 slides
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부 por
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부Eunseok Yi
8K visualizações97 slides
Design phase of game development of unity 2d game por
Design phase of game development of unity 2d game Design phase of game development of unity 2d game
Design phase of game development of unity 2d game Muhammad Maaz Irfan
1.4K visualizações19 slides
Level design in 11 points por
Level design in 11 pointsLevel design in 11 points
Level design in 11 points용태 이
4.1K visualizações22 slides

Mais procurados(20)

Study material ip class 12th por animesh dwivedi
Study material ip class 12thStudy material ip class 12th
Study material ip class 12th
animesh dwivedi168 visualizações
BGA Studio - Focus on BGA Game state machine por Board Game Arena
BGA Studio - Focus on BGA Game state machine BGA Studio - Focus on BGA Game state machine
BGA Studio - Focus on BGA Game state machine
Board Game Arena16.2K visualizações
16.03.24 sos project 컨셉 기획서_남진우 por Nam Jinwoo
16.03.24 sos project 컨셉 기획서_남진우16.03.24 sos project 컨셉 기획서_남진우
16.03.24 sos project 컨셉 기획서_남진우
Nam Jinwoo2K visualizações
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부 por Eunseok Yi
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
Eunseok Yi8K visualizações
Design phase of game development of unity 2d game por Muhammad Maaz Irfan
Design phase of game development of unity 2d game Design phase of game development of unity 2d game
Design phase of game development of unity 2d game
Muhammad Maaz Irfan1.4K visualizações
Level design in 11 points por 용태 이
Level design in 11 pointsLevel design in 11 points
Level design in 11 points
용태 이4.1K visualizações
FPS GAME FYP Documentation por Danial Ahmed
FPS GAME FYP DocumentationFPS GAME FYP Documentation
FPS GAME FYP Documentation
Danial Ahmed1.1K visualizações
1casestudy por Samuel-Schoettner
1casestudy1casestudy
1casestudy
Samuel-Schoettner1.4K visualizações
Blockchain in gaming industry por Celine George
Blockchain in gaming industryBlockchain in gaming industry
Blockchain in gaming industry
Celine George319 visualizações
Algorithm and flowchart por Shivam Sharma
Algorithm and flowchart Algorithm and flowchart
Algorithm and flowchart
Shivam Sharma171 visualizações
[BoardgameVN] Luật chơi Taluva por BoardgameVN
[BoardgameVN] Luật chơi Taluva[BoardgameVN] Luật chơi Taluva
[BoardgameVN] Luật chơi Taluva
BoardgameVN4.9K visualizações
[IGC 2016] 실전시나리오라이팅 - PD가 원하면 나는 쓴다 por Hwang Sang Hun
[IGC 2016] 실전시나리오라이팅 - PD가 원하면 나는 쓴다[IGC 2016] 실전시나리오라이팅 - PD가 원하면 나는 쓴다
[IGC 2016] 실전시나리오라이팅 - PD가 원하면 나는 쓴다
Hwang Sang Hun1.8K visualizações
The 8 steps to create a board game on Board Game Arena por Board Game Arena
The 8 steps to create a board game on Board Game ArenaThe 8 steps to create a board game on Board Game Arena
The 8 steps to create a board game on Board Game Arena
Board Game Arena33.1K visualizações
The Four Lenses of Game Making and Social Games por Tadhg Kelly
The Four Lenses of Game Making and Social GamesThe Four Lenses of Game Making and Social Games
The Four Lenses of Game Making and Social Games
Tadhg Kelly1.9K visualizações
Hướng dẫn luật chơi 7 Wonders Duel por Board Game Việt
Hướng dẫn luật chơi 7 Wonders DuelHướng dẫn luật chơi 7 Wonders Duel
Hướng dẫn luật chơi 7 Wonders Duel
Board Game Việt10.2K visualizações
Final Year Game Project Presentation por Nusrat Jahan Shanta
Final Year Game Project Presentation Final Year Game Project Presentation
Final Year Game Project Presentation
Nusrat Jahan Shanta5.8K visualizações
Hướng dẫn luật chơi board game Catan Seafarers por Board Game Việt
Hướng dẫn luật chơi board game Catan Seafarers Hướng dẫn luật chơi board game Catan Seafarers
Hướng dẫn luật chơi board game Catan Seafarers
Board Game Việt8.6K visualizações
Project presentation FPS por Shubham Rajput
Project presentation FPSProject presentation FPS
Project presentation FPS
Shubham Rajput4.3K visualizações
SRS REPORT ON A ANDROID GAME por milan tripathi
SRS REPORT ON A ANDROID GAMESRS REPORT ON A ANDROID GAME
SRS REPORT ON A ANDROID GAME
milan tripathi4.4K visualizações

Destaque

Making A Game Engine Is Easier Than You Think por
Making A Game Engine Is Easier Than You ThinkMaking A Game Engine Is Easier Than You Think
Making A Game Engine Is Easier Than You ThinkGorm Lai
4.4K visualizações51 slides
Global Game Jam @ Develop por
Global Game Jam @ DevelopGlobal Game Jam @ Develop
Global Game Jam @ DevelopGorm Lai
741 visualizações29 slides
Gdc09 Minipong por
Gdc09 MinipongGdc09 Minipong
Gdc09 MinipongSusan Gold
359 visualizações14 slides
SIGGRAPH 2007 IGDA Presentation por
SIGGRAPH 2007 IGDA PresentationSIGGRAPH 2007 IGDA Presentation
SIGGRAPH 2007 IGDA PresentationSusan Gold
398 visualizações44 slides
Gdc09 Minigames por
Gdc09 MinigamesGdc09 Minigames
Gdc09 MinigamesSusan Gold
493 visualizações45 slides
Global Game Jam @ Develop 09 por
Global Game Jam @ Develop 09Global Game Jam @ Develop 09
Global Game Jam @ Develop 09Gorm Lai
543 visualizações29 slides

Destaque(9)

Making A Game Engine Is Easier Than You Think por Gorm Lai
Making A Game Engine Is Easier Than You ThinkMaking A Game Engine Is Easier Than You Think
Making A Game Engine Is Easier Than You Think
Gorm Lai4.4K visualizações
Global Game Jam @ Develop por Gorm Lai
Global Game Jam @ DevelopGlobal Game Jam @ Develop
Global Game Jam @ Develop
Gorm Lai741 visualizações
Gdc09 Minipong por Susan Gold
Gdc09 MinipongGdc09 Minipong
Gdc09 Minipong
Susan Gold359 visualizações
SIGGRAPH 2007 IGDA Presentation por Susan Gold
SIGGRAPH 2007 IGDA PresentationSIGGRAPH 2007 IGDA Presentation
SIGGRAPH 2007 IGDA Presentation
Susan Gold398 visualizações
Gdc09 Minigames por Susan Gold
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
Susan Gold493 visualizações
Global Game Jam @ Develop 09 por Gorm Lai
Global Game Jam @ Develop 09Global Game Jam @ Develop 09
Global Game Jam @ Develop 09
Gorm Lai543 visualizações
Game Jams & Hobbyists por Gorm Lai
Game Jams & HobbyistsGame Jams & Hobbyists
Game Jams & Hobbyists
Gorm Lai1.7K visualizações
Community por Susan Gold
CommunityCommunity
Community
Susan Gold541 visualizações
Gold And Robinson 2009 por Susan Gold
Gold And Robinson 2009Gold And Robinson 2009
Gold And Robinson 2009
Susan Gold393 visualizações

Similar a Pong

FAST - Department of Computing MODULE CSYM028 por
FAST - Department of Computing   MODULE  CSYM028       FAST - Department of Computing   MODULE  CSYM028
FAST - Department of Computing MODULE CSYM028 ChereCheek752
2 visualizações26 slides
Game Development Session - 3 | Introduction to Unity por
Game Development Session - 3 | Introduction to  UnityGame Development Session - 3 | Introduction to  Unity
Game Development Session - 3 | Introduction to UnityKoderunners
258 visualizações33 slides
Game programming workshop por
Game programming workshopGame programming workshop
Game programming workshopnarigadu
1.7K visualizações132 slides
LAFS Game Design 7 - Prototyping por
LAFS Game Design 7 - PrototypingLAFS Game Design 7 - Prototyping
LAFS Game Design 7 - PrototypingDavid Mullich
2.9K visualizações49 slides
Making a game "Just Right" through testing and play balancing por
Making a game "Just Right" through testing and play balancingMaking a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancingJulio Gorgé
1.1K visualizações46 slides
Easy coding a multi device game with FireMonkey por
Easy coding a multi device game with FireMonkeyEasy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkeypprem
48 visualizações33 slides

Similar a Pong(20)

FAST - Department of Computing MODULE CSYM028 por ChereCheek752
FAST - Department of Computing   MODULE  CSYM028       FAST - Department of Computing   MODULE  CSYM028
FAST - Department of Computing MODULE CSYM028
ChereCheek7522 visualizações
Game Development Session - 3 | Introduction to Unity por Koderunners
Game Development Session - 3 | Introduction to  UnityGame Development Session - 3 | Introduction to  Unity
Game Development Session - 3 | Introduction to Unity
Koderunners 258 visualizações
Game programming workshop por narigadu
Game programming workshopGame programming workshop
Game programming workshop
narigadu1.7K visualizações
LAFS Game Design 7 - Prototyping por David Mullich
LAFS Game Design 7 - PrototypingLAFS Game Design 7 - Prototyping
LAFS Game Design 7 - Prototyping
David Mullich2.9K visualizações
Making a game "Just Right" through testing and play balancing por Julio Gorgé
Making a game "Just Right" through testing and play balancingMaking a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancing
Julio Gorgé1.1K visualizações
Easy coding a multi device game with FireMonkey por pprem
Easy coding a multi device game with FireMonkeyEasy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkey
pprem48 visualizações
Cerny method por Tim Holt
Cerny methodCerny method
Cerny method
Tim Holt36.9K visualizações
Rockstar Games por Rae Clarke
Rockstar GamesRockstar Games
Rockstar Games
Rae Clarke5.2K visualizações
DevLearn 2017 Play to Learn workshop slides por Sharon Boller
DevLearn 2017 Play to Learn workshop slidesDevLearn 2017 Play to Learn workshop slides
DevLearn 2017 Play to Learn workshop slides
Sharon Boller420 visualizações
The complete srs documentation of our developed game. por Isfand yar Khan
The complete srs documentation of our developed game. The complete srs documentation of our developed game.
The complete srs documentation of our developed game.
Isfand yar Khan26.8K visualizações
Quality Assurance 1: Why Quality Matters por Marc Miquel
Quality Assurance 1: Why Quality MattersQuality Assurance 1: Why Quality Matters
Quality Assurance 1: Why Quality Matters
Marc Miquel329 visualizações
Lecture 2: C# Programming for VR application in Unity por Kobkrit Viriyayudhakorn
Lecture 2: C# Programming for VR application in UnityLecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in Unity
Kobkrit Viriyayudhakorn836 visualizações
Unreal Engine Basics 01 - Game Framework por Nick Pruehs
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game Framework
Nick Pruehs741 visualizações
Game Design Document - Step by Step Guide por DevBatch Inc.
Game Design Document - Step by Step GuideGame Design Document - Step by Step Guide
Game Design Document - Step by Step Guide
DevBatch Inc.4K visualizações
Galactic Wars XNA Game por Sohil Gupta
Galactic Wars XNA GameGalactic Wars XNA Game
Galactic Wars XNA Game
Sohil Gupta921 visualizações
Unity Google VR Cardboard Deployment on iOS and Android por Kobkrit Viriyayudhakorn
Unity Google VR Cardboard Deployment on iOS and AndroidUnity Google VR Cardboard Deployment on iOS and Android
Unity Google VR Cardboard Deployment on iOS and Android
Kobkrit Viriyayudhakorn985 visualizações
DSC RNGPIT - Getting Started with Game Development Day 1 por DeepMevada1
DSC RNGPIT - Getting Started with Game Development Day 1DSC RNGPIT - Getting Started with Game Development Day 1
DSC RNGPIT - Getting Started with Game Development Day 1
DeepMevada198 visualizações

Mais de Susan Gold

ICEC por
ICECICEC
ICECSusan Gold
570 visualizações30 slides
Dgxpo por
DgxpoDgxpo
DgxpoSusan Gold
606 visualizações32 slides
Fog por
FogFog
FogSusan Gold
547 visualizações17 slides
Ivdc por
IvdcIvdc
IvdcSusan Gold
583 visualizações13 slides
GDC 2009 Game Design Improv por
GDC 2009 Game Design ImprovGDC 2009 Game Design Improv
GDC 2009 Game Design ImprovSusan Gold
820 visualizações40 slides
Gdc09 Minimissile por
Gdc09 MinimissileGdc09 Minimissile
Gdc09 MinimissileSusan Gold
330 visualizações11 slides

Mais de Susan Gold(18)

ICEC por Susan Gold
ICECICEC
ICEC
Susan Gold570 visualizações
Dgxpo por Susan Gold
DgxpoDgxpo
Dgxpo
Susan Gold606 visualizações
Fog por Susan Gold
FogFog
Fog
Susan Gold547 visualizações
Ivdc por Susan Gold
IvdcIvdc
Ivdc
Susan Gold583 visualizações
GDC 2009 Game Design Improv por Susan Gold
GDC 2009 Game Design ImprovGDC 2009 Game Design Improv
GDC 2009 Game Design Improv
Susan Gold820 visualizações
Gdc09 Minimissile por Susan Gold
Gdc09 MinimissileGdc09 Minimissile
Gdc09 Minimissile
Susan Gold330 visualizações
Setup por Susan Gold
SetupSetup
Setup
Susan Gold361 visualizações
Qe Reference por Susan Gold
Qe ReferenceQe Reference
Qe Reference
Susan Gold365 visualizações
Missilecommand por Susan Gold
MissilecommandMissilecommand
Missilecommand
Susan Gold281 visualizações
Assignment Pong por Susan Gold
Assignment PongAssignment Pong
Assignment Pong
Susan Gold341 visualizações
Global Game Jam Overview por Susan Gold
Global Game Jam OverviewGlobal Game Jam Overview
Global Game Jam Overview
Susan Gold901 visualizações
Agd Talk Speed Run por Susan Gold
Agd Talk   Speed RunAgd Talk   Speed Run
Agd Talk Speed Run
Susan Gold747 visualizações
Mscruise por Susan Gold
MscruiseMscruise
Mscruise
Susan Gold683 visualizações
GDC China 2007 por Susan Gold
GDC China 2007GDC China 2007
GDC China 2007
Susan Gold450 visualizações
Digra07 por Susan Gold
Digra07Digra07
Digra07
Susan Gold530 visualizações
Boston IGDA Meeting por Susan Gold
Boston IGDA MeetingBoston IGDA Meeting
Boston IGDA Meeting
Susan Gold387 visualizações
Curriculumframework2008 por Susan Gold
Curriculumframework2008Curriculumframework2008
Curriculumframework2008
Susan Gold605 visualizações
Game Program Highlights por Susan Gold
Game Program HighlightsGame Program Highlights
Game Program Highlights
Susan Gold514 visualizações

Pong

  • 1. Pong One of the first games. Tennis for Two and Space War were earlier, but they didn't have a score. This is the first game and where possible, simplifications will be made. The goal is to develop game construction principles that can be scaled. Rather than jump to what is the best solution, the code starts off simple and will be improved on. Focus on controls, core mechanic, playfield, design, and programming. Objectives ● developing a plan: features, iterations, refactoring, scaling ● setting up a 3D camera ● drawing the court, paddles, ball in 3D ● setting up a 2D camera and drawing the heads up display in 2D ● handling player input: keyboard and joystick ● collisions with the playfield, reflections ● collisions with paddles, point versus rectangle ● dynamically created objects, pointers, clean coding ● game controller class follows the singleton pattern ● recording and playing sounds Introduction These are the notes that go along with the presentation. There are lots of game development concepts to learn from even the simplest game. And pong is a simple game. This development doesn't attempt to re-create a fun version of pong. Development Plan: 1000 Features The agile practice of creating a list of features you want in your game seems to work best. Write out a list and give each feature an unique rank from 1000 to 0 -- 1000 being the most important to your game. Generally devote more time and energy to the higher ranking features. The feature list should include only things that can be seen, heard, felt, or otherwise tested by playing the game and not include work under the hood. basic court, ball, paddles are drawn • ball collisions with the walls • input from keyboard • input from joysticks • ball collisions with the player paddles •
  • 2. score displayed • game waits for start button, plays, ends • ball speeds up, English on the ball • This list is meant to get the big things out on the table and also give you ideas for a development plan. Development Plan: Iteration Take a step back and think about the development process and your goals. Game development lends itself well to an iterative approach. Build something small and work to improve it in steps. Consistent forward progress depends on choosing the size and direction of your iterative steps. Games are built by combining a number of individual technologies. The set of technologies you must pull together depend on the game, the engine, and the development environment. Iteration steps should be small and able to be completed in one sitting. In the beginning of a project, find steps that result in visual changes. Start by getting the main game objects drawn. a hello world window • overhead camera and drawing the court • draw the ball • draw the paddles • Next, do one step with all the different tech your game needs. The goal is to write code that can be improved on. The first part of a project focuses on a kind of breadth-first development. Keep your steps small -- aim for something you will complete in one sitting. player input and connect it to the paddles • collisions of the ball with the court • collisions of the ball with the player paddles • playing of a sound • 2D heads up display of score • It's important not to wait too long before tackling this next stage. Setup your game to run in an attract mode loop, play the game, and exit without any memory leaks. write a game controller that manages state transitions including the following • display a title screen • wait for start button to start the game • start and play the game until someone wins • display the game over screen • testing for clean, balanced coding • loop until the player exits • exit without any memory leaks • After the first pass of your game elements and technology is completed, you will likely have some messy code. Now is a good time to look at the overall structure of your game and decide if you can improve it. Also, look at each system, improving the names usually leads to better code.
  • 3. improve the structure • improve the names, define and follow a coherent approach • Now, focus on the game itself. Iterate on the elements that stick out the most. Eventually, your focus will change and instead of fixing things that are problems, you'll be improving things and looking for ways to make things more interesting. find the worst thing, iterate on it until it's not the worst thing • find things that you can make better • Realize that you will never run out of things to work on, but you will run out of time. Developing a Plan: Refactoring Plan on refactoring your code from the start. Rather than work to write perfect code, write good code that can be refactored. This means that you should write code that with distinct names. Set yourself up to be able to search and rename sections of code. Also, write code that groups a feature together. Refactor the sections when they get too big or the logic starts getting tangled or they clearly belong in their own function. It seems that most code is refactored two or three times during a project. However, don't be over anxious to refactor code, you should wait to refactor until the old code becomes unbearable. The Game Loop The beginning of the game controller class. Independent update and draw. Keep features orthogonal. Class pointers and references Pointers are an important part of game programming. Get used to using them. Common Identifier Space Start all of your identifiers at one. Reserve zero for defaults and uninitialized tests. Define one set of identifiers. Use these for game ids, return codes, other uses. enum { // int, unique ids for any use in the game // rather than have ids all over the place GAMEID_TMP=1, // reserved GAMEID_GAME, // game instance // game modes GAMEID_MODE_ATTRACT, GAMEID_MODE_GAME, // return codes GAMEID_BALL_REFLECT, GAMEID_BALL_OFFLEFT, GAMEID_BALL_OFFRIGHT, };
  • 4. Function construction Initialize. Body. Finalize and exit. One return. Initialization section must initialize all the objects. Design your objects so zeroing them initializes them. Return negative values for errors. int function() { int r; ///////////// // initialize /////// // body r=0; // normal exit code, success BAIL: ///////////////////// // finalize, clean up return r; } // function() Free what you allocate Clean up after yourself. Write your final code so that it is path independent. If non-zero, free and zero. Public Classes Projects go through a few phases. In the beginning, setup your code for flexibility. The beginning of a project is marked by changes in direction and structure. One way to facilitate this is with classes with all the elements marked public. Once you understand the problem space and your solution, you will refactor your code to move fields into the private or protected. Until then, it will only slow you down. Protecting fields becomes important when you scale your project up and add programmers. Be sure to set aside time for this step. Feature based accessors, not straight-to-vars. Peter Bennett. Allocating and Freeing Objects In C, memory allocated and freed with the malloc() and free() functions and objects must be setup by the programmer. In C++, the new operator calls malloc() to allocate memory and calls the constructor function to setup the object. Likewise, the delete operator calls the destructor function to cleanup the object and then calls free() to release the memory. Memory is one of your primary resources. Game objects are defined by classes and take up memory when they are created.
  • 5. QE Base Classes Derive from qe base classes so all the allocations run through the engine. Objects derived from qe base classes zero their memory when allocated. Derive from qeUpdateBase for objects that are managed by the game engine. Normally, you will override the update() and draw() functions with your own code. Derive from qe for simple, non-managed objects. Bracketing Resources Bullet-proof your resource allocation and freeing. Build a structure that guarantees that you free all the resources you allocate. This is accomplished with a 3-part structure: initialization, use, and finalization. The initialization is code that is guaranteed to run early in the game, and finalization code is guaranteed to run before you exit. Initialization code should simply zero all resource fields. The finalization code, which is also guaranteed to run, checks resource fields, frees any non-zero resource, and then zeros that field. There must be only one owner of each resource and one initialization and finalization section of code for each resource field. The code that runs in-between the initialization and finalization must follow the simple rules to test the resource field before each use and set the field if the resource is successfully created. This structure allows you to bracket your resource usage and provides the necessary structure to write code that guarantees balanced resource usage. initialization (guaranteed): zeros • finalization (guaranteed): tests, frees & zeros • usage: tests, allocates & sets, uses • Game Controller Class This class manages the game. Load all the initial content including images and sounds. Modes should be avoided because they promote structural discontinuity and ultimately complicate your program greatly. However, modes do make prototyping easier and novice programmers should use modes until a modeless structure resolves itself. Start off in attract mode. When the start button is pressed, drop into game mode. Attract mode waits for the start button and then drops into game mode. Display the last scores. Game mode runs the game until the game is over. Then drops back into attract mode. Matrices A matrix is a set of numbers that define any combination of movements, rotations, and scaling of objects in a 3D world. The matrices used here are 12 element matrices. The first 3 entries are the X, Y, and Z positions. The next 9 entries define a simple rotation and scaling matrix. Taken together, the 12 elements are all you need for most game operations and qe provides a set of functions to operate on the raw 12 element array of floats.
  • 6. Simple 3D Camera For now, it's best to use the code and experiment with it as you go. Cameras are a software metaphor implemented with matrices. The camera matrix transforms objects from world space into camera space. In other words, it treats the camera as the center of the world, and moves and rotates everything in the world in front of the camera. In OpenGL, this matrix is called the MODELVIEW matrix. There are two matrices in OpenGL. You can think of the MODELVIEW matrix as the way you position the camera, and the PROJECTION matrix as the camera's lens. OpenGL's PROJECTION matrix transforms objects from camera space into clip coordinates. The perspective division (divide by z) then transforms clip coordinates into normalized device coordinates. These coordinates are then mapped through the viewport into a window. BRK(), Code to Continue This macro expands to into an interrupt call that breaks the program execution and 'wakes up' the debugger at that point. If the debugger is not running, the macro has no effect. Write your code to detect errors and continue running. Players don't want to see a dialog box asking them if they want to send an error report to Microsoft. Even in extreme cases, do your best to continue running. That said, you must detect and handle all errors. When you run into an error, report it, and write code to do the best it can to continue running -- this may mean an object is drawn without a texture or may even be missing. Drawing The Court The court is managed by the world class. A solid rectangle is drawn for the court. // JFL 14 Aug 07 // JFL 15 Mar 09 int World::draw(void) { glColor3f(0,0,1); // set color to blue glPolygonMode(GL_FRONT,GL_FILL); // draw filled polygons glBegin(GL_QUADS); // draw quads counter-clockwise from camera's view glVertex3f(this->xyzMin[0],this->xyzMin[1],this->xyzMin[2]); glVertex3f(this->xyzMin[0],this->xyzMin[1],this->xyzMax[2]); glVertex3f(this->xyzMax[0],this->xyzMin[1],this->xyzMax[2]); glVertex3f(this->xyzMax[0],this->xyzMin[1],this->xyzMin[2]); glEnd(); return 0; } // World::draw() Reset Flags Once Flags can be used to signal a request from many possible places. Handle the flag in one place. Test the flag, clear it, call the handler. Clear the flag in only one section of code.
  • 7. 000ZY Coordinates This choice is largely arbitrary, but should be something you're comfortable with. One unit = one foot is the standard I use. This depends on the scale of your game, but should be determined before you start coding. Velocities Variable frame rate systems must multiply the velocity by the amount of time elapsed since the last loop. Keep track of the time since the last update. float t; // find time since last update t=this->timeOfLastUpdate; this->timeOfLastUpdate=qeTimeFrame(); t=this->timeOfLastUpdate-t; // delta // xyz += vel*t this->xyz[0]+=this->vel[0]*t; this->xyz[1]+=this->vel[1]*t; this->xyz[2]+=this->vel[2]*t; This update method -- moving by adding the velocity times the elapsed time -- is called Euler integration. The timeOfLastUpdate variable must be reset when the object is reset. 3D World, Camera The world is quot;in 3Dquot;. However, the camera is fixed above it and looking down. This gives the appearance of a 2D game. ////////////////////// // Camera class fields float fovyHalfRad; // field of view angle in y direction in radians float nearClip; // near clipping plane float farClip; // far clipping plane float winWidth; // in pixels float winHeight; // in pixels float winWDivH; // window aspect ratio float nearHeight; // height of window at near plane float mat12[12]; // camera matrix /////////////////////// // Camera setup -- once this->nearClip = 1; this->farClip = 500; this->fovyHalfRad = 0.5*((63*PI)/180.0); // 0.5*(degrees->radians) this->nearHeight = this->nearClip * MathTanf(this->fovyHalfRad); // camera matrix transforms from world space into camera space SET3(pos,0,CAMERA_Y,0); // position of camera SET3(at,0,0,0); // where camera is looking at SET3(up,0,0,-1); // the camera's up direction qeCamLookAtM12f(this->mat12,pos,at,up); // compute camera mat
  • 8. ///////////////////////////////////////////////////// // Camera matrices -- before you draw with the camera if(qeGetWindowSize(&this->winWidth,&this->winHeight)<0) bret(-2); this->winWDivH=this->winWidth/this->winHeight; // set the PROJECTION matrix (the camera lens) glMatrixMode(GL_PROJECTION); glLoadIdentity(); float yy = this->nearHeight,xx=this->nearHeight*this->winWDivH; glFrustum(-xx,xx,-yy,yy,this->nearClip,this->farClip); // set the MODELVIEW matrix (position and orientation of the camera) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); qeGLM12f(this->mat12); // set matrix 2D Gameplay The keyboard and joystick are used for the player's up and down. Keyboard as Buttons Input By default, the keyboard acts like a set of buttons to the game engine. QE uses button counts which are incremented when the buttons are pressed and released. When the button is down, the count is odd, and when the button is up, the count is even. The bottom bit is set for odd numbers and can easily be tested with a bitwise test. // up-down-left-right arrow mapping if(1&qeInpButton(QEINPBUTTON_UP)) /* up button is pressed */; else if(1&qeInpButton(QEINPBUTTON_DOWN)) /* down button is pressed */; Joystick Input Joystick positions are returned as floating point values. Some care should be taken to drop out the values when the joystick is near the center. // get player 0 joystick value -1..0..1 stick=qeInpJoyAxisf(0,QEJOYAXIS_LEFT_Y); #define STICK_DEADZONE 0.2 // enforce stick deadzone & re-normalize if(stick>STICK_DEADZONE) stick=(stick-STICK_DEADZONE)/(1.0-STICK_DEADZONE); else if(stick<-STICK_DEADZONE) stick=(stick+STICK_DEADZONE)/(1.0-STICK_DEADZONE); else // in deadzone, zero stick=0; Sounds Record sounds into .wav files. Most sound effects should be recorded in mono at a low resolution. Trigger the sounds in the game.
  • 9. // setup sound quot;bumpquot; on channel 1 if((r=qeSndNew(quot;bumpquot;,M_SNDNEW_CH_1,0,quot;art/sounds/pongbump.wavquot;))<0) BRK(); // trigger the sound qeSndPlay(quot;bumpquot;); Use Restart Functions Use restart rather than start functions. Names matter. Start functions usually hide assumptions regarding the state of the game. Drawing 2D: Camera and Image Use the 32 bit image of the digits. The image has been created so there are 8 digits on the first row and 2 on the second row. 0 1 2 3 4 5 6 7 and 8 9. // register image if(qeImgNew(quot;icons1quot;,0,quot;art/images/icons1.tgaquot;)<0) BRK(); // make sure path is correct qeTexBind(quot;icons1quot;); 2D images are drawn the same way other polygons are drawn. However, the camera and draw modes have to be setup correctly. qefnSysCamFlat(); // ortho: (0,0) top left (1,1) bottom right // setup texture mode to blend glPolygonMode(GL_FRONT,GL_FILL); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_ADD); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); Drawing 2D: UVs and Vertices As always, vertex coordinates are specified in the space defined by the camera matrices and define the structure of the object. UV coordinates define how the image maps from texture space to the polygon. UVs are specified in normalized image space. Ways to get the images to stretch or be drawn close to one to one from the image. To find UVs, start with the image coordinates and divide by the image size. // find image coordinates for a positive integer n // icons in our image are 32x32 and there are 8 across horizontally // these coordinates are in pixel coordinates of the image // top left of the image 0,0 u0=(n&7)*GAME_ICON_WIDTH; v0=(n>>3)*GAME_ICON_HEIGHT; // map image coordinates into uv space u0/=imgWidth; v0/=imgHeight;
  • 10. Name Your Objects Debugging is easier when object have string names. When debugging, checking the names of objects is a good way to verify system and game integrity. Also, you can sometimes trap on on the name.