Term 3 Study Guide Flashcards
black =( 0, 0, 0)
white =(255,255,255)
red =(255,0,0)
Defined some colors
Import pygame
Import random
Imports needed modules
class Block(pygame.sprite.Sprite):
Creates the block class - it derives from the Sprite class in Pygame
def __init__(self, color, width, height):
Constructs the color of the block and the width and height
pygame.sprite.Sprite.__init__(self)
Calls the parent class constructor
self. image = pygame.Surface([width, height])
self. image.fill(color)
Creates and image and fills it with color
self.rect = self.image.get_rect()
Fetches the rectangle object that has the dimensions of the image
pygame.init()
Initializes pygame
screen_width=700
screen_height=400
screen=pygame.display.set_mode([sceen_width,screen_height])
Sets the width and height of the screen
block_list = pygame.sprite.Group()
This is a list of ‘spites’. Each block in the program is added to this list. The list is managed by a class called ‘Group’.
all_sprites_list = pygame.sprite.Group()
This is a list of every sprite, including all blocks as well as the player block.
for i in range(50):
block = Block(black,20,15)
Uses a loop to produce 50 blocks
block. rect.x = random.randrange(screen_width)
block. rect.y = random.randrange(screen_height)
Sets a random location for the block
block_list.add(block)
all_sprites_list.add(block)
Add the block to the list of objects
player = Block(red, 20, 15)
all_sprites_list.add(player)
Creates a red player block