Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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()
2.
3. 4.
5.
6.
7.