Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PyGame - Unit 1 PyGame Unit 1 1.1 – 1.3 1.1 – Introduction to PyGame."— Presentation transcript:

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

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

3 PyGame - Unit 1 PyGame 1-1 Introduction to PyGame

4 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!!!

5 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

6 PyGame - Unit 1 Downloading PyGame PyGame Website –http://www.pygame.orghttp://www.pygame.org PyGame Download –http://www.pygame.org/download.shtmlhttp://www.pygame.org/download.shtml PyGame Documentation –http://www.pygame.org/docs/http://www.pygame.org/docs/ PyGame Tutorial –http://www.pygame.org/wiki/tutorialshttp://www.pygame.org/wiki/tutorials

7 PyGame - Unit 1 Hello World In PyGame 1. import pygame, sys 2. from pygame.locals import * 3. 4. 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()

8 PyGame - Unit 1 PyGame Window for Hello World

9 PyGame - Unit 1 Decomposing Hello World Breaking it Down

10 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

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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

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

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

23 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

24 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

25 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”)

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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:210 10 (left edge) + 200 (width) = 210 Automatically calculates coordinates for other features of the rectangle. PyGame - Unit 1

35 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

36 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

37 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

38 PyGame - Unit 1 PyGame 1-3 Primitive Drawing Functions

39 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.

40 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

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

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

43 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.

44 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

45 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


Download ppt "PyGame - Unit 1 PyGame Unit 1 1.1 – 1.3 1.1 – Introduction to PyGame."

Similar presentations


Ads by Google