Python 7 Levels, L6, Unit 60, Billiards Mini Game, I

Powered By EmbedPress

Exercise:

import pygame
class Ball(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed

    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 38 or self.rect.right > width - 38:
            self.speed[0] = -self.speed[0]
        if self.rect.top < 38 or self.rect.bottom > height - 38:
            self.speed[1] = -self.speed[1]   
        
pygame.init()

size = width,height = 855,470
screen = pygame.display.set_mode(size)
bg = pygame.image.load("desk.jpg")
screen.blit(bg,[0,0])
ball_1 = Ball("ball.png",[427,235],[10,-8])
screen.blit(ball_1.image,ball_1.rect)
group1 = pygame.sprite.Group()
goal_1 = Ball("goal.png",[40,48],[0,0])
goal_2 = Ball("goal.png",[418,42],[0,0])
goal_3 = Ball("goal.png",[792,42],[0,0])
goal_4 = Ball("goal.png",[40,402],[0,0])
goal_5 = Ball("goal.png",[418,410],[0,0])
goal_6 = Ball("goal.png",[788,402],[0,0])
group1.add(goal_1)
group1.add(goal_2)
group1.add(goal_3)
group1.add(goal_4)
group1.add(goal_5)
group1.add(goal_6)
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    ball_1.move()
    if pygame.sprite.spritecollide(ball_1,group1,True):
        pass
    screen.blit(ball_1.image,ball_1.rect)
    if len(group1) == 0:
        break
    for goal in group1:
        screen.blit(goal.image,goal.rect)
    pygame.display.update()
    pygame.time.delay(20)
    screen.blit(bg,[0,0])
pygame.quit()

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts