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
done = False
Loops until the user click the close button
clock = pygame.time.Clock()
Manages how fast the screen updates
while done==False:
for event in pygame.event.get():
if event.type==pygame.QUIT:
done = true
This is the main program loop
screen.fill(white)
Fills the screen with white
pos = pygame.mouse.get_pos()
Gets the current mouse position and returns the positions as a list of two numbers
player. rect.x = pos[0]
player. rect.x = pos[1]
Fetches the x and y out of the list and sets the player object to the mouse location.
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
checks if the player is colliding with anything if so erases the block.
for block in blocks_hit_list:
score +=1
print( score )
Checks the list of collisions and increases the score
all_sprites_list.draw(screen)
Draws all of the sprites
clock.tick(20)
Sets the clock to 20 frames per second
pygame.display.flip()
Updates the screen with what is drawn
pygame.quit()
Used to avoid glitches in idle