MOM! Phineas and Ferb are … Aims:

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Create a Simple Game in Scratch
Create a Simple Game in Scratch
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
A Christmas Scratch game
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
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
Introduction to TouchDevelop
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.
Game Maker Terminology
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Using Pro-Engineer to Create 3 Dimensional Shapes Kevin Manner Kevin Manner Tim Reynolds Tim Reynolds Thuy Tran Thuy Tran Vuong Nguyen Vuong Nguyen.
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.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Creating visual interfaces in python
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Open a new Flash File Action Script 2.0. Create a button like you did last lesson and name it Click to Play.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
GAME:IT Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a simple.
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.
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.
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.
Scratch Programming Cards
Sprites (Images) and Sounds
Sound and more Animations
Create a Halloween Computer Game in Scratch
Pixels, Colors and Shapes
Scratch for Interactivity
Catapult 2016.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
9. Drawing.
Flash Interface, Commands and Functions
10. User Input.
9. Drawing Let's Learn Python and Pygame
Let's Race! Typing on the Home Row
Scratch for Interactivity
MS PowerPoint 2010 Week 2.
File Handling Programming Guides.
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
14. Pong.
Items, Group Boxes, Check Boxes & Radio Buttons
Go to =>
Unit 11 – PowerPoint Interaction
More programming with "Processing"
Design Studies “Show Off” Project
Game Loop Update & Draw.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
Lecture 7: Introduction to Processing
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Creating a Simple Game in Scratch
Chapter 7 The Game Loop and Animation
Presentation transcript:

MOM! Phineas and Ferb are … Aims: Students to begin working with pygame. Students to understand working with graphics. ALL: Make a simple application using pygame. MOST: Understand how to do simple animation. SOME: Apply these skills to making a simple game.

We are going to start working with Pygame. Pygame is Python library that allows us to make animations and games. The first program we are going to look at simply draws a graphics window and changes the colour of the background when we click the mouse.

After we import pygame, we must get the pygame engine going using pygame.init(). Next we set up the screen. It's helpful to have the size of the window as a tuple as well as separate values for width and height. We use display.set_mode() to make a screen and then we make a pygame “rect” based on that. This “rect” object step makes it easier to control aspects of the screen. Finally we set a caption (similar to the titles we made for the GUI programs).

Here we define some colours. Each colour is a list of three values. The three numbers represent how much Red, Green and Blue we want. Each number can range from 0 to 255. Next we make a list of all the colours. We are going to cycle through this list in the main loop of the program. We will also need the length later on, so we put that in cLen. Finally, we initialise a counter (c), a “done” flag and another flag that will check if the mouse button has been released. As we know, Python will treat those zeros as “False”s.

The pygame application is basically just a “while” loop The pygame application is basically just a “while” loop. Each time through, we are going to check to see if the mouse has been pressed and then update the screen colour if we need to.

The “while” loop will keep running until done becomes “True” The “while” loop will keep running until done becomes “True”. First, we fill the screen with the colour that is item c of the list “colours”. Then pygame checks for any “events” that have happened during this cycle of the “while” loop. Then it walks over the list of events. If the user has clicked the cross in the top right of the window (the “pygame.QUIT” event type), we simply set “done” to True which will cause the “while” loop to stop.

If the user has clicked the (left) mousebutton and the “flag” is “False”, then we set the flag to “True” and increment c, using the modulus operator to make sure we never get a value that is greater than cLen. This will cause the screen colour to change when we get back to the start of the loop next time. The next event we check for is whether the mouse button has been released. When it has been released, we set the flag back to “False”, ready for the next press. This is important, if we do not to this, the program will change the window's colour several times for each mouse press.

We use pygame.display.flip() to update the display. The last line sits outside the “while” loop. This is important as it makes sure that pygame stops properly once the loop has finished. Task Type this in and have a play with the code. Try changing the size of the graphics window and its title. Then add some other colours and make sure they are displayed properly.

Our next program is going to be a simple “bouncing ball” demonstration. We will learn how to animate using pygame.

The first part is very similar to what we did last time. Set up a graphics window and define some colours.

The “ball” is actually going to be a red circle draws onto a black square. These lines set up some variables. The size is a tuple that specifies the square's height and width. We will use ballRad for the ball's radius. Finally the xSpeed and ySpeed will be how many pixels the ball moves right or left and up or down each time through the loop.

We need to make a pygame “Surface” object We need to make a pygame “Surface” object. We can draw our “ball” onto this and then “move” it around the screen. We also need to make a “rect” object based on the ball Surface, as this allows us to do useful things like know where its center is. Next we make a tuple that represents the center of the rect and use this to draw our “ball”. The pygame circle drawing method takes a surface, a colour, a pair of co-ordinates and a radius. Now we are almost ready to enter the main loop and get the ball moving.

This line makes the mouse pointer invisible in the pygame window. Here, we set done to “False” and enter the loop. To give the illusion of animation, we fill the screen with black each time through the loop and draw the “ball” in its updated position. You can also see the familiar block of code that allows the user to quit the program.

The rect we have created has a built-in move() method The rect we have created has a built-in move() method. We just pass in the “speed” values we defined earlier. Then we have two “if” clauses that flip the x and y speed values if the ball is heading off screen. Once again, the “rect” object is handy as it has built-in attributes like “top”, “bottom”, “left” and “right” that we can use here. Next, we “blit” the ball onto the sceen. We pass in the “Surface” (which has the ball drawn on it) and the “rect” (which defines its position). The “blit” command will draw the image on the screen, but it will only appear after we use flip().

Here's the entire loop, plus the pygame Here's the entire loop, plus the pygame.quit() command that you need to have outside of it.

Tasks: Once you have got the code running, experiment with changing the size of the ball and the window. Try some different values for the x and y speeds. Comment out the line that fills the screen with black and see what happens when you run the code.

Now we are going to explore how to make a simple bat and ball game. Once you have done this, you will be able to design your own games.

Here, we are just initialising some variables. We are going to set up a pygame clock so that you can control the speed at which your game runs. FPS will hold the value for “Frames Per Second”.

You have seen all this before ...

It's easier to make a bat than a ball It's easier to make a bat than a ball. Just make a rectangle and fill it with a colour. We need to get the “rect” so we can control it later. I've set the “colorkey” of the ball so that the black background of the square on which the ball is drawn stays transparent. We didn't need to do this in “bounce.py” as we were blitting the ball onto a black screen. The last line positions the bat's rect in the middle of the screen, 50 pixels from the bottom.

I want the program to say “Game Over I want the program to say “Game Over.” if the ball falls off the bottom of the screen. First I need to choose a font and a size. By passing in “None”, I just get the default font. Then I create my text object. I pass the message, “True” because I want anti-aliasing and the colour to the font object's render method. Then, as usual, I am going to need a “rect” object so that I can position my text on the screen. I set the rect's center to the center of the screen.

The only new thing here is the simple command that creates a pygame clock .

The pygame.mouse.get_pos() tells us the mouse's position as a pair of co-ordinates. We only need to use the x co-ordinate to calculate the new position for the center of the bat. As with the bouncy ball program, we add the x and y speeds to the ball's co-ordinates. Here I have used the left edge and the top rather than “centerx” and “centery”.

I have used very simple collision detection here. The first if statement checks whether the ball overlaps the bat along the horizontal plane. The second checks to see if the ball's bottom is at the same height as the top of the bat. If so, we reverse the ySpeed. There are more sophisticated ways to test for collision in pygame, but this is the simplest way to implement it. If you test the game a bit, you'll notice that the ball sometimes carries on when it looks as if it should have bounced.

In this game, we only bounce the ball if it hits the top, left or right walls. If it drops out the bottom, we display the “Game Over.” message.

The last few commands blit the bat and ball to the screen. The clock.tick() command only advances the program when the correct amount of time has passed. Then we update the display with flip(). As usual, the pygame.quit() command sits outside the “while” loop.

Tasks Add a score counter. Use the same process as for the “Game Over.” object to make your counter. Create a variable that gets one added to it every time the ball bounces off the bat. Update the text object and blit it to the the screen.