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.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

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.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Week 9 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 10 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
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.
Week 10 Writing Games with Pygame, continued Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Introduction to TouchDevelop
> 1 Diagrams in Word Faculty of Health Alan Grace.
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.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
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.
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.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
9. Media (sound effects, music, video) 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.
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Sprites (Images) and Sounds
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
15. Media (sound effects, music, video)
Scratch for Interactivity
Adapted from slides by Marty Stepp and Stuart Reges
Catapult 2016.
Animations.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Week 8 Classes and Objects
Keyboard Input.
9. Drawing.
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
Let’s Learn 10. Invaders Saengthong School, June – August 2016
13. Sprites Let's Learn Python and Pygame
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
16. Invaders.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
Lecture 7: Introduction to Processing
11. Animation.
Introduction to PowerPoint
Creating a Simple Game in Scratch
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
CSC 221: Introduction to Programming Fall 2018
CoE Software Lab II , Semester 2, Pong.
CoE Software Lab II 1. Pygame Intro , Semester 2,
Chapter 7 The Game Loop and Animation
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

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 6.beachBounce.py (again) 7.Finding Images for Sprites 2

 sprites: moving game characters / objects  collision detection: which sprites are touching?  event: a user action (e.g. mouse or key press), or computer change (e.g. clock tick)  game loop:  read new events  update sprites and game state  redraw game 1. Game Things in Pygame (again) 3

 The pygame.sprite module includes: 1.a Sprite class for creating Sprite objects  we will create game sprites by inheriting Sprite 2.a Group class for grouping sprites together  this makes it easier to test, update and draw many sprites at once 3.lots of collision detection functions  test if a sprite hits another sprite (or a group of sprites) 2. The Pygame sprite Module 4

 Every sprite object contains an image and a rectangle (which contains its (x,y) position, width and height).  Sprite includes many functions for adding the sprite to groups. 3. The Sprite Class 5 image rect Sprite object (x,y) w h other optional data a sprite is moved by changing the (x,y) value in rect

 Sprite image data is a Pygame surface  a surface can be created from a loaded picture, or by converting shapes (e.g. lines, circles, rect), or by converting text strings  Some useful surface functions: The image Data 6 Surface(( width, height )) makes new Surface of given size fill(( red, green, blue )) makes surface the given color (rgb 0-255) get_width(), get_height() returns the size of the surface get_rect() returns a Rect holding the (x,y), width, height of the surface

class BallSprite(pygame.sprite.Sprite): def __init__(self, fnm): super().__init__() self.image = pygame.image.load(fnm).convert_alpha() self.rect = self.image.get_rect() self.rect.center = [scrWidth/2, scrHeight/2] # start position of the ball # in center of window self.xStep, self.yStep = self.randomSteps() # step size and direction along each axis def randomSteps(self): # create a random +/- STEP pair x = STEP if random.random() > 0.5: x = -x y = STEP if random.random() > 0.5: y = -y return [x,y] 3.1. My BallSprite Class 7 image rect BallSprite object (x,y) w h xStep yStep xStep and yStep will be used to move the sprite scrWidth, scrHeight, STEP are globals – see later

def update(self): if pygame.sprite.spritecollideany(self, horizWalls): # change y-step direction at top and bottom sides self.yStep = -self.yStep if pygame.sprite.spritecollideany(self, vertWalls): # change x-step direction at left and right sides self.xStep = -self.xStep self.rect.x += self.xStep # move the ball horizontally self.rect.y += self.yStep # and vertically 8 horizWalls, vertWalls are globals – see later

class BlockSprite(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.Surface((width, height)) self.image.fill(BLACK) self.rect = self.image.get_rect() self.rect.topleft = (x, y) 3.2. My BlockSprite Class 9 image rect BlockSprite object (x,y) width height The image is a black rectangle.

 Sprite objects can be grouped together inside a Group object: # create wall sprites top = BlockSprite(0, 0, scrWidth, WALL_SIZE) bottom = BlockSprite(0, scrHeight-WALL_SIZE, scrWidth, WALL_SIZE) left = BlockSprite(0, 0, WALL_SIZE, scrHeight) right = BlockSprite(scrWidth-WALL_SIZE, 0, WALL_SIZE, scrHeight) horizWalls = pygame.sprite.Group(top, bottom) vertWalls = pygame.sprite.Group(left, right)  The sprites in a group can be tested, updated and drawn using functions: vertWalls.draw(screen) # draws both sprites (left, right) 4. Groups of Sprites 10

 The sprite module contains a few different ways to group sprites  beachBounce.py will use Group and OrderedUpdates  Group groups sprites in no order  OrderedUpdates groups sprites in order Different ways of Grouping 11

 Using rectangles  often too big, but fast to test  Using circles  how big should the circles be?  Using the images' non-transparent pixels  slow but most accurate 5. Types of Collision Detection 12

 All three approaches are in the sprite module:  pygame.sprite.collide_rect(sprite1, sprite2)  uses self.rect data in the Sprite objects  pygame.sprite.collide_circle(sprite1, sprite2)  requires self.radius data in the Sprite objects  pygame.sprite.collide_mask(sprite1, sprite2)  requires self.mask data in the Sprite objects  a mask is a black and white version of the sprite that shows its outline 13 imagemask

spritecollideany( sprite, group )  Returns True if sprite has collided with any sprite in the group spritecollide( sprite, group, kill )  Returns a list of all sprites in group that collide with sprite  If kill is True, a collision causes sprite to be deleted groupcollide( group1, group2, kill1, kill2 )  Returns list of all sprites in group1 that collide with group2 Collision Detection with Groups 14

def update(self): if pygame.sprite.spritecollideany(self, horizWalls): # change y-step direction at top and bottom sides self.yStep = -self.yStep if pygame.sprite.spritecollideany(self, vertWalls): # change x-step direction at left and right sides self.xStep = -self.xStep self.rect.x += self.xStep # move ball horizontally self.rect.y += self.yStep # and vertically See BlockSprite.update() 15

 The same bouncing ball example as before, but coded using sprites.  note the black "walls" around the sides of the window 6. beachBounce.py (again) 16

BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) WALL_SIZE = 10 STEP = 10 class BlockSprite(pygame.sprite.Sprite): # see slide 9 class BallSprite(pygame.sprite.Sprite): # see slides Code 17

# main pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(WHITE) pygame.display.set_caption("Bouncing Beachball") scrWidth, scrHeight = screen.get_size() # create wall sprites top = BlockSprite(0, 0, scrWidth, WALL_SIZE) bottom = BlockSprite(0, scrHeight-WALL_SIZE, scrWidth, WALL_SIZE) left = BlockSprite(0, 0, WALL_SIZE, scrHeight) right = BlockSprite(scrWidth-WALL_SIZE, 0, WALL_SIZE, scrHeight) horizWalls = pygame.sprite.Group(top, bottom) vertWalls = pygame.sprite.Group(left, right) ball = BallSprite('smallBall.png') # sprites = pygame.sprite.Group(top, bottom, left, right, ball) sprites = pygame.sprite.OrderedUpdates(top, bottom, left, right, ball) clock = pygame.time.Clock() 18

running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state ball.update() # redraw screen.fill(WHITE) sprites.draw(screen) # draws all 5 sprites pygame.display.update() pygame.quit() 19 Here is the benefit of using Sprite and Group – the game loop becomes very simple.

 You can draw your own using any paint program!  Some good sites:  Video Game Sprites:  Spriters Resource:  Open Game Art:  A great list of websites at "20 Best Free Art Resources For Game Developers":  20-best-free-art-resources-for-game-developers 7. Finding Images for Sprites 20