Game Programming - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Game Programming

Description:

int countdown; int volleycount; int blobx, ... countdown = 400; volleycount = 0; ... if(countdown==nextballcount/8) play_sample(goball, 255, 128, 1000, FALSE) ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 32
Provided by: Rap46
Category:

less

Transcript and Presenter's Notes

Title: Game Programming


1
Lecture 4 Game Programming Game Basics
Lecture 4 Game Programming Game Basics
2
Overview
The source code in this lecture is a complete
implementation of the demonstration pong game
discussed in Lecture 2. This sample program does
not take advantage of many of the features
available in the Allegro Graphics and Gaming
Library. The purpose of this code is to set a
baseline for game programming from which we will
investigate alternative approaches to solving
many of the problems and challenges in game
programming. The features of interest in this
sample are
  • An Introductory Animation Sequence
  • Use of Sound (.wav files)
  • Controlling the Main Game Loop
  • Updating Object Positions
  • Animation
  • Collision Detection
  • Keeping Score
  • An End of Game Animation Sequence

3
Start Game Animation
4
Game Action
5
End of Game Animation
6
//////////////////////////////////////////////////
/////////////// //
// // KING PONG - R.
A. Pilgrim 08/28/2007
// //
// ///////////////////////////////
////////////////////////////////// include
ltallegro.hgt define BLACK makecol(0,0,0) define
GREEN makecol(64,255,64) define RED
makecol(255,64,64) define YELLOW
makecol(255,255,64) int main(void) //
initialize variables bool done false
int padLeftX, padRightX int padLeftY,
padRightY int padLeftYold, padRightYold
int padWidth 6 int padHeight 60
int padVy 4 int rad 4 int
nextballcount 300 int bx,by,bvx,bvy
int bxold, byold int bxi, byi, bvxi, bvyi,
bvxmult int Lcount int Rcount
int countdown int volleycount int
blobx,bloby int blobsize 4 char k
Colors can be defined globally Variables are
declared Constants are declared and instantiated
7
SAMPLE ping SAMPLE pong SAMPLE
intro SAMPLE applause SAMPLE
rockyou SAMPLE buzzer SAMPLE
goball SAMPLE cheer SAMPLE laugh
SAMPLE crowd1 SAMPLE crowd2
SAMPLE crowd3 SAMPLE crowd4 SAMPLE
crowd5 FONT bigfont PALETTE
palette // init allegro and install
needed components allegro_init()
install_keyboard() //install_timer()
srand(time(NULL)) //install_sound if
(install_sound(DIGI_AUTODETECT, MIDI_NONE, "") !
0) allegro_message("Error
initializing sound system") return 1

Pointers to sound files are created but the sound
files themselves are loaded later... This game
uses the keyboard interface Sound files will be
.wav types
8
ping load_sample("ping.wav") pong
load_sample("pong.wav") intro
load_sample("rumble.wav") applause
load_sample("applause_crowd.wav") rockyou
load_sample("we_will_rock_you.wav") buzzer
load_sample("buzzer.wav") goball
load_sample("goball.wav") cheer
load_sample("cheer.wav") laugh
load_sample("laugh.wav") crowd1
load_sample("crowd1.wav") crowd2
load_sample("crowd2.wav") crowd3
load_sample("crowd3.wav") crowd4
load_sample("crowd4.wav") crowd5
load_sample("crowd5.wav")
bigfont load_font("ex03.pcx",palette,NULL)
//initialize video mode int ret
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480,
0, 0) if (ret ! 0)
allegro_message(allegro_error) return
1
Sound files are loaded and associated with the
pointers declared above. A large font is loaded.
The type of this file is .pcx
9
// initialize game state padLeftX 20
padRightX SCREEN_W - 20 padLeftY
SCREEN_H/2 padRightY SCREEN_H/2 bxi
SCREEN_W/2 byi SCREEN_H/2 bvyi
0 bvxi 2 bx bxi by byi
bvx 0 bvy bvyi bvxmult 1
Lcount 0 Rcount 0 countdown
400 volleycount 0
The set of variables defining the initial
positions of the moveable objects and the
counters of the main game loop are initialized.
These data represent the state of the game.
10
// Game Start Sequence play_sample(intro, 255,
128, 1000, FALSE) while(!keyKEY_SPACE)
textprintf_centre_ex(screen, bigfont,
SCREEN_W/2, SCREEN_H/2-20, (rand() 16), -1,
"KING PONG") textprintf_centre_ex(screen,
font, SCREEN_W/2,SCREEN_H-20, (rand()
16), -1,"PRESS SPACE TO START") blobx
rand()SCREEN_W bloby rand()SCREEN_H
rectfill(screen,blobx,bloby,blobxblobsize,blobyb
lobsize,
makecol(rand()256,rand()256,rand()256))
if(rand()1000) rectfill(screen,0,0,SCREEN
_W,SCREEN_H,BLACK) play_sample(cheer,255,128,1
000,FALSE) play_sample(crowd2,255,128,1000,TRUE)
rectfill(screen,0,0,SCREEN_W,SCREEN_H,BLACK)
The introductory animation sequence continues
until a player presses the space bar.
11
/


M A I N G A M E L O O P


/
while(!done) // check for
game quit if(keyKEY_ESC)
done true
if(countdowngt0) countdown - 1
if(countdown1)
bvx bvxibvxmult
stop_sample(intro)
if(countdownnextballcount/8)
play_sample(goball, 255, 128, 1000, FALSE)
// save current paddle/ball
positions before updates padLeftYold
padLeftY padRightYold padRightY
bxold bx byold by
// update ball position bx
bvx by bvy
The first check in the main game loop is to see
if the game has quit. This could be due to
somone pressing the ltESCgt key or because someone
has won the game.
12
// bounce ball off top and bottom of game
space if (by lt 0 by gt SCREEN_H) bvy
-bvy play_sample(pong, 255, 128, 900
10abs(bvxbvy), FALSE) //
manage ball hitting left paddle if ((bx lt
padLeftX padWidth/2) (abs(padLeftY -
by)ltpadHeight/2)) volleycount 1
if(volleycountgt8) switch(rand()
6) case (0)
play_sample(rockyou,255,128,1000,FALSE)
break case (1)
play_sample(crowd1,255,128,1000,FALSE)
break case (2)
play_sample(crowd2,255,128,1000,FALSE)
break case (3)
play_sample(crowd3,255,128,1000,FALSE)
break case (4)
play_sample(crowd4,255,128,1000,FALSE)
break case (5)
play_sample(crowd5,255,128,1000,FALSE
) break

Additional sounds are played when there have been
at least 8 successful ball returns.
13
play_sample(ping, 255, 128, 900
10abs(bvxbvy), FALSE) bx padLeftX
padWidth/2 bvx - bvx bvy 0
if(padLeftY-bylt-padHeight/5) bvy 1
if(padLeftY-bygtpadHeight/5) bvy - 1
if(padLeftY-bylt-padHeight/4) bvy
2 if(padLeftY-bygtpadHeight/4) bvy
- 2 if(bvy 0) bvx 1
if(bvxgt8) bvy rand() 4 - 2
if(rand() 100 gt 94) bvy rand() 4 -
2
14
// manage ball hitting right paddle
if ((bx gt padRightX - padWidth/2)
(abs(padRightY - by)ltpadHeight/2))
volleycount 1
play_sample(pong, 255, 128, 900
10abs(bvxbvy), FALSE) bx
padRightX - padWidth/2 bvx -
bvx bvy 0
if(padLeftY-bylt-padHeight/5) bvy
1 if(padLeftY-bygtpadHeight/5)
bvy - 1
if(padRightY-bylt-padHeight/4)
bvy 2 if(padRightY-bygtpadHeight/
4) bvy - 2
if(bvy 0) bvx - 1
if(bvxlt-8) bvy rand() 4 -
2 if(rand() 100 gt 94)
bvy rand() 4 - 2
Additional crowd sounds not necessary for both
the left and the right paddle.
15
// manage ball missing left paddle
if(bx lt padLeftX padWidth/2)
play_sample(buzzer, 255, 128, 1000, FALSE)
rectfill(screen,0,0,SCREEN_W,SCREEN_H,
BLACK) Rcount 1 bx
bxi SCREEN_W/3 by byi
bvy 0 bvx 0
bvxmult -1 countdown
nextballcount if(volleycountgt10)
play_sample(applause,255,128,1000,FA
LSE) if(volleycountlt2)
play_sample(laugh,255,128,1000,FALSE)
volleycount 0
16
// manage ball missing right paddle
if(bx gt padRightX - padWidth/2)
play_sample(buzzer, 255, 128, 1000,
FALSE) rectfill(screen,0,0,SCREEN_W,
SCREEN_H,BLACK) Lcount 1
bx bxi - SCREEN_W/3 by
byi bvy 0 bvx 0
bvxmult 1 countdown
nextballcount if(volleycountgt10)
play_sample(applause,255,128,1000,F
ALSE) if(volleycountlt2)
play_sample(laugh,255,128,1000,FALSE)
volleycount 0
17
// update scoreboard rectfill(screen,
SCREEN_W/2-25,0,SCREEN_W/225,20,BLACK)
textprintf_centre_ex(screen, bigfont, SCREEN_W/2,
10, 15, -1,
"d d", Lcount, Rcount)
// check for winner if(Lcount11
Rcount11) done true // manage
user input for paddle movement if(keyKEY_A)
padLeftY - padVy if(keyKEY_Z)
padLeftY padVy if(keyKEY_K)
padRightY - padVy if(keyKEY_M)
padRightY padVy // manage paddles
encountering boundary of game space
if(padLeftYltpadHeight/2) padLeftY
padHeight/2 if(padLeftYgtSCREEN_H -
padHeight/2) padLeftY SCREEN_H -
padHeight/2 if(padRightYltpadHeight/2)
padRightY padHeight/2
if(padRightYgtSCREEN_H - padHeight/2)
padRightY SCREEN_H - padHeight/2
18
// animate ball and paddles
circlefill(screen,bxold,byold,rad,BLACK)
circlefill(screen,bx,by,rad,YELLOW)
rectfill(screen,padLeftX-padWidth/2,padLeftYold-pa
dHeight/2, padLeftXpadWidth/2,p
adLeftYoldpadHeight/2,BLACK)
rectfill(screen,padRightX-padWidth/2,padRightYold-
padHeight/2, padRightXpadWidth/
2,padRightYoldpadHeight/2,BLACK)
rectfill(screen,padLeftX-padWidth/2,padLeftY-padHe
ight/2, padLeftXpadWidth/2,padL
eftYpadHeight/2,GREEN)
rectfill(screen,padRightX-padWidth/2,padRightY-pad
Height/2, padRightXpadWidth/2,p
adRightYpadHeight/2,RED) rest(10)
/


E N D O F M A I N L O O P



/
19
if(Lcountgt10 Rcountgt10) // end sequence
play_sample(cheer,255,128,1000,TRUE)
while(!keyKEY_SPACE) if(Lcountgt10)
textprintf_centre_ex(screen, bigfont,
SCREEN_W/2, SCREEN_H/2-20,
(rand() 16), -1, "GREEN WINNER!")
if(Rcountgt10) textprintf_centre_ex(screen,
bigfont, SCREEN_W/2, SCREEN_H/2-20,
(rand() 16), -1, "RED WINNER!")
textprintf_centre_ex(screen,
font, SCREEN_W/2,SCREEN_H-20,
(rand() 16), -1,"PRESS SPACE BAR TO QUIT
GAME") blobx rand()SCREEN_W bloby
rand()SCREEN_H rectfill(screen,blobx,blob
y,blobxblobsize,blobyblobsize,
makecol(rand()256,rand()256,rand()256))
if(rand()1000) rectfill(screen,0,0,SC
REEN_W,SCREEN_H,BLACK) //end
program allegro_exit() return
0 END_OF_MAIN()
20
Physics Demos
Physics Demo 1 - Demonstrates motion of objects
in a constant gravitational field.  Program is an
example of a multiple source file project. 
Special Features Includes functions for
converting from world coordinates to graphical
display coordinates, polar to cartesian
coordinates and conversion from degrees to
radians. Physics Demo 2 - This program
demonstrates the translation and rotation (2-D)
of a simple object.  Object movement is modeled
as ballistic (freefall) in a constant
gravitational field. A 2D rotation function has
been added to the kinematics module. Special
Features Double buffering, drawing a vector
object.
21
Motion in a Constant Gravitational Field
Separate the motion in the x direction
(horizontal) from motion in the y direction
(vertical). Gravity acts in the negative y
direction (-9.8 m/s2). There is no force in the
x direction on a moving object (without friction).
x, y - object location vx, vy - object
velocity ay - object acceleration due to gravity
-9.8 meters/second/second.
y
x
22
include ltallegro.hgt include "kinematics.h" def
ine YELLOW makecol(255,255,0) int main()
int max_x 640 int max_y 480
double x -150.0 double y -110.0
double vx 10.0 double vy 50.0
double ax 0.0 double ay -9.8
double dt 0.1
double wxmin -160.0 double wxmax
160.0 double wymin -120.0 double
wymax 120.0 int px,py int
rad 1 int col YELLOW int r,g,b
int n 10 bool done false
allegro_init() install_keyboard()
srand(time(NULL)) int ret
set_gfx_mode(GFX_AUTODETECT, max_x, max_y, 0,
0) if (ret!0)
allegro_message(allegro_error) return
1
23
while(!done !keyKEY_ESC)
if(xgtwxmax yltwymin n0)
n 1 x 0.0
y -110.0 yold y
xold x delrot
(double)(rand()200)/1000.0 - 0.1
labeled false radians
degrees_2_radians(ang)
polar_2_cartesian(vx,vy,vmag,radians)
ang 10.0
clear_to_color(buffer,0)
if(anggt180.0) done true
xold x yold y
move(x,vx,ax,dt) move(y,vy,ay,dt)
draw_obj(xold,yold,0,rot) rot
delrot draw_obj(x,y,col,rot)
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W,
SCREEN_H) rest(1)
24
Some Random Shots
25
Some Useful Functions
void move( double d, double dv, double da,
double dt) d d dvdt 0.5dadtdt
dv dv dadt int world_2_display(double
d, double dmin, double dmax, int pmax)
return (int)((double)pmax/(dmax-dmin)(d-dmin))
double degrees_2_radians(double angle)
return piangle/180.0 void polar_2_cartesian(d
ouble x, double y, double r, double rads)
x rcos(rads) y rsin(rads)
26
Sample Output
27
Object Rotation
Rotation of a point (x,y) is assumed to be about
the origin (0,0). An object may be defined by a
set of points S(i) (xi,yi). If rotation about
another point is desired, then the set of points
defining the object are rotated about the origin
and then translated to the desired location.
void rotate(double x, double y, double rads)
double xt,yt xt x yt y
x xtcos(rads) - ytsin(rads) y
ytcos(rads) xtsin(rads)
28
Double Buffering
The purpose of double buffering is to eliminate
or reduce the flicker in an animation due to
drawing directly on the display being viewed
(screen). Instead we will draw on a bitmap
elsewhere in memory and then transfer (blit) the
completed image to the screen in one operation.
BITMAP buffer int ret
set_gfx_mode(GFX_AUTODETECT_WINDOWED, max_x,
max_y, 0, 0) if (ret!0)
allegro_message(allegro_error) return
1 buffer create_bitmap(SCREEN_W,
SCREEN_H) clear_bitmap(buffer) blit(buffer
, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H)
29
Drawing a Simple Vector Object - The Star
struct location double x double
y obj5 0.0,10.0,8.4,-10.0,-10.0,2.0,10.0,2
.0,-8.4,-10.0
void draw_obj(double x, double y, int col, double
rot) int px0,py0,px1,py1,px2,py2
double rx,ry rx obj0.x ry
obj0.y rotate(rx,ry,rot) px0
world_2_display(xrx,wxmin,wxmax,max_x) py0
world_2_display(yry,wymax,wymin,max_y)
px1 px0 py1 py0 for(int
i1ilt5i) rx obji.x ry
obji.y rotate(rx,ry,rot)
px2 world_2_display(xrx,wxmin,wxmax,max_x)
py2 world_2_display(yry,wymax,wymin,max_y
) line(buffer,px1,py1,px2,py2,col)
px1 px2 py1 py2
line(buffer,px0,py0,px1,py1,col)
30
Movement and Rotation of a Vector Object
31
3D Rotation
Write a Comment
User Comments (0)
About PowerShow.com