2 More PyGame

download 2 More PyGame

of 3

Transcript of 2 More PyGame

  • 8/13/2019 2 More PyGame

    1/3

    Programming in Python: Graphics

    More advanced examplesDrawing picture using PyGame can be quite tedious. You can also make useof loops to generate

    interesting patterns and graphics. Here are some more advanced examples of graphics using PyGame. Wehave already looked at the first example in the previous lesson.

    # pygame1.py# the basic template to draw a picture that is not moving

    import pygamepygame.init() # ALL pygame programs need to initialize the pygame engine

    # before they can use it

    # Defining a colour constant(variables we should not be changing)RED = (255, 0, 0) # we use ALL CAPS for constants so we remember not to change them

    SIZE = (800, 600) # Open a pygame windowscreen = pygame.display.set_mode(SIZE)

    pygame.draw.line(screen, RED, (0,0), (100,50)) # draw our "scene"

    pygame.display.flip() # pygame is designed for fast moving graphics if you# don't use page flipping you can get a flickery mess# basically Everything we draw is drawn in memory# when we call flip() that picture gets copied to the# screen

    pygame.time.wait(3000) # pause for three seconds so we can see itpygame.quit() # pygame likes to crash computers when you don't

    # quit()

    # pygame2.py# A simple modification to the template allows us to focus on just what# we care about (the drawing) and not get bogged down in all the pygame# system stuffimport pygame

    pygame.init()SIZE = (800, 600)screen = pygame.display.set_mode(SIZE)

    def drawScene(screen):RED = (255, 0, 0)pygame.draw.line(screen, RED, (0,0), (100,50))

    pygame.display.flip()

    drawScene(screen)

    pygame.time.wait(3000) # pause for three seconds so we can see itpygame.quit() # pygame likes to crash computers when you don't

    # quit()

    The next few examples all use the same template, so I will only show the drawScene. Do a saveAs to start

    the new file so you don't have to re-type everything.

  • 8/13/2019 2 More PyGame

    2/3

    # pygame3.py# basic drawing commands

    def drawScene(screen):BLUE = (0, 0, 255)WHITE = (255, 255, 255)SEA_GREEN = (67, 205, 128)

    screen.fill(WHITE)

    box = pygame.Rect([50,10,100,60]) # box becomes a Rect object. type help(pygame.Rect)pygame.draw.rect(screen, BLUE, box) # to see all of the methods

    box = box.move([200,0])pygame.draw.rect(screen, BLUE, box, 2) # Last parameter is width, if you don't

    # add it, it will be 0. If it is 0 it will fill

    pygame.draw.circle(screen, SEA_GREEN, (50, 200), 50)pygame.draw.arc(screen, SEA_GREEN, pygame.Rect([150, 150, 250, 250]), 0, 3.14)pygame.draw.line(screen, BLUE, (0,500), (800,500))

    pygame.display.flip()

    # pygame4.py# using a for loop to draw patterns

    from pygame import *

    init()size = width, height = 800, 600screen = display.set_mode(size)

    def drawScene(screen):for x in range(0,width,10):

    draw.line(screen, (0, 255, 0), (x,0), (x,height))display.flip()

    # pygame5.py# nested for loop to draw a grid pattern

    def drawScene(screen):OLIVE = (107, 142, 35)for x in range(20,width,40):

    for y in range(20,height,40):draw.circle(screen, OLIVE, (x,y), 18)

    display.flip()

    # pygame6.py# nested for loop to draw a grid pattern# creating colours "on the fly"

    def drawScene(screen):for x in range(20,width,40):

    for y in range(20,height,40):c = ((x*256)/width,(y*256)/height,0)draw.circle(screen, c, (x,y), 18)

    display.flip()

  • 8/13/2019 2 More PyGame

    3/3

    More Graphics Exercises

    1. Draw a pattern of lines from the center of the screen to every 10th

    pixel on the perimeter of the

    screen. save as drawEx1.py

    2. Use graph paper to draw a scene (e.g. your house.) Determine which python shapes you would

    need to draw this on the computer and write the important coordinates on the paper. Create aprogram that draws your scene in python save as drawEx2.py

    3. Create a program that draws an attractive black and white chess board. save as drawEx3.py

    4. Create a program that draws a simple 2-d pyramid ofcircles on the screen. The base should have 15 circles

    and yes this involves some math. This question is

    fairly hard. I expect you to plan it out on paper first.

    save as drawEx4.py

    5. Create a program that draws 100, 20 x 20 rectangles atrandom positions on the screen.

    save as drawEx5.py

    6. Create a program that draws 1000 filled circles of

    radius 3 at random positions within 200 pixels of thecenter of the screen. Also randomly determine the

    colour of each circle separately.

    save as drawEx6.py

    7. Draw two randomly placed radius 10 circles on the screen then draw radius 2 circles every twenty

    pixels from the center of one to the center of the other. save as drawEx7.py