Python 7 Levels, L6, Unit 61, Billiards Mini Game, II

Powered By EmbedPress

Exercise:

import pygame
from random import choice
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()
clock = pygame.time.Clock()
size = width,height = 855,470
screen = pygame.display.set_mode(size)
bg = pygame.image.load("desk.jpg")
screen.blit(bg,[0,0])
group2 = pygame.sprite.Group()
rand_speed = [16,14,12,10,8,6,-16,-14,-12,-10,-8,-6]
ball_1 = Ball("ball.png",[427,235],[choice(rand_speed),choice(rand_speed)])
ball_2 = Ball("ball2.png",[227,235],[choice(rand_speed),choice(rand_speed)])
group2.add(ball_1)
group2.add(ball_2)
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()

times = 0#record number of loop cycles
ball2_move_flag = False
game_run_flag = True
score = 0#record points
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if game_run_flag:
        times += 1
        if times == 100:
            if ball_1.speed[0] > 0:
                ball_1.speed[0] -= 2
            elif ball_1.speed[0] < 0:
                ball_1.speed[0] += 2
            if ball_1.speed[1] > 0:
                ball_1.speed[1] -= 2
            elif ball_1.speed[1] < 0:
                ball_1.speed[1] += 2
                
            if ball_2.speed[0] > 0:
                ball_2.speed[0] -= 2
            elif ball_2.speed[0] < 0:
                ball_2.speed[0] += 2
            if ball_2.speed[1] > 0:
                ball_2.speed[1] -= 2
            elif ball_2.speed[1] < 0:
                ball_2.speed[1] += 2
            times = 0
            if ball_1.speed[0] == 0 and ball_1.speed[1] == 0:
                if ball2_move_flag == False:
                    game_run_flag = False
                elif ball_2.speed[0] == 0 and ball_2.speed[1] == 0:
                    game_run_flag = False            
        ball_1.move()
        if ball2_move_flag == True:
            ball_2.move()
        if pygame.sprite.collide_rect(ball_1,ball_2):
            ball2_move_flag = True
            rand = choice([1,2,3])
            if rand == 1:
                ball_1.speed[0] = -ball_1.speed[0]
                ball_1.speed[1] = -ball_1.speed[1]
                ball_2.speed[0] = -ball_2.speed[0]
                ball_2.speed[1] = -ball_2.speed[1]
            elif rand == 2:
                ball_1.speed[0] = -ball_1.speed[0]
                ball_2.speed[0] = -ball_2.speed[0]
            else:
                ball_1.speed[1] = -ball_1.speed[1]
                ball_2.speed[1] = -ball_2.speed[1]
        if pygame.sprite.groupcollide(group2,group1,False,True,None):
            score += 1 
            pass
        screen.blit(ball_1.image,ball_1.rect)
        screen.blit(ball_2.image,ball_2.rect)
        if len(group1) == 0:
            break
        for goal in group1:
            screen.blit(goal.image,goal.rect)
        final_text = "score: "+str(score)
        ft_font = pygame.font.Font(None,50)
        ft_surf = ft_font.render(final_text,1,(255,0,0))
        screen.blit(ft_surf,[100,40])
    else:
        final_text = "Your final score is: "+str(score)
        ft_font = pygame.font.Font(None,50)
        ft_surf = ft_font.render(final_text,1,(255,0,0))
        screen.blit(ft_surf,[280,235])
    pygame.display.update()
    #pygame.time.delay(20)
    clock.tick(30)
    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