1 159.234lecture 10 159.234 lecture 10 developing the tank game assignment #1 game structure drawing...

60
1 159.234 159.234 LECTURE 10 LECTURE 10 Developing the Tank Developing the Tank Game Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in, zoom-out

Upload: kerry-carr

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

1

159.234159.234 LECTURE 10LECTURE 10

Developing the Tank GameDeveloping the Tank Game

• Assignment #1 • Game Structure• Drawing the Tank•Firing the bullet• Keyboard, mouse• Zoom-in, zoom-out

Page 2: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

2

Objects in the GameObjects in the Game

TankAlienCloudsBulletLedge

We would want to develop the game by writing classes, and creating instances of them to enable us to keep track of each object’s property isolated from the others.

The game is too complex to be written using just the traditional structuredapproach. OOP is the most practical approach in writing this game.

Why develop this game?

Page 3: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

3

System of CoordinatesSystem of Coordinates

Tank, Alien & Clouds movementfollow this system of coordinates.

World System of Coordinates

+x

+y

0

To move upwards: + DispYTo move downwards: - DispYTo move to the right: + DispXTo move to the left: - DispX

All Physics equations work with this system of coordinates.

Disp – stands for DisplacementDisp – stands for Displacement

Page 4: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

4

System of CoordinatesSystem of Coordinates

EARTH_Y:

For example: 100

To respond to Left/Right movementcommands from the user, simple statements such as the following wouldsuffice:

Left: if(Boundary checking here…) TankX –= 2;

Right: if(Boundary checking here…) TankX += 2;

+x

+y

0

Page 5: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

5

System of CoordinatesSystem of Coordinates

The Bullet object is plotted using Physics equations and transformationEquations.

TransformationX(PhysicsX(t,..))

t – dictates the next position of the bullet.

+x

+y

0

Page 6: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

6

Transformation Equations

WORLD-to-DEVICE COORDINATES

159.234

1280 x 1024 pixels

100,000,000 miles x 500,000 miles

World System of Coordinates Device System of Coordinates

+x

+y+x

+y0

0

(Xworld,Yworld)(XDevice,YDevice)

Page 7: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

7

World BoundariesSETTING THE BOUNDARIES

159.234

Use the upper-left and bottom-right coordinates to set the boundaries

+x

+y+x

+y0

0

(Xworld,Yworld)(XDevice,YDevice)

Top-left: x1, y1Bottom-right: x2, y2

Page 8: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

8

Projectile Motion

Setting the World Boundaries

159.234

+x

+y

0

(Xworld,Yworld)

(x1, y1)

(x2, y2)

x1=0y1 = maximum_heightx2 = maximum_rangey2 = 0

ground

Page 9: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

9

System of Coordinates

EFFECTS OF CHANGING THE BOUNDARIES

159.234

What happens if we double all the maximum values for the WorldBound Coordinates?

What happens if we decrease the maximum values for the WorldBound Coordinates?

Page 10: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

10

World Boundaries

SETTING THE BOUNDARIES

159.234

gVoHeight

2

sin*2

max

where =85 degrees

gVo

2sinDistance Horizontal 2

max where =45 degrees

g

Vot flight

sin**2 Time of flight : from

take-off to landing

Page 11: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

11

World-to-Device Coordinates

TRANSFORMATION EQUATIONS

159.234

12

12

XX

XX

WorldBoundWorldBound

dDeviceBoundDeviceBounXslope

11*tx_intercep XX WorldBoundXslopedDeviceBoun

tx_intercepWorld* XX XslopeDevice

Computed using the Physics equation for x

Page 12: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

12

Projectile Motion

PHYSICS EQUATIONS

159.234

where g = 9.8 m/sec.2 pull of gravity

Vo in m/sec. initial velocityt in sec. Time in radians Launching Angle

tVox *cos*

tVogty *sin*2

1 2

Increment t in the equations and x and y will be automatically adjusted.

Page 13: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

13

Projectile Motion

Unit Conversion for Theta (degrees-to-radians)

159.234

= Theta_in_degrees * M_PI/180

Defined in math.h

e.g. cos(), sin()

Page 14: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

14

Projectile Motion

PUTTING THE PIECES TOGETHER

159.234

// InitGraphics here// InitBoundaries here t=0.0; while(t < tf) { cleardevice(); setcolor(RED); circle (Xdev( x(t, Vo, Theta) ), Ydev( y(t, Vo, Theta)), 12); t=t+tinc;

} Physics Equation for x

World-to-Device Transformation Function

circle(x, y, radius)

Page 15: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

15

BulletBullet

class Bullet {public:

private };

Physics EquationsTransformation EquationsDrawing the BulletMoving the BulletChecking for Collision – can be handled outside the Bullet class, but this will depend on your class design.

The codes that make the Tank move will have to be assembled together inside a function, rather than being dispersed inside a while loop (as seen earlier).

Converting the code into its object-oriented representation:

PP

Page 16: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

16

BulletBullet

class Bullet {public:

private };

Physics EquationsTransformation EquationsDrawing the BulletMoving the BulletChecking for Collision

How to synchronize the movement of the tank and the bullet?

How to make the bullet follow the path of a trajectory moving to the right/left?

What controls the speed of the bullet animation?

Converting the code into its object-oriented representation:

QQ

QQ

QQ

Page 17: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

17

Bullet & TankBullet & Tank

The bullet should be released where the Tank’s nozzle points to, and follow the appropriate trajectory (whether to move to the left, or right).

How can this be done?

Page 18: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

18

Releasing the bulletReleasing the bulletCASES TO CONSIDER

Note: the tank’s nozzle can be directed by the user to any angle (e.g. [0, 170])

Page 19: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

19

Setting the boundaries for the BulletSetting the boundaries for the Bullet

• Next, set the bullet’s world components (x, y) to point to the location of the tank’s nozzle.

x = tankNozzleX;y = tankNozzleY;

The time of flight will have to be calculated using the formula that considers a Target lower than the launch point.

AA

Page 20: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

20

Target lower than the launch point

Page 21: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

21

Class TankClass Tank

Launching Angle of Bullet = Theta from Tank Vo = Vo from Tank

You should monitor the current Vo and Theta set by the user for the Tank, and pass these parameters to the Bullet class prior to releasing the bullet.

Page 22: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

22

Class TankClass Tank

Initially, NozzleAngle = Angle as defined by user – 90; //45-90 Theta = Angle as defined by user; //45

Next, every time the Tank’s NozzleAngle is changed (through userkeyboard interaction), we perform the following updates:

• To move the NozzleAngle in a clock-wise direction: NozzleAngle = NozzleAngle +2

Theta = Theta -2 • To move the NozzleAngle in a counter clock-wise direction:

NozzleAngle = NozzleAngle -2Theta = Theta +2

Tank’s Nozzle Angle vs. Bullet’s Launching AngleTank’s Nozzle Angle vs. Bullet’s Launching Angle

Page 23: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

23

Class TankClass Tank

NozzleAngle = Angle NozzleLength = 25 //some constant value

Angle from user (e.g. 45 degrees)

(NozzleX, NozzleY)

(Tx,Ty)

NozzleX = Tx + NozzleLength*cos((NozzleAngle) * M_PI / 180.0)

NozzleY = Ty + NozzleLength*sin((NozzleAngle) * M_PI / 180.0)

Take note: The Nozzle position is calculated relative to the Tank’s body.Therefore, when the tank’s body moves at some angle, the nozzle moves as well.

Page 24: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

24

Class TankClass Tank

Tank during take-offTank during take-off

When in-flightWhen in-flight

Making the Tank to appear to be inclined would Making the Tank to appear to be inclined would require some trigonometric formulas.require some trigonometric formulas.

Nozzle moves with the Tank’s body!

Page 25: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

25

Class TankClass Tank

(TankX,TankY)(TankX,TankY)

In so doing, only TankX and TankY needs to be updated every time the user instructs the tank to move via keyboard control.

For easier handling, you may want to compute all the coordinates of the tank figure based on just a single point in the Tank (e.g. TankXTankX, TankYTankY).

Top-right

bottom-right

Top-left

bottom-left

The rest of the other coordinates can be calculated relative to this point.

Page 26: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

26

Facing to the RightFacing to the Right

Top-right X = TankX+W*cos((Angle)*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Top-right Y = TankY+H+W*sin((Angle)*M_PI/180));e.g. Angle=5e.g. Angle=5

//top-right tankBody[2]=Xdev(WBound,DBound,(tankX+W*cos((angle) * M_PI / 180.0))); tankBody[3]=Ydev(WBound,DBound,(tankY+H+W*sin((angle) * M_PI / 180.0)));

Page 27: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

27

Facing to the RightFacing to the Right

Bottom-right X = TankX+W*cos((Angle)*M_PI/180)) + (H*sin(Angle*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Bottom-right Y = TankY+W*sin((Angle)*M_PI/180));

e.g. Angle=5e.g. Angle=5

//bottom-right tankBody[4]=Xdev(WBound,DBound,(tankX+W*cos((angle) * M_PI / 180.0)) + (H*sin(angle*M_PI/180.0))); tankBody[5]=Ydev(WBound,DBound,(tankY+W*sin((angle) * M_PI / 180.0)));

Page 28: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

28

Facing to the RightFacing to the Right

Top-left X = TankX;

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Top-left Y = TankY+H;

tankBody[0]=Xdev(WBound,DBound,tankX); tankBody[1]=Ydev(WBound,DBound,tankY+H);

Page 29: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

29

Facing to the RightFacing to the Right

Bottom-left X = TankX + (H*sin(Angle*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Bottom-left Y = TankY;e.g. Angle=5e.g. Angle=5

//bottom-left tankBody[6]=Xdev(WBound,DBound,tankX+(H*sin(angle * M_PI/180.0))); tankBody[7]=Ydev(WBound,DBound,tankY);

Page 30: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

30

Facing to the LeftFacing to the Left

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Page 31: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

31

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

Page 32: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

32

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

Page 33: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

33

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

Page 34: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

34

Fillpolyvoid fillpoly (int numpoints, int *polypoints);

159.234

fillpoly draws the outline of a polygon with numpointsnumpoints points in the current line style and color (just as drawpoly does), then fills the polygon using the current fill pattern and fill color. polypointspolypoints points to a sequence of (numpoints * 2) integers. Each pair of integers gives the x-x- and y-y-coordinatescoordinates of a point on the polygon.

Page 35: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

35

Fillpoly Examplevoid fillpoly (int numpoints, int *polypoints);

159.234

float W=35, H=15; int tankBody[8]; //top-left tankBody[0]=Xdev(tankX); tankBody[1]=Ydev(tankY+H); //top-right tankBody[2]=Xdev((tankX+W*cos((angle) * M_PI / 180.0))); tankBody[3]=Ydev((tankY+H+W*sin((angle) * M_PI / 180.0))); //bottom-right tankBody[4]=Xdev((tankX+W*cos((angle) * M_PI / 180.0)) + (H*sin(angle*M_PI/180.0))); tankBody[5]=Ydev((tankY+W*sin((angle) * M_PI / 180.0))); //bottom-left tankBody[6]=Xdev(tankX+(H*sin(angle * M_PI/180.0))); tankBody[7]=Ydev(tankY); setcolor(YELLOW); setfillstyle(SOLID_FILL,RED); fillpoly(4,tankBody);

Page 36: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

36

Pieslicevoid pieslice (int x, int y, int stangle, int endangle, int radius);

159.234

Use with setcolor() and setfillstyle() functions

Page 37: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

37

void drawWheel(float x, float y, float r, int a, directionType xDir){ a = a % 360; setcolor(BLACK); circle(Xdev(x),Ydev(y),Xdev(r)); setfillstyle(SOLID_FILL,RED); setlinestyle(SOLID_LINE,0,1); pieslice(Xdev(x),Ydev(y),a,a+30,Xdev(r)); setfillstyle(SOLID_FILL,GREEN); pieslice(Xdev(x),Ydev(y),a+180,a+30+180,Xdev(r));}

Page 38: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

38

Keyboard HandlingKeyboard HandlingMonitoring the Control and Shift keys:

if(GetAsyncKeyState(VK_CONTROLVK_CONTROL)<0) { ControlFlag = ! ControlFlag; }

bool ControlFlag, ShiftFlag;

if(GetAsyncKeyState(VK_SHIFTVK_SHIFT)<0) { ShiftFlag = ! ShiftFlag; }

For the Tank to Jump to the Right: Control + Shift + Right Arrow Control + Shift + Right Arrow key

For the Tank to Jump to the Left: Control + Shift + Left Arrow Control + Shift + Left Arrow key

Page 39: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

39

Keyboard HandlingKeyboard HandlingPossible approach in monitoring key combinations :

if(GetAsyncKeyState(VK_RIGHTVK_RIGHT)<0) {

XDir=RIGHT; if(ShiftFlagShiftFlag) {

outtext("SHIFT + RIGHT"); ShiftFlag=!ShiftFlag;ShiftFlag=!ShiftFlag; } if(ControlFlagControlFlag)

{ outtext("CTRL + RIGHT"); if (TankX < getmaxx()-W) TankX += 2; Angle=Angle-5; RaiseWheelFlag=TRUE; ControlFlag=!ControlFlag;ControlFlag=!ControlFlag; }

Page 40: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

40

Element of SurpriseElement of Surprise

rand()

159.234

generates a pseudorandom number - returns int

int RandomVal(int min, int max){

return (min + (rand() % ((max-min)+1) ));}

srand(time(NULL));

Page 41: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

41

Element of SurpriseElement of Surprise

srand()

159.234

Seed for random-number generation

Seed the random-number generator with current time so that  the numbers will be different every time we run.   srand( (unsigned)time( NULL ) );  

/* Display 10 numbers. */   for( i = 0;   i < 10;i++ )      printf( "  %6d\n", rand() );

Page 42: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

43

Element of SurpriseElement of Surprise

rand()

159.234

float RandomVal(float min, float max){

float r;

r = (float)rand()/RAND_MAX;r = min + (r*(max-min));

return r;}

rand()rand() returns a pseudo-random integral number in the range (0 to RAND_MAX)-1

Page 43: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

44

Time elapsed, wait…

clock()

159.234

void wait ( int seconds ){ clock_t clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {}}

clock_t clock_t startTime, elapsedTime;

startTime = clock();…...elapsedTime = (clock() - startTime)/CLOCKS_PER_SEC;

Page 44: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

Assign #2

45

Page 45: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

46

From nozzle to groundFrom nozzle to ground

The changes would guarantee that the complete trajectory would be plotted on screen (from t=0 to TimeOfFlight). The only thing left would be to account for the motion of the projectile until it hits the ground (EARTH_Y).

from t=0 to TimeOfFlight

While BulletY is less than EARTH_Y, let the bullet fall (t=t+tinc)

EARTH_Y

T=TimeOfFlight

t=0

Page 46: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

47

From nozzle to groundFrom nozzle to ground

The TimeOfFlight is computed only from the point of release to the same y location. However, we need to consider the case when the tank is on top of the ledge.

from t=0 to TimeOfFlight

(x1, y1)

(x1, y1)

T=TimeOfFlight

t=0

Page 47: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

48

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

Check to see if the tank’s center position falls within the safe landingzone of the ledge.

Page 48: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

49

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

But when can the tank possibly land on the ledge?

When it is falling! It could have jumped off another ledge,or jumped from the ground.

QQ

Flag variables will have to be created to monitor these cases.

Page 49: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

50

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

How do we know that the tank is falling?How do we know that the tank is falling?

If its PrevY position is less than its CurrentY position, orIf it’s (cx, cy) position goes out of the safe landing zone area (fell-off the ledge by accident!).

+Y

QQ

Flag variables will have to be created to monitor these cases.

Page 50: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

51

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

When do we allow a Tank to jump?When do we allow a Tank to jump?

If it’s currently on top of a ledgeon top of a ledge, or if it’s resting on the groundresting on the ground.

+Y+Y

Flag variables will have to be created to monitor these cases.

QQIf it’s not on the air yet!

Page 51: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

52

Jumping over the LedgeJumping over the Ledge

** You will have to check if the Tank can actually jump first, beforeallowing it to jump on screen. So even if the user presses the key combination that instructs the Tank to jump

e.g. (Ctrl+Shift+Left) to jump left, or (Ctrl+Shift+Right) to jump right

The tank should be coming from the ground or off the ledge, inorder to jump.

Page 52: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

53

Class LedgeClass Ledge

class Ledge {public:friend void CheckPlatform(Tank &tank, Ledge &ledge)private };

Drawing the LedgeMoving the LedgeCheckPlatform – friend function

The ledge class can be made to be a friend of class Tank. In turn, the ledge can carry (automatically adjust the Tank’sx & y positions, as well as compute the Tank’s new center of gravity) the Tank, once it has landed safely on top of it.

The Ledge is a flying ledge that can carry the Tank.

**

Page 53: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

54

Friend Functions (for Tank & Ledge)Friend Functions (for Tank & Ledge)

CheckPlatform – friend function

This is just one possible approach for solving the ‘Tank jump’ problem:

**

//tells the tank if it fell-off the ledge or if it’s still inside the safe zone//tells the Tank if it can jump or not//tells the Tank if it fell-off the ledge accidentally

Page 54: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

55

Class LedgeClass Ledge

class Tank {public:friend class Ledge;friend void CheckPlatform(Tank &tank, Ledge &ledge);private: };

Drawing the TankMoving the TankCheckPlatform – friend function

The Tank does not know whether or not it has landed on top of the Ledge.

Page 55: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

56

Common PitfallsCommon Pitfalls

Cannot click simulation window

Page 56: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

57

Common PitfallsCommon Pitfalls

Isolation of keyboard handling from Class Tank

Multiple key press signals are sent with one key press

//if(GetAsyncKeyState(VK_SPACE)<0) //continuous detection of the same key press

//if(GetAsyncKeyState(VK_SPACE) & 0x8000 == 0x8000)

if(GetAsyncKeyState(VK_SPACE) & 0x0001 == 0x0001)

Page 57: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

58

Common PitfallsCommon Pitfalls

Isolation of Transform functions

Page 58: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

59

Common PitfallsCommon Pitfalls

Cannot find the function move()

Page 59: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

60

Common PitfallsCommon Pitfalls

Which one to use?

Rock rock;

Rock* rock;

Rock* rock[8];

Rock** rock;

Page 60: 1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

61

Common PitfallsCommon Pitfalls

Some extra “blank space” is read when reading information from a file.

Program crashes when reading a text file, but no syntax error is reported by the compiler.