Sprites A sprite is a 2D image or animation that is integrated into a larger scene. Originally, sprites were created by special hardware that would super-impose.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Python Objects and Classes
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
How do games work? Game Workshop July 4, Parts Sprites/pictures Map/background Music/sounds Player character Enemies Objects.
Road Map Introduction to object oriented programming. Classes
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
Pygame Dick Steflik.
Guide to Programming with Python
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Lua & Love2D Game Engine GALAGUH
AI & 2D Development COSC 315 Fall 2014 Bridget M. Blodgett.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Developing the Game User Interface (UI) Lesson 5.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 23 Game Graphics II.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
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.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●
Game Maker Terminology
High Performance Java Swing Animation David Wallace Croft Presented to the Plano Java Users Group Plano, TX Copyright 2004 David Wallace Croft.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Guide to Programming with Python Week 15 Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Bubble sort Please use speaker notes for additional information!
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Simple Collision Detection By David Yan Under the direction of Professor Susan Rodger and Chari Distler Duke University, June 2015.
XNA ● Proprietary Microsoft framework ● C#. Interface.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.
PyGame - Unit 2 PyGame Unit – – Animation.
PyGame - Unit 4 PyGame Unit – Object Oriented Programming OOP.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
GAME:IT Mario Creating Platform Games Level 4 with GML Game Maker Language (GML) allows users more flexibility in game design. GML is similar to how real.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Review Scene Management (Scene, Layer, Director) Sounds Menus Sprites & Actions.
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.
Sprites (Images) and Sounds
Sound and more Animations
Modern Programming Tools And Techniques-I
Graphical Output Basic Images.
MOM! Phineas and Ferb are … Aims:
Catapult 2016.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Week 8 Classes and Objects
Keyboard Input.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Review Scene Management (Scene, Layer, Director) Sounds Menus
Creating and Deleting Instances Access to Attributes and Methods
13. Sprites Let's Learn Python and Pygame
Variables ICS2O.
Coding Concepts (Sub- Programs)
Game Loop Update & Draw.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Collision Detection Platforms.
This presentation document has been prepared by Vault Intelligence Limited (“Vault") and is intended for off line demonstration, presentation and educational.
Chapter 7 The Game Loop and Animation
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

Sprites A sprite is a 2D image or animation that is integrated into a larger scene. Originally, sprites were created by special hardware that would super-impose one video source on top of another. This was done because computers were too slow to redraw the entire screen every time an object moved.

The pygame.sprite Module This module contains several simple classes to be used within games. The use of these classes is entirely optional when using Pygame. The Sprite class is intended to be used as a base class for the different types of objects in the game. It can't really be used on its own. There are several Group classes that help store sprites as well as simply operations such as updating and drawing sprites. This module also contains several collision functions. These help find sprites inside multiple groups that have intersecting bounding rectangles.

Sprite = pygame.sprite.Sprite(*groups) Base class for visible game objects. The initializer can accept any number of Group instances to be added to. pygame.sprite.Sprite.update — method to control sprite behavior pygame.sprite.Sprite.add add the sprite to groups pygame.sprite.Sprite.remove remove the sprite from groups pygame.sprite.Sprite.kill remove the Sprite from all Groups pygame.sprite.Sprite.alive does the sprite belong to any groups pygame.sprite.Sprite.groups list of Groups that contain this Sprite

pygame.sprite.Sprite() (cont.) A Sprite object is only useful if used with Group containers. Groups require a Sprite derived class to override the Sprite.update() method and have valid Sprite.image and Sprite.rect attributes. When subclassing the Sprite, be sure to call the base initializer before adding the Sprite to Groups.

pygame.sprite.Sprite() (cont.) class Block(pygame.sprite.Sprite): def __init__(self, width, height): # Call the parent class constructor pygame.sprite.Sprite.__init__(self) # Create or load an image. MUST be name <image> self.image = pygame.Surface(width, height) # Get the rectangle. MUST be named <rect> self.rect = self.image.get_rect() def update(): # Used to update the object as needed. self.rect.move_ip(1,1)

Group = pygame.sprite.Group(*sprites) Simple container class to hold and manage multiple Sprite objects. The constructor takes any number of Sprite arguments to add to the Group. The Sprites in the Group are not ordered, so drawing and iterating the Sprites is in no particular order. pygame.sprite.Group.sprites — list of the Sprites this Group contains pygame.sprite.Group.copy duplicate the Group pygame.sprite.Group.add add Sprites to this Group pygame.sprite.Group.remove remove Sprites from the Group pygame.sprite.Group.has test if a Group contains Sprites pygame.sprite.Group.update call the update method on contained Sprites pygame.sprite.Group.draw blit the Sprite images pygame.sprite.Group.clear draw a background over the Sprites pygame.sprite.Group.empty remove all Sprites

pygame.sprite.Group (cont.) Group.update() and Group.draw() are the standout methods of the Group class: Group.update(*args) will call the Sprite.update() method of all Sprites in the Group, provided the derived class has one defined. It will pass the argument is any to each Sprite. There is currently no way to retrieve a return value from Sprite.update(). Group.draw(Surface) draws each Sprite in the Group to the Surface argument, provided each Sprite object has a Sprite.image and a Sprite.rect attribute. The Group does not keep Sprites in any order, so the draw order is arbitrary, which may give undesired results.

Group Types The Group class has evolved since its creation. Here is a list of currently available Group types. pygame.sprite.Group — A container class to hold and manage multiple Sprite objects. pygame.sprite.GroupSingle Group container that holds a single sprite. pygame.sprite.RenderPlain Same as pygame.sprite.Group pygame.sprite.RenderClear pygame.sprite.RenderUpdates Group sub-class that tracks dirty updates. pygame.sprite.OrderedUpdates RenderUpdates sub-class that draws Sprites in order of addition. pygame.sprite.LayeredUpdates LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates. pygame.sprite.LayeredDirty LayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.

Sprite Collisions The Sprite module provides 3 helper methods to detect collisions between sprites: spritecollide(sprite, group, dokill, collided = None) spritecollideany(sprite, group, collided = None) groupcollide(group1, group2, dokill1, dokill2, collided = None)

pygame.sprite.spritecollide() spritecollide(sprite, group, dokill, collided=None) Returns a list containing all Sprites in a Group that intersect with another Sprite. Intersection is determined by comparing the Sprite.rect attribute of each Sprite. The dokill argument is a bool. If set to True, all Sprites that collide will be removed from the Group. …

The collided Argument (cont.) All three collision methods have an optional collided argument. It is a callback function used to calculate if two sprites are colliding. If collided is not passed, all sprites must have a sprite.rect value which is used to calculate the collision.

The collided Argument (cont.) The available callbacks are: pygame.sprite.collide_rect — Collision detection between two sprites, using rects. pygame.sprite.collide_rect_ratio Collision detection between two sprites, using rects scaled to a ratio. pygame.sprite.collide_circle Collision detection between two sprites, using circles. pygame.sprite.collide_circle_ratio Collision detection between two sprites, using circles scaled to a ratio. pygame.sprite.collide_mask Collision detection between two sprites, using masks.

pygame.sprite.groupcollide() groupcollide(group1, group2, dokill1, dokill2, collided=None) This will find collisions between all the Sprites in two groups. Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect. If either dokill argument is True, the colliding Sprites will be removed from their respective Group.