2 What is pyGame? A set of Python modules to make it easier to write games. –home page:http://pygame.org/http://pygame.org/ –documentation:http://pygame.org/docs/ref/http://pygame.org/docs/ref/

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Create a Simple Game in Scratch
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Create a Simple Game in Scratch
RAPTOR Syntax and Semantics By Lt Col Schorsch
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
GameMaker.  A lot of Different Definitions  Easier to Say What is NOT a Game  Movie is Not a Game  No Active Participation  Final Outcome is Fixed.
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.
SDL Programming Introduction. Initialization 2  The first thing you must do is initialize SDL  int SDL_Init( SDL_INIT_EVERYTHING )  This will return.
Introduction to Scratch!
Visual Basic .NET BASICS
Meet Pygame Noah Kantrowitz June 8, The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
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
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Game Maker – Getting Started What is Game Maker?.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
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.
1 Project designed and created by M. Shajith Kumar.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
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.
(More) Event handling. Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
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
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
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
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
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Scratch for Interactivity
Catapult 2016.
Animations.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
9. Drawing.
Flash Interface, Commands and Functions
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
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
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
Topics Graphical User Interfaces Using the tkinter Module
11. Animation.
Creating a Simple Game in Scratch
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
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:

2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation: PyGame is a package that is not part of the standard Python distribution, so if you do not already have it installed (i.e. import pygame fails), download and install a suitable version from pyGame helps you do the following and more: –Sophisticated 2-D graphics drawing functions –Deal with media (images, sound F/X, music) nicely –Respond to user input (keyboard, joystick, mouse) –Built-in classes to represent common game objects

3 pyGame at a glance pyGame consists of many modules of code to help you: cdromcursorsdisplaydrawevent fontimagejoystickkeymouse moviesndarraysurfarraytimetransform To use a given module, import it. For example: import pygame from pygame import * from pygame.display import *

4 The game loop The structure of the games we’ll consider always follows this fixed pattern:

5 Initializing pyGame To start off our game, we must pop up a graphical window. Calling display.set_mode creates a window. –The call returns an object of type Surface, which we will call screen. We can call methods on the screen later. –Calling display.set_caption sets the window's title. from pygame import * pygame.init() # starts up pyGame screen = display.set_mode(( width, height )) display.set_caption(" title ")... pygame.quit()

6 Pygame.sprites -a pygame module with basic game object classes Sprites:Simple base class for visible game objects –It is an object that can move about in a game, and has internal behavior and state of its own and contains Onscreen characters. –This module contains several simple classes to be used within games. –For example, a spaceship would be a sprite, the player would be a sprite, and bullets and bombs would all be sprites. collision detection: Seeing which pairs of sprites touch. event: An in-game action such as a mouse or key press. event loop: Many games have an overall loop that: –waits for events to occur, updates sprites, redraws screen Note : The use of these classes is entirely optional when using Pygame. The classes are fairly lightweight and only provide a starting place for the code that is common to most of the games.

7 PyGame Drawing Basics import pygame module import pygame pygame.init() Opening a Window #This opens a window of size 640,480, and stores it in a variable called screen screen = pygame.display.set_mode((640,480)) You have to pass this variable anytime you want to do something to the screen The screen –If your screen is of size 640,480: The point (0,0) is at the upper left hand corner of the screen. x coordinates increase from left to right, y coordinates increase from top to bottom So: –Upper right is (640,0) –Lower left is (0,480) –Lower right is (640,480)

8 PyGame Drawing Basics Updating the Screen Changes you make to the screen—e.g. filling it with color, or drawing on it— do not show up immediately! Instead, you have to call this function pygame.display.update() –This shows you all the drawing you did since the last call to pygame.display.update() it is all changes since the screen was created Why do you have to do this? –This implements a features called double buffering. Double buffering is a feature of PyGame that lets you make lots of changes to the screen, and then have them all show up together as a single frame. Otherwise, when there is fast animation, the screen would "flicker" and it would be annoying to people playing the game Colors –colorName = (r,g,b)n ame the color anything you like.(r=red,g=green,b=blue) –all three should be integers between 0 and 255, with 255 being brightest, and 0 being darkest Examples: red = (255,0,0) green = (0,255,0) blue = (0,0,255) darkBlue = (0,0,128) white = (255,255,255) black = (0,0,0) pink = (255,200,200)

9 PyGame Drawing Basics Drawing Remember that after you draw, you have to call pygame.display.update() before your changes show up. screen.fill(color)fills the entire screen with the given color e.g. screen.fill(white) pygame.draw.lines(screen, color, closed, pointlist, thickness) –draws a series of lines, connecting the points specified in pointlist –pointlist is a list of tuples, specifying a series of points, e.g. to draw a ‘V’ you might use [(100,100), (150,200), (200,100)], with closed = False –closed should be either True or False, indicating whether to connect the last point back to the first –thickness is the thickness of the line (in pixels). –Ex: pygame.draw.lines(screen, black, False, [(100,100), (150,200), (200,100)], 1)

10 PyGame Drawing Basics pygame.draw.rect(screen, color, (x,y,width,height), thickness) –draws a rectangle (x,y,width,height) is a Python tuple –x,y are the coordinates of the upper left hand corner –width, height are the width and height of the rectangle –thickness is the thickness of the line. If it is zero, the rectangle is filled pygame.draw.circle(screen, color, (x,y), radius, thickness) –draws a circle (x,y) is a Python tuple for the center, radius is the radius –thickness is the thickness of the line. If it is zero, the rectangle is filled pygame.draw.arc(screen, color, (x,y,width,height), start_angle, stop_angle, thickness) –draws an arc (portion of an ellipse) –(x,y,height,width) are the coordinates of a rectangle that the arc would fit inside if it were drawn all the way around –if height and width are equal, then the rectangle is a square, and the arc will be a portion of a circle –start_angle and stop_angle are the angle on the unit circle in radians (not degrees) where the arc stops and starts

11 Enabling the quit button To make the quit button work, we need two things: –An infinite loop, that goes around and around looking for events. –Some code that looks for a QUIT event, and handles that event Here is an infinite loop in Python: while (True): # do something –“#do something” will be done over and over again, until we exit the program. –What is it that we want to do over and over? Each time through the loop, we need to look for quit events and if we find one, we need to quit the program: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit();

12 Enabling the quit button –The call to pygame.event.get() returns a list of every event that has happened since we called pygame.init(), or since the last call to pygame.event.get(). –One by one, the variable event is set to each of those events. –For each one of those, we use an if test to see if that event is equal to pygame.QUIT which is a value that means that the user clicked the red X (or red Circle) to close the window –If the user did that, then do two things: Call pygame.quit() to shut down pygame (this includes closing the window) Call sys.exit() to shut down the program (this exits the infinite loop) We put this code inside a while (True): infinite loop so that we can do it over and over again—we have to keep doing it, because we don't know when the user will click the red X.

13 Example-1

14 Example-2 import pygame screen = pygame.display.set_mode((640, 480)) running = 1 while running: event = pygame.event.poll() if event.type == pygame.QUIT: running = 0 screen.fill((0, 0, 0)) pygame.draw.line(screen, (0, 0, 255), (0, 0), (639, 479)) pygame.draw.aaline(screen, (0, 0, 255), (639, 0), (0, 479)) pygame.display.flip() #aaline = antialiased line.Using anti-aliasing can make the line appear less jagged in some cases.

15 Screen Coordinates (blit) To position an object on the screen, we need to tell the blit() function where to put the image. In pygame we always pass positions as an (X,Y) coordinate. This represents the number of pixels to the right, and the number of pixels down to place the image. The top-left corner of a Surface is coordinate (0, 0). Moving to the right a little would be (10, 0), and then moving down just as much would be (10, 10). When blitting, the position argument represents where the topleft corner of the source should be placed on the destination. Basically, blit means to copy graphics from one image to another. A more formal definition is to copy an array of data to a bitmapped array destination. You can think of blit as just "assigning" pixels. Blitting assigns the color of pixels in our image.

16 Animation (making drawings move) To make a drawing move, –you just put code inside your infinite loop that erases the old drawing (e.g. screen.fill(white) or screen.fill(black)) –draws the new drawing (e.g. using function calls starting with pygame.draw) in a different place than it was before! –calls pygame.display.update() to make all the changes appear Remember: All the changes in between two to pygame.display.update() show up all at once as a new frame (just like a new frame in a movie), So, you can create the illusion of smooth motion while (True): # check for quit events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); screen.fill(white) # erase the screen # draw the updated picture updatePoints(points) # changes the location of the points pygame.draw.lines(screen,black,false,points,1) # redraw the points pygame.display.update() # update the screen

17 Making the motion go faster or slower There two ways to make the motion go faster or slower –Insert a call to pygame.time.delay(5) somewhere inside the loop—the number is the number of milliseconds to delay. –To use a clock: (better way). Put the clock near the start of the program, somewhere after pygame.init() but definitely outside the main loop clock = pygame.time.Clock() Inside the loop, put the code msElapsed = clock.tick(30) #where 30 is the frame rate you want (e.g. 30 frames per second) snow_example.py

18 Exercises Modify the program to draw the two lines in different colors. Write a program that draws three color bars in different colors. Take note of the height per bar, so that all three can fit and have some space for movement. Write a program that draws arc, circles, rectangles on the screen.