Multimedia-Programmierung Übung 6

Similar documents
Multimedia-Programmierung Übung 5

Multimedia-Programmierung Übung 7

Multimedia-Programmierung Übung 5

Multimedia-Programmierung Übung 7

PyGame Sprites. an excellent turorial exists for PyGame sprites here kai.vm.bytemark.co.uk/ piman/writing/spritetutorial.shtml.

Sprites and collisions

slide 1 gaius Python Classes you can use theclass keyword to create your own classes here is a tiny example of a class

""" idea.py simplest possible pygame display demonstrates IDEA / ALTER model Andy Harris, 5/06 """

PyGame Unit ?

Video Games. Writing Games with Pygame

4! Programming with Animations

Introduction to Game Programming Lesson 4 Lecture Notes

PyGame Unit 4 OOP. 4.1 Object Oriented Programming. PyGame - Unit 4

Making things move. Time and Space

PYTHON NOTES (drawing.py and drawstuff.py)

Pyganim Documentation

There s already a ton of code written called libraries that we can use by importing. One that we use often is random

Multimedia-Programmierung Übung 7

Multimedia-Programmierung Übung 3

8! Programming with Sound

pygame Lecture #5 (Examples: fruitgame)

7! Programming with Animations

Multimedia-Programmierung Übung 3

2 Multimedia Programming with Python and Pygame

8 Physics Simulations

Multimedia-Programmierung Übung 1

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

CS 140 Final Exam Review Problems

Introduction to Game Programming Lesson 5 Lecture Notes, Part 1: The draw Module

a. Nothing. b. The game will run faster. c. The game will run slower. 5. What does this code do?

CHAPTER 1. Command Arguments Description Example. Prints something to the console. A value can be text in quotes or a variable name.

2 Development Platforms for Multimedia Programming

EXCODE. Code An App From Scratch

GETTING STARTED WITH RASPBERRY PI

Python Pygame: Mario movement. Python Pygame: Mario movement. Python Pygame: Mario movement. Mario on rails

pygame Lecture #2 (Examples: movingellipse, bouncingball, planets, bouncingballgravity)

Multimedia-Programmierung Übung 4

5! Programming with Sound

animation, and what interface elements the Flash editor contains to help you create and control your animation.

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Haup9ach)

Making use of other Applications

Computing Science. Advanced Higher Programming. Project 1 - Balloon Burst. Version 1. Software Design and Development. (using Python and Pygame)

OOP Fall 2016 Documentation

Animating the Page IN THIS CHAPTER. Timelines and Frames

We will start our journey into Processing with creating static images using commands available in Processing:

CS 1110 Final, December 17th, Question Points Score Total: 100

Summer 2012 Animation

4 Overview on Approaches to Multimedia Programming

Platform Games Drawing Sprites & Detecting Collisions

Chapter 7 Applets. Answers

append() function, 66 appending, 65, 97, 296 applications (apps; programs), defined, 2, 296

Pygame In a Few Minutes

4 Overview on Approaches to Multimedia Programming

the gamedesigninitiative at cornell university Lecture 17 Color and Textures

Smoother Graphics Taking Control of Painting the Screen

W: The LiveWires module

Making Your First Character

PSD to Mobile UI Tutorial

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

PixelSurface a dynamic world of pixels for Unity

GETTING STARTED WITH PYGAME ON THE RASPBERRY PI

8 th Grade Animation Exam Study Guide

CS 1110 Final, December 9th, Question Points Score Total: 100

Adobe Flash CS3 Reference Flash CS3 Application Window

clipbit Release 0.1 David Fraser

Adobe Animate Basics

Multimedia-Programmierung Übung 1

TOON BOOM HARMONY Paint Preferences Guide

Multimedia-Programmierung Übung 1

the gamedesigninitiative at cornell university Lecture 16 Color and Textures

Full Circle VIRTUALIZATION PT4. ISSUE #41 - September contents ^

Program a Game Engine from Scratch. Development Checkpoint #8. Sprite & Resource Manager

CS 1110 Final, December 8th, Question Points Score Total: 100

the gamedesigninitiative at cornell university Lecture 17 Color and Textures

CS 170 Java Programming 1. Week 9: Learning about Loops

Objects. say something to express one's disapproval of or disagreement with something.

HO-1: INTRODUCTION TO FIREWORKS

CISC 1600, Lab 3.1: Processing

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

To build shapes from scratch, use the tools are the far right of the top tool bar. These

COMP : Practical 9 ActionScript: Text and Input

Lesson 22 Enhancing Presentations with Multimedia Effects

CS 354R: Computer Game Technology

Introduction to Flash - Creating a Motion Tween

2 Development of multimedia applications

2 Development of multimedia applications

3dSprites. v

CS 1110 Final, December 8th, Question Points Score Total: 100

Adobe After Effects Tutorial

ACS-1805 Introduction to Programming (with App Inventor)

Chapter 9 Animation System

UI Elements. If you are not working in 2D mode, you need to change the texture type to Sprite (2D and UI)

7 Programming with Animations

the gamedesigninitiative at cornell university Lecture 12 2D Animation

Computer Graphics (CS 563) Lecture 4: Advanced Computer Graphics Image Based Effects: Part 1. Prof Emmanuel Agu

Creating an Animated Image in SFML Creating Packed Images in SFML

Tutorial: Overview. CHAPTER 2 Tutorial

Adobe Photoshop Sh S.K. Sublania and Sh. Naresh Chand

ADOBE TRAINING CS6 PHOTOSHOP BASICS: EDITING PHOTOS & WORKING WITH TEXT - 1

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Transcription:

Multimedia-Programmierung Übung 6 Ludwig-Maximilians-Universität München Sommersemester 2018 Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-1

Today Sprites, Sprite Groups and Sprite Animations Illustrated with + Literature: W. McGugan, Beginning Game Development with Python and Pygame, Apress 2007 ENGELBERT, Roger. Cocos2d-x by Example: Beginner's Guide. Packt Publishing Ltd, 2015. Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-2

Sprites in General a.k.a. Spooky things that move but are not really there Historically: something that is laid over the background implemented in hardware Today: anything that moves over the screen hardware fast enough -> sprites are now software-generated Background: Sprite: Screen: + = Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-3

Sprites in Pygame Module pygame.sprite provides basic classes and methods to handle sprites Class pygame.sprite.sprite used as base class for game objects Group Objects are provided as containers/lists for sprites Collision detection included (see next tutorial) http://www.pygame.org/docs/ref/sprite.html Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-4

The Sprite Class Sprite objects must contain an image and a location self.image is a Surface that contains the image information self.rect is a Rect object that determines the location of the sprite A subclass of Sprite should also overwrite the update() method Contains derived methods that handle the object in groups: kill() removes the sprite from all groups remove(*groups) removes the sprite from a list of groups add(*groups) adds the sprite to groups groups() returns a list of groups the sprite belongs to alive() tests whether the sprite belongs to any groups Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-5

Basic Sprite Example import pygame from pygame.locals import * class Box(pygame.sprite.Sprite): def init (self, color, initial_position): pygame.sprite.sprite. init (self) self.image = pygame.surface((20,20)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = initial_position call the superclass constructor define the image Surface define the rect def update(self): pass pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) box = Box((255,0,0),(0,0)) while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((0, 0, 0)) screen.blit(box.image,box.rect) pygame.display.update() blit the sprite Result: Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-6

Using the update Method Update can hold any number of arguments For efficient use of groups, sprites that do the same should have the same arguments class Box(pygame.sprite.Sprite): def init (self, color, initial_position): pygame.sprite.sprite. init (self) self.image = pygame.surface((20,20)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = initial_position self.speed = 300 def update(self, time_passed): moved_distance = time_passed * self.speed self.rect.left += moved_distance Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-7

Using the update Method II import pygame from pygame.locals import *... # Box Class here pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) box = Box((255,0,0),(0,0)) clock = pygame.time.clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((0, 0, 0)) time_passed = clock.tick() / 1000.0 box.update(time_passed) screen.blit(box.image,box.rect) pygame.display.update() update the sprite Result: Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-8

Using the update Method - Several Objects import pygame from pygame.locals import *... # Box Class here pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) box = Box((255,0,0),(0,0)) box2 = Box((0,255,0),(0,60)) box3 = Box((0,0,255),(0,120)) clock = pygame.time.clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((0, 0, 0)) time_passed = clock.tick() / 1000.0 box.update(time_passed) box2.update(time_passed) box3.update(time_passed) screen.blit(box.image,box.rect) screen.blit(box2.image,box2.rect) screen.blit(box3.image,box3.rect) pygame.display.update() too cumbersome Result: Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-9

Sprite Groups Sprite groups (e.g. pygame.sprite.group) are basically lists for sprites Handle the cumbersome details for the programmer: sprites() returns a list of the sprites in that group copy() returns a copy of the group add(*sprites) adds a sprite to the list remove(*sprites) removes the specified sprites from the list has(*sprites) determines whether all sprites are in this group update(*args) calls the update method of all sprites in this group (requires that they use the same arguments) draw(surface) draws all the sprites in this group to the specified surface (uses Sprite.image and Sprite.rect) clear(surface,background) erases the last drawn sprites from the list empty() removes all sprites from the list Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-10

Handling Complexity using Groups import pygame from pygame.locals import *... # Box Class here pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) boxes = ([(255,0,0),(0,0)],[(0,255,0),(0,60)],[(0,0,255),(0,120)]) sprites = pygame.sprite.group() for box in boxes: sprites.add(box(box[0],box[1])) create a group clock = pygame.time.clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((0, 0, 0)) time_passed = clock.tick() / 1000.0 sprites.update(time_passed) sprites.draw(screen) pygame.display.update() call update on the group draw all sprites in the group onto the screen surface Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-11

Advanced Groups (RenderUpdates) Drawing the whole screen every time a sprite moves is inefficient RenderUpdates helps to avoid this Special draw() method: draw(*sprites) returns a list of Rect objects that define the areas that have been changed Efficient for non-animated backgrounds Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-12

Using RenderUpdates import pygame from pygame.locals import *... # Box Class here pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) boxes = ([(255,0,0),(0,0)],[(0,255,0),(0,60)],[(0,0,255),(0,120)]) sprites = pygame.sprite.renderupdates() for box in boxes: sprites.add(box(box[0],box[1])) clock = pygame.time.clock() RenderUpdates group background = pygame.surface.surface((640,480)) background.fill((0,0,0)) screen.blit(background,(0,0)) while True:... QUIT procedure here time_passed = clock.tick() / 1000.0 sprites.update(time_passed) rects = sprites.draw(screen) pygame.display.update(rects) sprites.clear(screen,background) call draw and store the changed areas clear the changes done Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-13

Advanced Groups (OrderedUpdates) Remembers the order in which sprites are added Order is used for drawing the sprites to the screen Helps painting objects in the correct order Slower to add and remove sprites than other groups Iterating Sprite Groups sprites = pygame.sprite.group()... for sprite in sprites: print sprite Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-14

Sprites in Cocos2d-x Module CCSprite.h provides basic classes and methods to handle sprites Class Sprite::create() used as base class for game objects If rect is not specified, Cocos2d-x will automatically use the full width and height of the image file you specify. http://www.cocos2d-x.org/docs/programmersguide/3/index.html auto mysprite = Sprite::create("mysprite.png"); auto mysprite = Sprite::create("mysprite.png,Rect(0,0,200,200)); Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-15

Sprite Groups Grouping sprites may be required for tracking different object classes Add the two member variables to the header file These will store different sprite groups cocos2d::ccarray *_group1; cocos2d::ccarray *_group2; Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-16

Sprite Groups New sprites are added to the array Its tag is set to an individual identifier (e.g. 1) group1->settag(1); _group1->addobject(group1); group2->settag(2); _group2->addobject(group2); Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-17

Sprite Groups Now, you can identify the sprite groups and do something For example, delete objects after collision: if (sprite->gettag() == 1) // object1 { _object1->removeobject(sprite); } Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-18

Keyframe Animations (Recap) Keyframes are defined Intermediate steps are interpolated Basic interpolators/tweens/... built into many programming environments (e.g. CreateJS, JavaFX, Cocos2d-x) Examples: motion, color, shape BUT: PyGame does not provide built-in interpolators! Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-19

Sprite Animations Animations consist of frames that are displayed one after another Frame 1... Frame 7 Either imported as single graphics or with sprite sheets Single graphics are not recommended as loading each file takes time, switching between the files is expensive and sprites can't be optimized Graphic from http://shinylittlething.com/2009/07/21/pygame-and-animated-sprites/ Contains small tutorial on creating animations with sprite sheets in Pygame Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-20

Sprite Sheets & Spriting Sprite sheets contain all possible movements for a character If no meta data is provided, each Sprite should have the same size for easy slicing in software http://www.yoyogames.com/extras/resource/file/san1/90/90/male_character_outline_final.png http://www.themysticalforestzone.com/sprite_section.htm Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-21

Creating Sprite Sheets Editing with Photoshop, Gimp, Pixen etc. Pay attention to positioning of character and background color (should not appear in character) Tool support sprite sheet generation (see next slide) Pixen (Mac only) Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-22

Tools for Creating Sprite Sheets For example: https://zwopple.com/zwoptex/ https://www.codeandweb.com/texturepacker http://renderhjs.net/shoebox/ Optimize image size Remove transparent pixels Target multiple scales and fortmats Generate plist files https://zwopple.com/zwoptex / https://www.codeandweb.com/textu repacker Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-23

Slicing Sprite Sheets def load_sliced_sprites(self, w, h, filename): images = [] master_image = pygame.image.load(os.path.join('ressources', filename)).convert_alpha() master_image.set_colorkey((255,0,255)) master_width, master_height = master_image.get_size() for i in xrange(int(master_width/w)): images.append(master_image.subsurface((i*w,0,w,h))) set transparent color, background color of sprite sheet create subsurfaces return images More specialized slicing function may be needed due to incompatible sprite sheet (e.g. with borders) Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-24

First Sprite Animation 1 import os, pygame from pygame.locals import * def load_sliced_sprites(self, w, h, filename):... class BombWithAnimation(pygame.sprite.Sprite): def init (self, color, initial_position, fps): pygame.sprite.sprite. init (self) self.act_frame = 0 # create the images for the animation self.frames = load_sliced_sprites(20,20, explosed-sprite.png ) self.image = self.frames[0] self.rect = self.image.get_rect() self.rect.topleft = initial_position self.fps = fps self.change_time = 1.0/self.fps self.time = 0 def update(self, time_passed): self.time += time_passed if self.time >= self.change_time: self.act_frame = (self.act_frame + 1) % len(self.frames) self.image = self.frames[self.act_frame] self.time = 0 Remember current frame Based on the frames per second (fps) calculate the time needed for animation changes Frame changed? Change frame Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-25

First Sprite Animation 2... pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) bomb1 = BombWithAnimation((0,0),4) clock = pygame.time.clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((100, 200, 0)) time_passed = clock.tick() / 1000.0 bomb1.update(time_passed) screen.blit(bomb1.image,bomb1.rect) pygame.display.update() Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-26

Multiple Parallel Animations... pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) bomb1 = BombWithAnimation((0,0),4) bomb2 = BombWithAnimation((40,40),2) clock = pygame.time.clock() two bombs in two different framerates while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill((100, 200, 0)) time_passed = clock.tick() / 1000.0 bomb1.update(time_passed) screen.blit(bomb1.image,bomb1.rect) bomb2.update(time_passed) screen.blit(bomb2.image,bomb2.rect) pygame.display.update() Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-27

Sprite Sheets in Cocos2d-x Generate sprite sheet (and plist XML) Load sprite sheet in SpriteFrameCache for quick access The must be SpriteFrame is loaded only once // load the Sprite Sheet auto spritecache = SpriteFrameCache::getInstance(); // the.plist file can be generated with Cocos Studio or ShoeBox spritecache->addspriteframeswithfile("sprites.plist"); // load the Sprite Sheet auto mysprite = Sprite::createWithSpriteFrameName("mysprite.png"); Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-28

Polygone Sprites GPU has to process transparent and visible pixels Polygone sprites are made of a series of triangles This improves performance by saving pixels http://cocos2d-x.org/docs/programmers-guide/3/ 10285 Pixels 4089 Pixels Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-29

Polygone Sprites AutoPolygon processes an image into a 2d polygon mesh at runtime. // Generate polygon info automatically. auto pinfo = AutoPolygon::generatePolygon("filename.png"); // Create a sprite with polygon info. auto sprite = Sprite::create(pinfo); Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-30

Frame by Frame Animation with Sprite Sheets I Vector HelloWorld::getAnimation(const char *format, int count) { auto spritecache = SpriteFrameCache::getInstance(); Vector animframes; char str[100]; for(int i = 1; i <= count; i++) { sprintf(str, format, i); animframes.pushback(spritecache->getspriteframebyname(str)); } return animframes; } https://www.codeandweb.com/texturepacker/tutorials/animations-and-spritesheets-in-cocos2d-x Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-31

Frame by Frame Animation with Sprite Sheets II auto frames = getanimation("capguy/walk/%04d.png", 8); auto sprite = Sprite::createWithSpriteFrame(frames.front()); background->addchild(sprite); sprite->setposition(100,620); auto animation = Animation::createWithSpriteFrames(frames, 1.0f/8); sprite->runaction(repeatforever::create(animate::create(animation))); https://www.codeandweb.com/texturepacker/tutorials/animations-and-spritesheets-in-cocos2d-x Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-32

Useful Links http://www.pygame.org/docs http://www.cocos2d-x.org/wiki/sprites Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-33