Download presentation
Presentation is loading. Please wait.
1
Game Loop Update & Draw
2
Heart of the Game Loop Most of the work in every video game stems from the Update command Update is made up of a series of operations, often held in other code blocks (subprograms) for readability purposes: Check for User Input (keyboard, mouse, etc.) If input is found, trigger any needed actions. For example, start a jump animation or pause the game Update Game Data (largest part with many pieces): Update locations for moving game elements like players, bullets, etc. Update animation frames for each element as needed Apply any physics (gravity, acceleration, etc.) Check for collisions Apply in game data changes, e.g. health, scores, game state, etc. Play any sounds that are requested from any of the previous update actions Not all games will need all of these Update operations
3
Draw the Results The Draw command is simply a visual representation of the data just updated. For example you could play a game of Snakes and Ladders without a board while the game simply told you in text what space number you were on. The graphics are the way we bring our games to life but they have no logic of their own. Draw simply displays what the data currently stores. If the data says a player’s score is 1000 points, then that is what is displayed. The only exception to the “no-logic” rule is game states. For example Draw will display something completely different in the Menu state than it will in the Play or Highscore states. Like Update there is a sequence of commands to ensure a standard result: Draw background images Draw middle ground images (characters, enemies, items, etc.) Draw foreground images (typically environment images for depth, bushes, clouds, etc.) Draw the Heads Up Display (HUD) (life bars, scores, inventory, overlaid menus, etc.)
4
The Java Game Engine Java is simply a programming language, as such, some languages are more difficult to make games in than others. Java is one of those more challenging ones. To aid in your game creation, your teachers have built a game engine that handles a lot of the complex operations for you. The most important of these is the Game Loop. When using this Game Engine and the premade Game project you will find a starting point. This starting point has a few sections of code you need to work with, which we will discuss in the next few slides
5
Global Variables Up until now we have asked you to keep your variables local whenever possible, however when building games and using a Game Loop, this is not always an option. Because a typical Game Loop runs at 60 FPS, If you were to define a new variable inside the Update command that means you will be redefining that variable 60 times per second needlessly. Therefore, if you will need a variable throughout the game, such as playerScore, it should be created as a global non-static variable: e.g. private int score = 0; Ideally, your global definitions will be placed just below this code block
6
The main Up until now, you have done all of your work in main
When building games you will only use this code block to setup the look of your game: Game Name: The text that will show in the window border Game Width: The width of the game screen, using the global variable screenWidth here will give you the whole width of the monitor Game Height: The height of the game screen, you can also use screenHeight FPS: The frame rate for your game
7
Load Content The LoadContent section has a straight-forward purpose.
It is a block of code that is executed only ONCE at the beginning of the Game Loop. This is where you will perform 2 actions: Load any media, images, sounds, video etc. Setup any starting data values like scores, game state, player locations, etc.
8
Update Update is one of the two code blocks that is part of the Game Loop, it is therefore repeated once for each frame of the Game Loop Keep this in mind and be aware that every action you do in here is executed A LOT. If your game slows down it is typically here you will need to look to solve the problem. Remember, this is where you will update all game components. However, note the highlighted area below. This is the code that calls the LoadContent code and should NOT be touched by yourself unless you truly know what you are doing. Also, notice the deltaTime variable, this holds the amount of time passed since the last Update, in milliseconds. This can be useful during animation and physics processing.
9
Draw Draw is the second and last code block that is executed as part of the Game Loop Again, remember that every command you put here is repeated for every frame of the game. Recall that only Drawing code should go here with the one exception of game state logic.
10
Conclusion Notice that you actually do not see the Game Loop in the program. This is because the logic of its execution exists elsewhere. In future lessons you will see that there are a number of components in the Game Engine to help you with everything from drawing lines to performing complex animation For now, know that there is one other tool that you will find helpful, the Helper. Inside the Helper library exists a number of premade items and commands for you, including a set of Colours, a random number generator and some Color creation commands to build your own custom colours. To use this functionality, in your program simply type Helper. including the dot and you will see all the options popup for you to choose from. E.g. Store a random number between 1 to 10 in the a variable, randomNum randomNum = Helper.RandomValue(1,10);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.