Sound and more Animations

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
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.
Video Game Design Lesson 1. Game Designer Person involved in the development of a video game Person involved in the development of a video game Usually.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
INTRODUCTION TO SCRATCH. About Me Resources Scratch Website Learn Scratch Washington-Lee Computer.
Games and Simulations O-O Programming in Java The Walker School
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
Game Design Creating a game called PING Phase 3: Steps for building basic game.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Locally Edited Animations We will need 3 files to help get us started at
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
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.
Today’s lecture 2-Dimensional indexing Color Format Thread Synchronization within for- loops Shared Memory Tiling Review example programs Using Printf.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
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.
Game Maker Terminology
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
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.
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.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The.
Review Expressions and operators Iteration – while-loop – for-loop.
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.
PyGame - Unit 2 PyGame Unit – – Animation.
GAME:IT Junior Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a.
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.
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.
Building a rocks and spaceship game with Pygame Zero
Sprites (Images) and Sounds
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Sound Music & Sound Effects.
15. Media (sound effects, music, video)
Scratch for Interactivity
Animations.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)

Dayton Metro Library Computer Basics September 19, 2018
Computation as an Expressive Medium
13. Sprites Let's Learn Python and Pygame
Number and String Operations
8. Starting Pygame Let's Learn Python and Pygame
Dayton Metro Library Place photo here Computer Basics December 8, 2018.
Go to =>
Breakout in Greenfoot Barb Ericson
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Go to =>
Lecture 7: Introduction to Processing
Let’s begin our game!.
ICT Gaming Lesson 3.
SNAP CIRCUITS INSTRUCTIONS
Creating a Simple Game in Scratch
So you want to be a Game Designer
CoE Software Lab II 1. Pygame Intro , Semester 2,
Chapter 7 The Game Loop and Animation
Animation Translation.
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

Sound and more Animations

Playing Sounds are important in Gaming The Sound effects are good to play when the player takes damage, slashes a sword, or collects a coin. Games are better if they have background music (to set the tone) playing regardless of what was going on in the game.

How do we add Sounds? Pygame can only load one music file to play in the background at a time. To load a background music file, call the pygame.mixer.music.load() function and pass it a string argument of the sound file to load. This file can be WAV, MP3, or MIDI format.

How do we add Sounds?

How do we add Sounds? To begin playing the loaded sound file as the background music, call the pygame.mixer.music.play(-1, 0.0) function. The -1 argument makes the background music forever loop when it reaches the end of the sound file. If you set it to an integer 0 or larger, then the music will only loop that number of times instead of looping forever. The 0.0 means to start playing the sound file from the beginning. If you pass a larger integer or float, the music will begin playing that many seconds into the sound file. For example, if you pass 13.5 for the second parameter, the sound file will begin playing at the point 13.5 seconds in from the beginning.

Catanimation.py Lab Experiment with different sounds in your Catanimation.py program. Try adding sound bits as well as background music.

What is Next? More Animations

Animation.py

Animation.py In this program we have several different blocks bouncing off of the edges of the window. The blocks are different colors and sizes and move only in diagonal directions.

Animation.py In order to animate the blocks (that is, make them look like they are moving) we will move the blocks a few pixels over on each iteration through the game loop.

How the Animation Program works In this program, we will have three different colored blocks moving around and bouncing off the walls. In order to do this, we need to first consider exactly how we want the blocks to move.

Moving and Bouncing the Blocks Each block will move in one of four diagonal directions: Down and left Down and right Up and left Up and right. When the block hits the side of the window, we want it to "bounce" off the wall and move in a new diagonal direction. The blocks will bounce as shown in the picture.

Moving and Bouncing the Blocks The new direction that a block moves after it bounces depends on two things: Which direction it was moving before the bounce Which wall it bounced off

Moving and Bouncing the Blocks There are a total of eight possible ways a block can bounce - two different ways for each of the four walls. For example, if a block is moving down and right, and then bounces off of the bottom edge of the window, we want the block's new direction to be up and right.

Moving and Bouncing the Blocks We can represent the blocks with a Rect object to represent: The position and size of the block A tuple of three ints to represent the color of the block An integer to represent which of the four diagonal directions the block is currently moving.

Animation.py

Animation.py In this program the size of the window's width and height is used for more than just the call to set_mode(). We will use constant variables to make the program more readable. Remember, readability is for the benefit of the programmer, not the computer. If we ever want to change the size of the window, we only have to change lines 8 and 9.

Animation.py We will use the keys on the number pad of the keyboard to remind us which belongs to which direction. 1 is down and left 3 is down and right 7 is up and left 9 is up and right. However, it may be hard to remember this, so instead we will use constant variables instead of these integer values.

Animation.py We will use a constant variable to determine how fast the blocks should move. A value of 4 means that each block will move 4 pixels on each iteration through the game loop.

Setting up the Blocks We will set up a dictionary to be the data structure that represents each block. The dictionary will have the keys of: 'rect' (with a Rect object for a value) 'color' (with a tuple of 3 ints for value) 'dir' (with one of our direction constant variables for a value).

Setting up the Blocks blocks[0] is the dictionary b1 We will store these data structures in variables b1, b2, and b3 On line 31 we put all of these data structures in a list, and store the list in a variable named blocks. blocks[0] is the dictionary b1

The Game Loop Inside the game loop, we want to: Move all of the blocks around the screen in the direction that they are going. Bounce the block if they have hit a wall. Draw all of the blocks to the windowSurface . Call pygame.display.update() to draw the surface to the screen. Call pygame.event.get() to check for QUIT event.

The Game Loop Before we draw any of the blocks on the windowSurface surface, we want to fill the entire surface with black so that anything we previously drew on the surface is covered. Once we have blacked out the entire surface, we can redraw the blocks.

Moving Each Block We want to update the position of each block, so we must loop through the blocks list and perform the same code on each block's data structure. Inside the loop, we will refer to the current block as b so it will be easy to type.

Moving Each Block So if the direction of the block is either DOWNLEFT or DOWNRIGHT, we want to increase the top attribute. If the direction is UPLEFT or UPRIGHT, we want to decrease the top attribute. If the direction of the block is DOWNRIGHT or UPRIGHT, we want to increase the left attribute. If the direction is DOWNLEFT or UPLEFT, we want to decrease the left attribute.

Checking If The Block Has Bounced After we have moved the block, we want to check if the block has gone past the edge of the window. If it has, we want to "bounce" the block, which in the code means set a new value for the block's 'dir' key. When the direction is set, the block will move in the new direction on the next iteration of the game loop.

Checking If The Block Has Bounced We need to make this check for each of the 4 edges of the window.

Drawing the Blocks in the Window Now that we have moved the block (and set a new direction if the block has bounced off the window's edges), we want to draw it on the windowSurface surface. We can draw this using the pygame.draw.rect() function. This is the last line of the for loop. We want to run the moving, bouncing, and drawing code on each of the blocks stored in the blocks list, which is why we loop through each of them. Also, if we wanted to add new blocks or remove blocks from our program, we only have to modify the blocks list and the rest of the code still works.

Drawing the Window on the Screen The call to the time.sleep() function is there because the computer can move, bounce, and draw the blocks so fast that if the program ran at full speed, all the blocks would just look like a blur. This call to time.sleep() will stop the program for 20 milliseconds. There are 1000 milliseconds in a second, so 0.001 seconds equals 1millisecond and 0.02 equals 20 milliseconds.

What is Next? Collision Detection

Animation.py Lab Type in and test the animation. Comment line 90 and see how fast it goes. Then change time to 1.0. What happens? Comment “fill(black)” to see trail of rectangles Add at least 2 more different rectangles.