L L Line CSE 420 Computer Games Lecture #6 Game Foundations.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

AGD: 5. Game Arch.1 Objective o to discuss some of the main game architecture elements, rendering, and the game loop Animation and Games Development.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 19 Other Graphics Considerations Review.
Topics Introduction Hardware and Software How Computers Store Data
Visual Basic .NET BASICS
Meet Pygame Noah Kantrowitz June 8, The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,
CHAPTER TEN AUTHORING.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
1 Computer Graphics Week2 –Creating a Picture. Steps for creating a picture Creating a model Perform necessary transformation Lighting and rendering the.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
1 Perception and VR MONT 104S, Fall 2008 Lecture 21 More Graphics for VR.
Game Maker – Getting Started What is Game Maker?.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
CHAPTER 3 (P.49-53) Importing modules + pygame intro.
GAM666 – Introduction To Game Programming ● DirectDraw, the 2D component of DirectX, uses the term “surface” to mean an area of memory in which pixel data.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
Graphics in Python On entry: Run Python 2.78 from N: drive/computing and ICT VLE: Computing home page - check your feedback Success criteria: ●Understands.
Intro to Pygame Lecture 05. What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
13. Sprites. Outline 1.Game Things in Pygame (again) 2.The Pygame sprite Module 3.The Sprite Class 4.Groups of Sprites 5.Types of Collision Detection.
CPT 450 Computer Graphics 12th Lecture – Animation.
Sprites (Images) and Sounds
Sound and more Animations
Reading and Writing Image Files
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Working in the Forms Developer Environment
Scratch for Interactivity
Binary Notation and Intro to Computer Graphics
Catapult 2016.
Animations.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
Topics Introduction Hardware and Software How Computers Store Data
8. Installing Pygame
Keyboard Input.
9. Drawing.
Flash Interface, Commands and Functions
Game Loops + Part II Reference:
Computer presentation
Computer Programming I
CSC461 Lecture 8: Input Devices
10. User Input.
Application with Cross-Platform GUI
9. Drawing Let's Learn Python and Pygame
Understand Windows Forms Applications and Console-based Applications
Importing modules + pygame intro
13. Sprites Let's Learn Python and Pygame
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
Topics Introduction Hardware and Software How Computers Store Data
Operating Systems.
Game Loop Update & Draw.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Professor John Canny Spring 2004 March 5
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Computer Literacy BASICS
Using Animation and Multimedia
Professor John Canny Spring 2003 March 12
Creating a Simple Game in Scratch
CoE Software Lab II 1. Pygame Intro , Semester 2,
Presentation transcript:

L L Line CSE 420 Computer Games Lecture #6 Game Foundations

Objectives Understanding the need for a graphic toolkit such as SDL Understanding space and time in games Creating a general game loop Installing and using the pygame library Using the IDEA framework to set up a game overview Lecture #6 Game Foundations

Objectives (cont.) Using the ALTER framework to manage a game loop Handling screen displays Managing colors Moving objects on the screen Lecture #6 Game Foundations

Games and Time Most programs run more slowly than the underlying computer. Games are run as quickly as possible. This is demanding on the processor and graphics capabilities. Lecture #6 Game Foundations

The Importance of Frame Rate Frame rate is the speed at which the visual display updates. A faster frame rate leads to more fluid animation and is more computationally intensive. The goal is to have a fast, consistent frame rate. Lecture #6 Game Foundations

Games and Space Games are often run in different display modes than typical programs. Each dot is a pixel. 640 x 480 and 800 x 600 pixels are common screen resolutions. The pixels are often stretched to fill the entire screen. Games often use custom user interfaces. Lecture #6 Game Foundations

Introducing the Gaming Loop Most games have the same general programming structure. It begins with initialization (setting up the computer to display game-style information). It continues with a display loop. It terminates when the user indicates (directly or by losing) the end of the game. Lecture #6 Game Foundations

Setting Up Game Resources Most games involve multimedia resources: 2D images 3D models Background images Sound effects Resources are usually created in specialized authoring tools. Games are often constructed with temporary "placeholder" media. Lecture #6 Game Foundations

Creating the Game Entities 2D games are made up of objects that move around on the screen. The objects are usually created once at the beginning of the program. Objects may be introduced to the user much later during the game play. As much preparation as possible is done before the user begins playing the game. Lecture #6 Game Foundations

Building the (Almost) Endless Loop The game play itself occurs inside a loop. The loop repeats quickly and at a consistent rate. The loop ends when the user or game situation demands an end to the game. Lecture #6 Game Foundations

Frame Rate = Loop Speed The frame rate is dependant on the speed of the main loop. Realistic animation requires the loop to happen many times a second. If there’s too much computation, the game will slow down. Python games normally run at 30 FPS (frames per second). Lecture #6 Game Foundations

Getting Input from the User A game has to interact with the user; otherwise, it's an animation. Get input from the mouse, joystick, keyboard, or other devices. Discern user intention from these input devices. Get input from the system (passage of time, collision of objects). Lecture #6 Game Foundations

Updating Game Entities Change entity characteristics according to input: Speed and direction changes Damage or death of objects Boundary collisions (leaving the stage) Real change is done on invisible variables. Change will be reflected to the user via a visual update. Lecture #6 Game Foundations

Refreshing the Screen The visual display is the primary output to the user. Essentially you create a new image every frame. Erase (or partially erase) the previous position of each entity. Draw the entity in its new state or position. Display the new image to the screen. Lecture #6 Game Foundations

Introducing Graphics APIs Working with graphics and input technology is complex. APIs (Application Programming Interfaces) gather common tools. Popular Graphics APIs: DirectX (Windows 2D and 3D) OpenGL (open-source 3D) SDL (open-source 2D) Lecture #6 Game Foundations

SDL and Pygame SDL is free, powerful, multi-platform, and open-source. SDL is written in C, so it runs very quickly. Pygame is a Python wrapper to SDL. It allows everything you need for 2D gaming. Lecture #6 Game Foundations

Installing Pygame Download binaries from www.pygame.org. Our course textbook uses pygame 1.7 with Python 2.4.4. Other versions may not work in the same way. Lecture #6 Game Foundations

Primary Features of Pygame Display mechanism (screen) Surface object (images) Rect object (shapes) Input modules (key, mouse, joystick) Output modules (images, audio) Utility modules (time, transformation) Lecture #6 Game Foundations

IDEA / ALTER Framework Easy to remember Encapsulates main ideas of game loop IDEA - initialization ALTER - main loop Lecture #6 Game Foundations

IDEA Import and Initialize Display configuration Entities Action Lecture #6 Game Foundations

IDEA Code See idea.py #I - Import and initialize import pygame pygame.init() #D - Display configuration screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Hello, world!") #E - Entities (just background for now) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((0, 0, 255)) #A - Action (broken into ALTER steps) Lecture #6 Game Foundations

Initialize Import the pygame module. Run pygame's initialization routine. #I - Import and initialize import pygame pygame.init() Lecture #6 Game Foundations

Display Configure the display. Set the display mode. Set screen size. Set the window caption. #D - Display configuration screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Hello, world!") Lecture #6 Game Foundations

Entities Create game entities (backgrounds, images, sounds). The background is a Surface object. Same size as screen. Convert to pygame format. Fill to change color. #E - Entities (just background for now) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((0, 0, 255)) Lecture #6 Game Foundations

Action Set up the primary game loop. Action steps are detailed in the ALTER acronym Lecture #6 Game Foundations

ALTER Assign values to key variables Loop Timing Events Refresh display Lecture #6 Game Foundations

ALTER Code From idea.py #A - Assign values to key variables clock = pygame.time.Clock() keepGoing = True #L - Set up main loop while keepGoing: #T - Timer to set frame rate clock.tick(30) #E – Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False #R - Refresh display screen.blit(background, (0, 0)) pygame.display.flip() Lecture #6 Game Foundations

Assign Values Create a clock to manage frame rate. Set a Boolean sentry variable. Initialize keepGoing to True. #A - Assign values to key variables clock = pygame.time.Clock() keepGoing = True Lecture #6 Game Foundations

Main Loop Create a loop based on the keepGoing variable. For all code that’s executed, each frame goes inside this loop. #L - Set up main loop while keepGoing: Lecture #6 Game Foundations

Timing Call clock variable’s tick() method. The parameter sets the game’s maximum frame rate. 30 FPS is typical for Python games. #T - Timer to set frame rate clock.tick(30) Lecture #6 Game Foundations

Handle Events Get all events that happened in this frame with event.get(). If any of the events are QUIT events: Set keepGoing to False. This setting will end the loop on its next cycle. Catch other events (mouse, keyboard) here, as well. #Event Handling for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False Lecture #6 Game Foundations

Refresh Display Update the background. For now, blit the background onto the screen. Set the background position to (0, 0) to completely cover the screen. Use flip() to double-buffer. #R - Refresh display screen.blit(background, (0, 0)) pygame.display.flip() Lecture #6 Game Foundations

Blitting an Image blit is a shortening of bitblit, which is a type of binary block transfer. It’s a memory technique used to quickly copy a rectangular chunk of memory to another location. Blit functionality is built into modern graphics cards. Copying an image is blitting it to a new position. Lecture #6 Game Foundations

Double-Buffering Displaying graphics to screen is a slow process. It's faster to assemble graphics in memory, then blit the resulting image to display memory. Pygame uses the flip() method to copy the background image to the display memory. This process is called double-buffering. Double-buffering reduces flicker and improves animation quality. Lecture #6 Game Foundations

Colors in Pygame Pygame (like most computing applications) uses additive color models. Colors are determined by three numbers indicating the amount of red, green, and blue light in a pixel. Each value ranges between 0 and 255 (base 10) or between 0x00 and 0xFF (base 16). Lecture #6 Game Foundations

More on Color Web developers are often more comfortable with hex color schemes. See colorViewer.py for an interactive example of colors in Python. Look at the code to preview more complex pygame programs. Use arrows to pick a color. Use spacebar to switch between decimal and hex numbering. Lecture #6 Game Foundations

Moving an Object Begin with the IDEA/ALTER framework. Add a box entity. Add variables to describe box position. Inside the loop, manipulate variables. Redraw the background. Redraw the box in a new position. Lecture #6 Game Foundations

Creating the Box See moveBox.py. Begin with a copy of idea.py. The following code is added to the Entities section: #make a red 25 x 25 box box = pygame.Surface((25, 25)) box = box.convert() box.fill((255, 0, 0)) Lecture #6 Game Foundations

Setting Up Position Variables The box has variables to control its X and Y positions. The following code is also in the Entities section: # set up some box variables box_x = 0 box_y = 200 Lecture #6 Game Foundations

Moving the Box After checking events, update the box variables: Increase the box_x variable by five. This doesn't actually move the box! The actual motion happens in the refresh step. #Events for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False #modify box value box_x += 5 Lecture #6 Game Foundations

Checking for Boundaries In general, whenever you change a variable, think about boundary conditions. If the box leaves the right side of the screen, set its X value to 0. This setting causes the box to wrap around the screen. #check boundaries if box_x > screen.get_width(): box_x = 0 Lecture #6 Game Foundations

Refreshing the Screen Use the blit() method to draw the box on the screen. Use (updated) box_x and box_y variables to determine the box's position. To appear, the box must be drawn after the background. #Refresh screen screen.blit(background, (0, 0)) screen.blit(box, (box_x, box_y)) pygame.display.flip() Lecture #6 Game Foundations

Discussion Questions Why use an API such as SDL/pygame? In what ways are games approximations of normal space and time? Why learn a framework such as IDEA/ALTER? How do pygame colors relate to the Web-safe color palette? Why do modern graphics cards contain built-in bitblit functions? Lecture #6 Game Foundations

Summary You should now understand The need for a graphic toolkit such as SDL Space and time in games Creating a general game loop Installing and using the pygame library Using the IDEA framework to set up a game overview and the ALTER framework to manage a game loop Handling screen displays and managing colors Moving objects on the screen Lecture #6 Game Foundations

Drawing and Event Handling Next Lecture Drawing and Event Handling Lecture #6 Game Foundations

References Andy Harris, “Game Programming, The L Line, The Express Line to Learning”; Wiley, 2007 Lecture #6 Game Foundations