Sei sulla pagina 1di 3

Programming in Python: Graphics More advanced examples

Drawing picture using PyGame can be quite tedious. You can also make use of loops to generate interesting patterns and graphics. Here are some more advanced examples of graphics using PyGame. We have already looked at the first example in the previous lesson.
# pygame1.py # the basic template to draw a picture that is not moving import pygame pygame.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 window screen = pygame.display.set_mode(SIZE) pygame.draw.line(screen, RED, (0,0), (100,50)) pygame.display.flip() # # # # # # draw our "scene"

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) pygame.quit()

# pause for three seconds so we can see it # 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 stuff import 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) pygame.quit() # pause for three seconds so we can see it # 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.

# 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]) pygame.draw.rect(screen, BLUE, box) # box becomes a Rect object. type help(pygame.Rect) # 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, 600 screen = 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()

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 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 a program that draws your scene in python save as drawEx2.py Create a program that draws an attractive black and white chess board. Create a program that draws a simple 2-d pyramid of circles 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 Create a program that draws 100, 20 x 20 rectangles at random positions on the screen. save as drawEx5.py Create a program that draws 1000 filled circles of radius 3 at random positions within 200 pixels of the center of the screen. Also randomly determine the colour of each circle separately. save as drawEx6.py 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 save as drawEx3.py

2.

3. 4.

5.

6.

7.

Potrebbero piacerti anche