Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.

Similar presentations


Presentation on theme: "The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems."— Presentation transcript:

1 The Stingray Example Program CMT3311

2 Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems including: –game loop structuring – XNA does this –basic datatype specs for sprites and images –sprite movement –behaviour of game objects –collision detection – etc...

3 Example 2D game We will look at –Requirements for game –How to model game state with a finite state machine –Graphic requirements –Specification of classes –Modelling of movement –Collision detection

4 Stingray design Concept - game that mixes features of asteroids and frogger – you only get a powerup if there are more than 5 stars alive - leads to distinct gameplay Theme - from Stingray Sketches, some ideas on resources needed ‘Storyboard’ design of modes of game

5 Requirements There will be a player controlled ship, stingray, which rotates and moves and fire bullets Enemies will fly about the screen and if they collide with stingray, stingray will lose a life Stingray can kill enemies with bullets A powerup will randomly appear on screen for a few seconds (if 5+ enemies are left) and gives lives if picked up The number of enemies will increase at each level and score recorded Explosions should be generated appropriately There should be introduction, inter level, and gameover screens

6 Finite state machime We will model the program as a Finite State Machine (FSM) A FSM can only be in one state at a time A FSM may transition between states A FSM can be drawn with circles for states and directed line for transitions A FSM can be translated into code

7 FSM of Stingray game

8 A FSM gives us an overall control structure – a top down approach The states are defined by an enumeration type

9 FSM in Code Both the Update and Draw functions will now implement the FSM by if statements If(gamestate==START){... } If(gamestate==PLAYING){... } If(gamestate==PREPARELEVEL){... } If(gamestate==GAMEOVER){... } And the game exits when escape is pressed

10 Transitions Transitions between states are coded in Some transitions depend on internal logic Others depend on user input e.g. moving from PREPARELEVEL to PLAYING

11 Graphics requirements A Set of sprites will be needed In this version of the program it was decided to not have bitmaps for backgrounds so that the window size can be resized easily The coding supported this, the screen width and height are set in the GameConstants file

12 Image sizes and format Aim to have your game in a 640x480 window - could be bigger or smaller though... Sprite size will be related to window size, you can scale though Powers of two are good eg: 64*64, 8*8 Various tools like Paintshop Pro/Photoshop will allow you to change the colour depth and size of sprites

13 Stingray sprites Stingray Bullet Explosion Enemy Powerup

14 Classes From the requirements a two classes were designed –Class Ship to model the stingray –Class PowerUp to model the powerup Two utility classes were also planned –Class GameConstants to store global game constants in one place –Class Helper to store any useful little functions that don’t fit in other classes

15 Classes The explosion, enemy and bullet objects all need more than one instance An arbitrary decision was made to implement the basic objects as structs and build a manager class for each type of struct which manages an array of them Leading to the structs: explosion, enemy and bullet And the classes ExplosionManager, EnemyManager and BulletManager

16 Class Ship Gets user input and responds to it while playing, rotating, moving and firing bullets Arbitrarily is responsible for doing collision detection and response with enemies and the powerup Has four functions Initialise() Reset() and Draw() are trival, Update() is more interesting though

17 Movement Its easy to move in eight directions (rotation by 45deg intervals) How do we do it? What if we want to move in any direction?

18 2D Vectors and points are both specified by 2 numbers

19 Unit or Normalised Vector Has a length of 1 From Pythagorus sqrt(x*x+y*y) = length =1 Unit vectors are often convenient to use in many applications But we may want to use other vector eg. A velocity vector may define both orientation in the usual way and describe speed by the length

20 Tracking orientation If we know the sprite’s starting orientation vector... Then when we rotate the sprite we can work out its current orientation by rotating the starting vector by the same amount

21 Tracking orientation

22 Class Ship – class variables

23 Rotating vectors in Update() Implements 2D rotation formula in update –x’=x*cos(theta)- y*sin(theta) and –y’= x*cos(theta)+y*sin(theta) Note how the cos and sin functions are accessed via the Math class Also note the casting going on – C# is very fussy about types …

24 Moving in the direction you are pointing If you know the direction you are pointing it is very easy to move forward This basic model is use to move all moving sprites including bullets and enemies

25 Fire bullet When the ship fires a bullet it send its position and direction

26 Collision detection with enemies

27 Collision response When ship and enemy collide a large explosion it initiated The controller is also vibrated. You have to be careful to manage vibration. It is attenuated by the Update function. It is zeroed in the Game1 function when lives fall to zero or the level is completed – otherwise it can be left vibrating when it shouldn’t be.

28 Ship Class The update function also does collision detection with the powerUp

29 Struct PowerUp The PowerUp struct is pretty straightforward. In the update function the powerup will spawn itself randomly If its already alive it will fade transparency to a certain level and then kill off the PowerUp

30 PowerUp Class – Update()

31 Manager classes The manager classes are similar they all have an array of structs to manage They all have constructor that initialise their arrays and associate the right texture with struxt They all have loops that update and draw their structs In the following we only note interesting points of these classes – see the source code for full examples

32 Struct Enemy This struct has an initialise function that gives the struct a random unit vector for direction a random starting position by one of the four corners of the screen a random speed

33 Struct Enemy

34 Class EnemyManager Just manages the array of enemies One interesting feature is that the distance an enemy moves is scaled by the time since it was last called This is official MS advice but I do not always or consistently implement it – you are not required to either but its not a bad thing if you do

35 The basic formula where factor is an arbitrary scale factor is: X’ = X+((X.direction*speed)*factor*timeDelta); Eg in EnemyManager.Update() it is:

36 Bullet struct The bullet struct is unremarkable It has similar variables to stingray and enemy for movement

37 BulletManager This class is straight forward The interesting thing is has to do is collision detection and response between bullets and enemies – shown next

38 BulletManager Update()

39 Explosion effect The explosion effect implemented is very simple The sprite is rotated and scaled and made increasingly transparent over time The scale factor is varied to ditinguish between a big explosion (and enemy hitting the ship) and a little on (a bullet hitting an enemy)

40 CreateExplosions

41 Explosion.Update()

42


Download ppt "The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems."

Similar presentations


Ads by Google