# Create a simple box sprite # Draw three instances of the sprite # We store the box instances # Task: add a 4th box somewhere # Task: make all boxes be 30x30 import sys, os, pygame from pygame.locals import * class Box(pygame.sprite.Sprite): def __init__(self, color, initial_position): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([15, 15]) self.image.fill(color) # Make our top-left corner the passed-in location. self.rect = self.image.get_rect() self.rect.topleft = initial_position pygame.init() screen = pygame.display.set_mode([150, 150]) boxes = [] # create a list of box sprites boxes.append(Box([255, 0, 0], [0, 0])) # Make a red box in the top left boxes.append(Box([0, 255, 0], [0, 60])) # Make a green box mid-screen boxes.append(Box([0, 0, 255], [0, 120])) # Make a blue box at the bottom of the creen for b in boxes: # loop over the boxes screen.blit(b.image, b.rect) # blit each pygame.display.flip() # update the screen def input(events): for event in events: if event.type == QUIT: return False return True going = True while going: going = input(pygame.event.get()) pygame.quit()