|
| 1 | +# Krishan Patel |
| 2 | + |
| 3 | +"""Chaper 18: Sprites and Collision Detection |
| 4 | +From Hello World! Computer Programming for Kids and Beginners |
| 5 | +Copyright Warren and Carter Sande, 2009-2013 |
| 6 | +""" |
| 7 | + |
| 8 | +# 17.1 Using sprites to put multiple ball images on the screen |
| 9 | +import sys |
| 10 | +import pygame |
| 11 | + |
| 12 | +# Defines ball subclass |
| 13 | +class Ball(pygame.sprite.Sprite): |
| 14 | + """Defines Ball subclass""" |
| 15 | + def __init__(self, image_file, loc): |
| 16 | + pygame.sprite.Sprite.__init__(self) |
| 17 | + self.image = pygame.image.load(image_file) |
| 18 | + self.rect = self.image.get_rect() |
| 19 | + self.rect.left, self.rect.top = loc |
| 20 | + |
| 21 | +# Sets window size |
| 22 | +size = width, height = 640, 480 |
| 23 | +screen = pygame.display.set_mode(size) |
| 24 | +screen.fill([255, 255, 255]) |
| 25 | +IMG_FILE = "images/beach_ball.png" |
| 26 | +balls = [] |
| 27 | +for row in range(0, 3): |
| 28 | + for column in range(0, 3): |
| 29 | + location = [column * 180 + 10, row * 180 + 10] |
| 30 | + ball = Ball(IMG_FILE, location) |
| 31 | + balls.append(ball) # Adds balls to a list |
| 32 | + |
| 33 | +for ball in balls: |
| 34 | + screen.blit(ball.image, ball.rect) |
| 35 | +pygame.display.flip() |
| 36 | + |
| 37 | +RUNNING = True |
| 38 | +while RUNNING: |
| 39 | + for event in pygame.event.get(): |
| 40 | + if event.type == pygame.QUIT: |
| 41 | + RUNNING = False |
| 42 | + sys.exit() |
| 43 | +pygame.quit() |
0 commit comments