PyGame - Unit 1 PyGame Unit 1 1.1 – 1.3 1.1 – Introduction to PyGame.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Introduction to Programming
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
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
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
SDL Programming Introduction. Initialization 2  The first thing you must do is initialize SDL  int SDL_Init( SDL_INIT_EVERYTHING )  This will return.
CIS 205—Web Design & Development Flash Chapter 1 Getting Started with Adobe Flash CS3.
Graphics and Multimedia Part #2
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Visual Basic .NET BASICS
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
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.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Algorithms Writing instructions in the order they should execute.
1 A first OpenGL program Brian Farrimond Robina Hetherington.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
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.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
3. Drawing 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.
Sprites (Images) and Sounds
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Catapult 2016.
Animations.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
9. Drawing.
Building Java Programs
Building Java Programs
10. User Input.

9. Drawing Let's Learn Python and Pygame
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Basic Graphics Drawing Shapes 1.
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
WICS - Flappy Anteater Python Tutorial
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Lecture 7: Introduction to Processing
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
Chapter 7 The Game Loop and Animation
Presentation transcript:

PyGame - Unit 1 PyGame Unit – – Introduction to PyGame

PyGame - Unit 1 Text Book for Python Module Making Games With Python and PyGame –By Al Swiegert Easily found on the Internet: – pygame /chaptershttp://inventwithpython.com/ pygame /chapters

PyGame - Unit 1 PyGame 1-1 Introduction to PyGame

PyGame - Unit 1 Your Wait is Over We are finally here. PyGame is fun. You won’t truly know whether or not you like programming until we dig into PyGame. Let’s get started!!!

PyGame - Unit 1 Objectives By the end of this unit you will be able to: –Download and Install PyGame –Describe and modify the PyGame version of Hello World. –Define the primary parts of the game loop: Events Game State Drawing on the screen

PyGame - Unit 1 Downloading PyGame PyGame Website – PyGame Download – PyGame Documentation – PyGame Tutorial –

PyGame - Unit 1 Hello World In PyGame 1. import pygame, sys 2. from pygame.locals import * pygame.init() 5. DISPLAYSURF = pygame.display.set_mode((400, 300)) 6. pygame.display.set_caption('Hello World!') 7. while True: # main game loop 8. for event in pygame.event.get(): 9. if event.type == QUIT: 10. pygame.quit() 11. sys.exit() 12. pygame.display.update()

PyGame - Unit 1 PyGame Window for Hello World

PyGame - Unit 1 Decomposing Hello World Breaking it Down

Import Statement (Hello World) 1. import pygame, sys –Importing the pygame and sys modules so we can use their functions. Normally to call a function in an imported module you use: moduleName.functionName() 2. from pygame.locals import * –Allows you skip the moduleName. PyGame - Unit 1

Hello World (initialize) 4. pygame.init() Must call pygame.init() after the imports to use PyGame. PyGame - Unit 1

PyGame.display.set_mode (Surface Object) DISPLAYSURF = pygame.display.set_mode((400, 300)) The generated pygame.surface object is stored in the DISPLAYSURF variable. All visible images must be placed on DISPLAYSURF This screen is: –400 pixels wide –300 pixels tall PyGame - Unit 1

PyGame.display.set_mode (Surface Object) DISPLAYSURF = pygame.display.set_mode((400, 300)) The set_mode() function must be called with a tuple –Must have the double parenthesis: ((400, 300)) –If you use single parenthesis, you will get an error like this: TypeError: argument 1 must be 2-item sequence, not int. PyGame - Unit 1

Hello World (Set the Caption) 6. pygame.display.set_caption('Hello!') “Hello!” will now display in the title bar of the game window. PyGame - Unit 1

Game State Game state - a set of values for all the variables in a game program. Examples include: –player’s health and position –the health and position of any enemies –which marks have been made on a board –Who’s turn it is PyGame - Unit 1

Hello World (Game Loops and Game States) 7. while True: # main game loop 8. for event in pygame.event.get(): Game Loop – Loops until the game ends. –Does three things: Handles Events Updates game state Draws the game state on the screen. Three things covered in next slides. PyGame - Unit 1

Hello World (Events) 8. for event in pygame.event.get(): Event – When users interact with a program, programs interact with each other, the passage of time, etc. pygame.event.get() –Returns a list of events that have occurred since the last time we called this function. PyGame - Unit 1

The QUIT Event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() QUIT is a PyGame constant The QUIT event type is generated when the user closes the screen. (Continued) PyGame - Unit 1

pygame.quit() function. for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() The pygame.quit() function stops the PyGame process. The sys.exit() function should always be called just after pygame.quit() PyGame - Unit 1

pygame.display.update() pygame.display.update() function - draws the Surface object returned by pygame.display.set_mode() to the screen –This object is in the DISPLAYSURF variable). Since the Surface object hasn’t changed, the same black image is redrawn to the screen each time pygame.display.update() is called. PyGame - Unit 1

Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. 7 – 13 pg1-1-1.py pg1-1-3.py

PyGame - Unit 1 PyGame 1-2 Pixels, Surface objects, Color objects, and Rect objects

PyGame - Unit 1 Objectives By the end of this unit you will be able to work with PyGame: –Surface objects. –Colors and transparency –Rect objects

Pixels Pixel – Little square dots on your computer screen. –Each pixel starts off as black but can be changed by the program. PyGame - Unit 1

Cartesian Coordinates Used to position items on the screen. Represented as (x, y) Have students identify the coordinates of specific squares on the image to the right. PyGame - Unit 1 Cartesian coordinates (Image courtesy of Al Sweigart “Making Games with Python and Pygame”)

The Surface Object Surface objects - objects that represent a rectangular 2D image. Display Surface - object returned by: pygame.display.set_mode() –Often your program will draw several different things to a Surface object. –Once you are done drawing everything on the display Surface object for a given loop iteration, it can be drawn to the screen using pygame.display.update() PyGame - Unit 1

Frames Frame – One iteration of the game loop. just like a still image on a paused DVD is called. Animation – A series of still images (frames) played in sequence to simulate movement. PyGame - Unit 1

Colors RGB – The amount of Red, Green and Blue in a given color. Produces a broad array of color possibilities. Values between 0 (no color) and 255 (fully saturated). –Red(255, 0, 0) –Green(0, 255, 0) –Blue(0, 0, 255) –White(255, 255, 255) –Black(0, 0, 0) –Purple(128, 0, 128) What do the parenthesis around the values mean??? PyGame - Unit 1

Transparent Colors (RGBA) Alpha Value - a measure of how opaque (not transparent) a color is. –Alpha Value of 255 = full intensity (0% transparent) –Alpha Value of 128 = half intensity (50% transparent) –Alpha Value of 64 = ¼ intensity (75% transparent) Tuple for Green - (0, 255, 0) –To make it fifty percent transparent, add a fourth integer: (0, 255, 0, 128) PyGame - Unit 1

Drawing Using Transparent Objects 1.Create a Surface object with the convert_alpha() method. –For example, the following code creates a Surface object in which transparent colors can be drawn: ALPHA_SURF = DISPLAYSURF.convert_alpha() 1. Once things have been drawn on the Surface object stored in ALPHA_SURF, then it can be ―blitted (copied) to DISPLAYSURF so it will appear on the screen. –Blitting is very important and discussed in greater detail later. PyGame - Unit 1

Pygame.Color Objects Allow you to create color objects. In this example myColor is a red object that is 50% transparent. myColor = pygame.Color(255, 0, 0, 128) The myColor object may be used as a function parameter elsewhere in the program. PyGame - Unit 1

Rect Objects Two ways to represent rectangular areas in PyGame: –A tuple of four integers (x, y, width, height) : 1. The x coordinate of the top left corner. 2. The y coordinate of the top left corner. 3. The width (in pixels) of the rectangle. 4. Then height (in pixels) of the rectangle. OR –The pygame.Rect object PyGame - Unit 1

The pygame.Rect Object #myRect = pygame.Rect(x, y, width, height) myRect = pygame.Rect(10, 20, 200, 300) print(myRect == (10, 20, 200, 300)) Returns: True PyGame - Unit 1

Rect Object (Automatically Calculated Coordinates) #myRect = pygame.Rect(x, y, width, height) myRect = pygame.Rect(10, 20, 200, 300) To retrieve the X coordinate of the right edge of the myRect: myRect.right Returns: (left edge) (width) = 210 Automatically calculates coordinates for other features of the rectangle. PyGame - Unit 1

Rect Object (Changing an Attribute) myRect = pygame.Rect(10, 20, 200, 300) If you reassign the right attribute, all the other attributes are automatically recalculated: myRect.right = 350 print(myRect.left) Returns:150 myRect is 200 pixels wide. –350 (new right edge) – 200 (Width) = 150 (new left edge) PyGame - Unit 1

Rect Object (Attributes) AttributeReturns or Sets myRect.bottom The int value of the Y-coordinate of the bottom side. myRect.centerx The int value of the X-coordinate of the center of the rectangle. myRect.width The int value of the width of the rectangle. myRect.topleft A tuple of two ints: (left, top) myRect.midleft A tuple of two ints: (left, centery) PyGame - Unit 1

Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. 13 – 20 Hello World: –pg1-2-1.py –pg1-2-2.py Rect Object: –pg1-2-3.py

PyGame - Unit 1 PyGame 1-3 Primitive Drawing Functions

PyGame - Unit 1 Objectives By the end of this lesson you will be able to: –Draw the following shapes using the PyGame primitive drawing functions: Polygons Rectangles Circles Ellipses –Color individual pixels using the pygame.PixelArray object.

Drawing a Line pygame.draw.line(surface, color, start_point, end_point, width) pygame.draw.line(DISPLAYSURF, BLUE, (60, 60), (120, 60), 4) PyGame - Unit 1

Drawing a Circle pygame.draw.circle(surface, color, center_point, radius, width) pygame.draw.circle(DISPLAYSURF, BLUE, (100, 50), 20, 0) PyGame - Unit 1

Drawing a Rectangle pygame.draw.rect(surface, color, rectangle_tuple, width) pygame.draw.rect(DISPLAYSURF, RED, (50, 25, 100, 50), 5) PyGame - Unit 1

Drawing an Ellipse pygame.draw.ellipse(surface, color, bounding_rectangle, width) pygame.draw.ellipse(DISPLAYSURF, RED, (50, 20, 80, 40), 3) PyGame - Unit 1 Note: The ellipse fits inside the rectangle.

Drawing a Polygon pygame.draw.polygon(surface, color, pointlist, width) pygame.draw.polygon(DISPLAYSURF, GREEN, ( (146, 0),(291, 106),(236, 277),(56,277), (0, 106))) PyGame - Unit 1 The last coordinate is unnecessary. A line is automatically drawn from the last point to the first

PyGame - Unit 1 Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. 13 – 24 Primitive Drawing Functions: –pg1-3-1.py –pg1-3-2.py