9. Drawing.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Processing Lecture. 1 What is processing?
1 Cascading Style Sheets Continued Different kinds of selectors in a style sheet –Simple- Pseudo-Class –Contextual- Pseudo-Element –Class Image Styles.
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.
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
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Tkinter Canvas.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
GRAPHICS MODULE 14 STUDY BOOK. Graphic commands SCREEN - puts the screen into graphics mode WINDOW - allows scaling of the screen LINE - 3 formats –LINE.
By: Affaf Muzammal ALLIED SCHOOL 46-Iqbal Block, Ittefaq Town, Multan Road, Lahore ,
CHAPTER 3 (P.49-53) Importing modules + pygame intro.
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
Paint Tutorial Created February 2006 Start Paint: Start>Programs>Accessories>Paint.
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.
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
9. Media (sound effects, music, video) Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
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
MS Paint A simple drawing tool that can be used to create simple or elaborate drawings. These drawings can be either black-and-white or color, and can.
MOM! Phineas and Ferb are … Aims:
Introduction to Illustrator
Pixels, Colors and Shapes
15. Media (sound effects, music, video)
Catapult 2016.
Animations.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Learn Animations in Fireworks
Creating Vector Graphics
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
Basic Graphics Drawing Shapes 1.
Project title Scheduled dates Team members JAN FEB MAR APR MAY JUN
Importing modules + pygame intro
13. Sprites Let's Learn Python and Pygame
Graphics -- Introduction
Digital Media Dr. Jim Rowan ITEC 2110.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
WICS - Flappy Anteater Python Tutorial
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
11. Animation.
How to Start This PowerPoint® Tutorial
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
Working with images and scenes
CoE Software Lab II , Semester 2, Pong.
CoE Software Lab II 1. Pygame Intro , Semester 2,
JavaScript – Let’s Draw!
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

9. Drawing

Outline Screen Coordinates Lines Rectangles Circles, ellipses, arcs Polygons Text Images

1. Screen Coordinates The window is 640 pixels wide by 480 pixels high screen = pygame.display.set_mode((640,480)) The window is 640 pixels wide by 480 pixels high a pixel is a drawing square on the screen surface Pygame can create many surfaces which can all be drawn on for now, I'll use only the screen surface

Screen Coordinates The screen has pixel coordinates starting from (0,0) in the top-left, and moving across and down. x-axis (0,0) y-axis (320,240) (x, y) (639,479) (640-1,480-1)

Drawing with Python draw.py contains examples of: Separate and joined lines; normal and anti-aliased Rectangles (filled and unfilled) Circles Ellipses Arcs Polygons Text (system and custom) Images

draw.py

Top Level of draw.py drawStuff(screen) pygame.init() screen = pygame.display.set_mode((640, 480)) screen.fill(WHITE) pygame.display.set_caption("Drawing Examples") drawStuff(screen) clock = pygame.time.Clock() running = True while running: # game loop clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False pygame.display.update() pygame.quit()

def drawStuff(screen): # draw a green line pygame. draw def drawStuff(screen): # draw a green line pygame.draw.line(screen, GREEN, (0, 0), (100, 100), 5) # draw several red lines for y in range(0, 100, 10): pygame.draw.line(screen, RED, (0, 10+y), (100, 110+y), 5) :

Pygame.draw Module http://www.pygame.org/docs/ref/draw.html

(0,0) 2. Draw Separate Lines (100,100) Each line needs a drawing surface (the screen), a color, a start point, an end point, and line thickness. # draw a green line from (0,0) to (100,100); 5 pixels thick pygame.draw.line(screen, GREEN, (0, 0), (100, 100), 5) # draw red lines from (0,10) to (100,110); 5 pixels thick for y in range(0, 100, 10): pygame.draw.line(screen, RED, (0, 10+y), (100, 110+y), 5)

(370,110) Drawing Joined Lines (370,187) Each point is an (x,y) tuple inside a points tuple # draw thick red connected lines (that looks like "Hi") points = ( (370, 110), (370, 187), (372, 143), (411, 144), (412, 187), (412, 110), (412, 187), (432, 177), (436, 146), (433, 180) ) pygame.draw.lines(screen, RED, False, points, 3)

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). http://www.pygame.org/docs/ref/draw.html

Anti-Aliasing (smoothing) Second line (using anti-aliasing) seems much smoother. pygame.draw.line(screen, BLACK, (480, 425), (550, 325), 1) pygame.draw.aaline(screen, BLACK, (500, 425), (570, 325), 1) (550,325) (570,325) (480,425) (500,425)

A closer look at Anti-Aliasing Zoomed in:

3. Drawing a Rectangle Each rectangle needs a drawing surface (the screen), a color, a top-left point, width, height, and optional line thickness. thickness of 0 indicates a filled rectangle if no thickness given, 0 is used # draw a unfilled black rectangle; # top-left (x,y) = (20,20), (width,height) = (250,100) pygame.draw.rect(screen, BLACK, (20, 20, 250, 100), 2) (20,20) 250 100

3.1. Draw Random Rectangles randomRects.py draws 100 rectangles at random positions filled with random colors.

randomRect.py import pygame, sys, random from pygame.color import THECOLORS from pygame.locals import * pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(THECOLORS['white']) pygame.display.set_caption("Random Rectangles") for i in range (100): # pick random numbers for rectangle size and position width = random.randint(0, 250) height = random.randint(0, 100) top = random.randint(0, 400) left = random.randint(0, 500) # pick a random color by name color_name = random.choice( list(THECOLORS.keys()) ) color = THECOLORS[color_name] # draw the rectangle pygame.draw.rect(screen, color, (left, top, width, height)) # 0

No change to the game loop. clock = pygame.time.Clock() running = True while running: # game loop clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state (nothing yet) # redraw game pygame.display.update() pygame.quit()

3.2 Draw a Sine Curve sineRects.py draws a sine curve using small 1x1 rectangles look closely

sineRects.py import pygame, sys, math from pygame.locals import * BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(WHITE) pygame.display.set_caption("Sine Curve Drawn with Rectangles") for x in range(0, 640): y = int(math.sin(x/640.0 * 4*math.pi) * 200 + 240) # sine curve # draw using small rectangles pygame.draw.rect(screen, BLACK, (x, y, 1, 1), 1) # (x,y) is the loc of each rectangle; (width,height) == 1 clock = pygame.time.Clock() running = True while running: : # game loop unchanged

A Smoother Sine Curve sineLines.py draws a sine curve using connected lines.

sineLines.py import pygame, sys, math from pygame.locals import * BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(WHITE) pygame.display.set_caption("Sine Curve Drawn with Lines") coords = [] for x in range(0, 640): y = int(math.sin(x/640.0 * 4*math.pi) * 200 + 240) # sine curve coords.append((x, y)) # make a list of points pygame.draw.lines(screen, BLACK, False, coords, 1) # plot the points, joined together clock = pygame.time.Clock() running = True while running: : # game loop unchanged

4. Drawing a Circle Each circle needs a drawing surface (the screen), a color, a center point, radius, and optional line thickness. thickness of 0 indicates a filled circle # draw a filled blue circle at (340,60) radius 40 pygame.draw.circle(screen, BLUE, (340, 60), 40, 0) (340,60) 40

Drawing an Ellipse An ellipse is drawn inside a 'bounding box' (400,20) 150 Drawing an Ellipse 100 An ellipse is drawn inside a 'bounding box' Each ellipse needs a drawing surface (the screen), a color, a top-left point, width, height (for the box), and optional line thickness. thickness of 0 indicates a filled ellipse If the box is square, the ellipse will be a circle # draw a mustard-colored ellipse, # inside a rectangle defined as # top-left (x,y) = (400,20), (width,height) = (150,100) MUSTARD = (204, 204, 0) pygame.draw.ellipse(screen, MUSTARD, (400,20,150,100), 0)

Drawing an Arc An arc is a partially drawn ellipse (20,220) 250 Drawing an Arc π/2 angle 200 0 angle An arc is a partially drawn ellipse the ellipse is defined using a bounding box the arc of the ellipse is defined using a starting and ending point in radians #draw an arc pygame.draw.arc(screen, BLACK,(20,220,250,200),0, pi/2, 2)

Common Angles in Radians

draws an arc (portion of an ellipse) 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 starts and stops http://www.pygame.org/docs/ref/draw.html

Multiple Arcs 250 (20,220) 200 # draw fours arc to form an ellipse; # an arc has a bounding box, start angle, end angle, line width pygame.draw.arc(screen, BLACK,(20, 220, 250, 200), 0, pi/2,2) pygame.draw.arc(screen, GREEN,(20, 220, 250, 200), pi/2, pi,2) pygame.draw.arc(screen, BLUE, (20, 220, 250, 200), pi, 3*pi/2, 2) pygame.draw.arc(screen, RED, (20, 220, 250, 200), 3*pi/2, 2*pi,2) (20,220) 250 200

(332,319) (483,335) 5. Drawing a Polygon (237,372) (232,392) Each point is an (x,y) tuple inside a points tuple A polygon is always closed its last point connects back to the first # draw a green filled polygon points = ( (237, 372), (332, 319), (483, 335), (422, 389), (447, 432), (359, 379), (320, 439), (232, 392) ) pygame.draw.polygon(screen, GREEN, points, 0)

6. Text Drawing text takes 3 steps: set the font: the default font, a system font, or a custom font loaded from a "ttf" file Convert (render) the text into an image Draw (blit) the image onto the screen

Using the Default font (270,250) # 1. load default font; 48pt size font = pygame.font.Font(None, 48) # 2. render anti-aliased (smooth) black text as an image textImage = font.render("Hello World", True, BLACK) # 3. draw text image with its top-left corner at (270,250) screen.blit(textImage, (270, 250)) (270,250)

System Fonts System fonts are the ones in Windows (or the Mac, or Linux). you can print a list using print("All system fonts:", sorted(pygame.font.get_fonts())) may not be the same on different computers if a font is not present then the default font is used instead

Using MS Windows "Comic Sans" font # 1. load font; 48pt size, bold, not italic font = pygame.font.SysFont('comicsansms', 48, True, False) # 2. render anti-aliased black text as an image textImage = font.render("Hello World", True, BLACK) # 3. draw text image with its top-left corner at (270,250) screen.blit(textImage, (270, 250)) (270,250)

Using a Custom Font You can use a TrueType (ttf) font file many excellent free fonts are available online e.g. at http://www.1001freefonts.com/ Including a custom font in your game means that you know the right font will be used no matter what computer your game is on.

Using a Mexican font from http://www.1001freefonts.com/mexican-fonts.php # 1. load font file; 40pt size myFont = pygame.font.Font("GringoNights.ttf", 40) # 2. render anti-aliased blue text as an image labelImage = myFont.render("Python in the Wild West", True, BLUE) # 3. draw text image with its top-left corner at (200,210) screen.blit(labelImage, (200, 210)) (200,210)

7. Using an Image Prepare the image (jpg, bmp, gif, or png format) (70,245) 7. Using an Image Prepare the image (jpg, bmp, gif, or png format) use JPG for big photos use PNG for small photos with transparent parts use GIF for simple line drawings Put image file in same directory as program. Use pygame.image.load() and blit(): # load graphics; top-left at (70,245) image = pygame.image.load("saengthong.jpg").convert() screen.blit(image, (70,245))

Cars in a Snowy Field randomCars.py draws three random car images on top of a background image guess which is my car?

randomCars.py field.jpg import pygame, sys, random from pygame.locals import * pygame.init() screen = pygame.display.set_mode([795, 500]) # big enough for background image pygame.display.set_caption('Random Cars') # draw background image background = pygame.image.load("field.jpg").convert() screen.blit(background, (0,0)) :

# draw 3 random car images spaced out across the window x = 30 for i in range (3): id = random.randint(0, 6) # choose a number between 0-6 car = pygame.image.load("car" + str(id) + ".png").convert_alpha() # the images are all called car?.png, with ? = 0 to 6; # they have transparent backgrounds screen.blit(car, (x,420-car.get_height())) # base of images at y == 420 x += car.get_width() + 10 clock = pygame.time.Clock() running = True while running: : # game loop unchanged

Images with Transparent Parts The car images are PNG files with transparent backgrounds. This means they must be converted to Pygame surfaces using convert_alpha(): car = pygame.image.load("car0.png").convert_alpha() there is nothing in the background of the image car0.png

Pygame issues a warning (not an error) when using convert_alpha() on some types of PNG files it's because Pygame is using an old PNG library ignore the warning messages

Positioning the Cars The bottom of the cars line up at y == 420

The lining up requires a bit of maths since the position of an image is set by its top-left corner y = 420 – image's height screen.blit(car, (x,420-car.get_height())) (x, y) width height 420

The x value is also a bit tricky since the cars must be spaced out across the background without overlapping. calculate a new x value by adding the car image's width (plus a bit extra) x += car.get_width() + 10 (x, y) (old x, old y) width 10 420