1 / 15

Lecture 13 Putting it all Together

Lecture 13 Putting it all Together. Key Features of King Pong. Animated Splash Screen Game Object Classes Game Play - Keyboard Controls Sound Effects Score Display Game End/New Game. Making a Splash Screen. 1. select image 2. reduce to 16 colors 3 . edit palette 4 . overlay text

george
Download Presentation

Lecture 13 Putting it all Together

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13 Putting it all Together

  2. Key Features of King Pong Animated Splash Screen Game Object Classes Game Play - Keyboard Controls Sound Effects Score Display Game End/New Game

  3. Making a Splash Screen 1. select image 2. reduce to 16 colors 3. edit palette 4. overlay text 5. convert to bmp A splash screen is the first image displayed when a game is started. In this example we modify a photographic by reducing the number of colors and then changing them using a bitmap editing tool (Paint Shop Pro). The overlayed text was added using Power Point. The image/text was then copied back into Paint Shop Pro to create a .bmp image.

  4. Splash Screen Animation The use of the ParticleEngine Demo code in the King Pong splash screen requires the use of conditional code segments in the Update( ) and Draw( ) methods. In XNA the update-draw game loop is hardwired. This is an artificial limitation imposed on the game developer by XNA. This issue is easily addressed by simply checking the program state (i.e. using bool started) to determine which code segments to execute.

  5. Update( ) Method (start == false) the bouncing particle on the splash screen is an instance of the ball class (same one used for the ping-pong ball in the game) same old wall bounce coding call to the particleEngine Update( ) method if (!start) { if (sb.Pos.X < 0 || sb.Pos.X > bkg.Width) sb.moveBall(true, false, 0); elseif (sb.Pos.Y < 0 || sb.Pos.Y > bkg.Height) sb.moveBall(false, true, 0); else sb.moveBall(false, false, 0); particleEngine.EmitterLocation = newVector2(sb.Pos.X, sb.Pos.Y); particleEngine.Update(); } else { if (countdown <= 0) { ballstuff(); crowdsounds(); } else { countdown -= 1; if (countdown == 0) gogame.Play(); } } The boolean variable start is set to false when King Pong begins. When the spacebar is pressed, start is set equal to true and the startup sound, rumble is disposed. Note that the spacebar is used to start a new game when the previous game is over (gameover = true). if (keybrd.IsKeyDown(Keys.Space)) { if (gameover && start) gamestart(); else { start = true; rumble.Dispose(); } }

  6. The Player Class classPlayer { protectedint maxV = 13; publicVector2 Pos; publicint Leng; privateint vY; publicint Score; public Player(int px, int py, int plen) { Pos.X = px; Pos.Y = py; vY = 0; Leng = plen; Score = 0; } publicvoid stopPaddle() { vY = 0; } publicint spin() { return vY/2; } } publicvoid movePaddle(int dir, int maxY) { if (dir > 0) { vY -= 1; if(vY<-maxV) vY=-maxV; Pos.Y += vY; if (Pos.Y < 0) { Pos.Y = 0; vY = 0; } } else { vY += 1; if(vY>maxV) vY=maxV; Pos.Y += vY; if (Pos.Y + Leng > maxY) { Pos.Y = maxY - Leng; vY = 0; } } } when player is moving, paddle speeds up until it reaches the maximum speed. paddle is stopped when it reaches the top or bottom of the game space The Player class manages the paddle location and velocity. The spin( ) method returns half the paddle velocity when called. This is used to transfer some of the paddle velocity to the ball when it is hit.

  7. The Ball Class classBall { publicVector2 Pos; publicint vX; publicint vY; public Ball(int x, int y, int vx, int vy) { Pos.X = x; Pos.Y = y; vX = vx; vY = vy; } publicvoid moveBall(bool hitpaddle, bool hitwall, int v) { if (hitpaddle) { vX = -vX; vY += v; } if (hitwall) vY = -vY; Pos.X += vX; Pos.Y += vY; } } The Ball class has properties that maintin the ball position and velocity. When the ball hits a paddle, some of the paddle Y velocity is transferred to the ball. The position of the ball is updated in the moveBall( ) method. When the ball hits the top or bottom (the walls) of the game space the y velocity vY has its sign flipped.

  8. Game Play For best performance it is important that no more than one event is triggered for each object in any one call to the Update( ) method. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); keybrd = Keyboard.GetState(); if ((!keybrd.IsKeyDown(Keys.A) && !keybrd.IsKeyDown(Keys.Z)) || (keybrd.IsKeyDown(Keys.A) && keybrd.IsKeyDown(Keys.Z))) p1.stopPaddle(); if ((!keybrd.IsKeyDown(Keys.K) && !keybrd.IsKeyDown(Keys.M)) || (keybrd.IsKeyDown(Keys.K) && keybrd.IsKeyDown(Keys.M))) p2.stopPaddle(); if(keybrd.IsKeyDown(Keys.A) && !keybrd.IsKeyDown(Keys.Z)) p1.movePaddle(1, bkg.Height); if(keybrd.IsKeyDown(Keys.Z) && !keybrd.IsKeyDown(Keys.A)) p1.movePaddle(0, bkg.Height); if(keybrd.IsKeyDown(Keys.M) && !keybrd.IsKeyDown(Keys.K)) p2.movePaddle(0, bkg.Height); if(keybrd.IsKeyDown(Keys.K) && !keybrd.IsKeyDown(Keys.M)) p2.movePaddle(1, bkg.Height); When player is pressing both or neither paddle move keys, the paddle is stopped immediately. player 1 is pressing the A key to move the paddle up player 1 is pressing the Z key to move the paddle down player 2 is pressing the K key to move the paddle up player 2 is pressing the M key to move the paddle down

  9. Update( ) Method (start == true) if (!start) { if (sb.Pos.X < 0 || sb.Pos.X > bkg.Width) sb.moveBall(true, false, 0); elseif (sb.Pos.Y < 0 || sb.Pos.Y > bkg.Height) sb.moveBall(false, true, 0); else sb.moveBall(false, false, 0); particleEngine.EmitterLocation = newVector2(sb.Pos.X, sb.Pos.Y); particleEngine.Update(); } else { if (countdown <= 0) { ballstuff(); crowdsounds(); } else { countdown -= 1; if (countdown == 0) gogame.Play(); } } At the start of the game the ball velocity is set to zero (0) and the integer countdown is set to 50. Countdown is decremented by one for each call to Update( ) until it reaches zero (0). When countdown == 0 the gogame sound (beep) is played. Once countdown reaches zero (0) the ballstuff( ) method is called each time Update( ) method is called.

  10. The ballstuff( ) Method publicvoid ballstuff() { if (b.Pos.Y <= 0 || b.Pos.Y >= bkg.Height - ball.Height) { b.moveBall(false, true, 0); } elseif ((Math.Abs(b.Pos.X - p1.Pos.X + ball.Width / 2) < ball.Height) && (b.Pos.Y + ball.Height > p1.Pos.Y) && (b.Pos.Y < p1.Pos.Y + p1.Leng)) { b.moveBall(true, false, p1.spin()); ping.Play(); volleycount += 1; } elseif ((Math.Abs(b.Pos.X - p2.Pos.X) < ball.Height) && (b.Pos.Y + ball.Height > p2.Pos.Y) && (b.Pos.Y < p2.Pos.Y + p2.Leng)) { b.moveBall(true, false, p2.spin()); pong.Play(); volleycount += 1; } else b.moveBall(false, false, 0); if (b.Pos.X < 0) { if (b.vY == 0) laugh.Play(); b.Pos.X = bkg.Width / 2; b.vX = -ballspeed; curballspeed = -ballspeed; b.vY = 0; b.Pos = StartPos; buzzer.Play(); volleystart(); p2.Score += 1; } ball has hit top or bottom of game space ball is touching player 1 paddle so p1 moveball is called to update ball position and transfer some of the paddle speed to the ball. The volleycount is incremented and the ping sound is played. ball is touching player 1 paddle so p1 moveball is called to update ball position and transfer some of the paddle speed to the ball. The volleycount is incremented and the pong sound is played. if (b.Pos.X > bkg.Width) { if (b.vY == 0) laugh.Play(); b.Pos.X = bkg.Width / 2; b.vX = ballspeed; curballspeed = ballspeed; b.vY = 0; b.Pos = StartPos; buzzer.Play(); volleystart(); p1.Score += 1; } } p1 misses ball p2 misses ball if paddle misses ball, score is updated, ball position, velocity, volleycount and countdown are all reset and the buzzer sound is played. If ball Y velocity is zero when paddle misses ball, the laugh sound is played.

  11. Ball Motion Controlled by moveball( ) Method moveball([ball hits paddle?],[ball hits wall],velocity to transfer to ball) moveball(true,false,p1.spin( )) p1.spin( ) = p1.vY/2 moveball(false,false,0) moveball(false,true,0)

  12. The Sound Files crowd[0] = Content.Load<SoundEffect>("crowd1"); crowd[1] = Content.Load<SoundEffect>("crowd2"); crowd[2] = Content.Load<SoundEffect>("crowd3"); crowd[3] = Content.Load<SoundEffect>("crowd4"); crowd[4] = Content.Load<SoundEffect>("crowd5"); cheer[0] = Content.Load<SoundEffect>("cheer1"); cheer[1] = Content.Load<SoundEffect>("cheer2"); cheer[2] = Content.Load<SoundEffect>("cheer3"); rumble = Content.Load<SoundEffect>("rumble"); rockyou = Content.Load<SoundEffect>("rockyou"); buzzer = Content.Load<SoundEffect>("buzzer"); gogame = Content.Load<SoundEffect>("goball"); ping = Content.Load<SoundEffect>("ping"); pong = Content.Load<SoundEffect>("pong"); laugh = Content.Load<SoundEffect>("laugh"); the SoundEffect array crowd[ ] is a set of 5 crowd sounds of increasing noise levels. the SoundEffect array cheer[ ] is a set of three different crowd cheering sound samples. "Let's get ready to rumble!....You ready for this?" "We will, we will rock you!" "Buzz!" "Beep!" "Ping" "Pong" Crazy Laughing Guy

  13. The crowdsounds( ) Method The volleycount increases as the play continues without a miss. The crowd noise is elevated at a volleycount of 0, 2, 4, 6, and 10. above a vollycount of 11 there is a 2% chance of one of the crowd sounds being played each time Update( ) is called. When the volleycount drops back to zero (0) we know that one of the players must have scored. This condition is used to initiate one or more of the three cheer sounds: cheer[0] if oldvolleycount > 8, + cheer[1] if oldvolleycount > 12, + cheer[2] if oldvolleycount > 16. publicvoid crowdsounds() { if (oldvolleycount != volleycount) { if (volleycount == 0) crowd[0].Play(); if ((p1.Score == 10 || p2.Score == 10) && volleycount == 1) rockyou.Play(); if (volleycount == 2) crowd[1].Play(); if (volleycount == 4) crowd[2].Play(); if (volleycount == 6) crowd[3].Play(); if (volleycount == 10) crowd[4].Play(); if (volleycount > 11 && rnd.Next() > 0.98) crowd[rnd.Next(0,4)].Play(); if (oldvolleycount > 8 && volleycount == 0) cheer[0].Play(); if (oldvolleycount > 12 && volleycount == 0) cheer[1].Play(); if (oldvolleycount > 16 && volleycount == 0) cheer[2].Play(); oldvolleycount = volleycount; } }

  14. Adding a New Sprite Font 1. Add a new item in KingPongContent 2. Select Sprite Font 3. Change size and style in XML file 4. Save with an appropriate name

  15. The Draw( ) Method Because of the way XNA hardwires repeated calls to the Update( ) Draw( ) methods we need to provide alternative paths of execution for the pre-game, game, and post-game sections of our game programs. protectedoverridevoid Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); if (!start) { spriteBatch.Draw(splash, splashRect, Color.White); particleEngine.Draw(spriteBatch); } else { spriteBatch.Draw(bkg, bkgRect, Color.White); spriteBatch.Draw(paddle, p1.Pos, Color.White); spriteBatch.Draw(paddle, p2.Pos, Color.White); spriteBatch.Draw(ball, b.Pos, Color.White); spriteBatch.DrawString(font, Convert.ToString(p1.Score), Score1Pos, Color.White); spriteBatch.DrawString(font, Convert.ToString(p2.Score), Score2Pos, Color.White); if(gameover) spriteBatch.DrawString(font2, "Press SpaceBar to Start New Game", newGameMsgPos, Color.White); } spriteBatch.End(); base.Draw(gameTime); } matches the if(!start) conditional in the Update( ) method. draw game space bkg image draw paddles draw ball display scores display Game Over message

More Related