import pygame
from pygame.locals import *
from random import randint, choice
from functions import load_sound,load_image

class Bonus(pygame.sprite.Sprite):
    def __init__(self,freq,width,height,image):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image(image, -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.freq = freq
        
    def spawn(self,width,height):
        rog = randint(1,4)
        if rog == 1:
            self.rect.topleft = 10, 10+50
        elif rog == 2:
            self.rect.topleft = width-70, 10+50
        elif rog == 3:
            self.rect.topleft = width-70, height-70
        else:
            self.rect.topleft = 10, height-70
        rand1 = randint(0,1)
        rand2 = randint(0,1)
        if (rand1==0):
            rand1=-1
        if (rand2==0):
            rand2=-1
        self.movex = 3*rand1
        self.movey = 3*rand1
        
    def update(self):
        self._walk()
        
    def _walk(self):
        newpos = self.rect.move((self.movex, self.movey))
        if self.rect.left < self.area.left or \
            self.rect.right > self.area.right:
            self.movex = -self.movex
            newpos = self.rect.move((self.movex, self.movey))
            self.image = pygame.transform.flip(self.image, 1, 0)
        if self.rect.top < self.area.top+50 or \
            self.rect.bottom > self.area.bottom:
            self.movey = -self.movey
            newpos = self.rect.move((self.movex, self.movey))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
        
    def action(self):
        pass
        
class Bomb(Bonus):
    def __init__(self,width,height,opcje):
        Bonus.__init__(self,20,width,height,"ball.png")
        self.booms = []
        for i in range(2):
            self.booms.append(load_sound("boom0"+str(i+1)+".wav",opcje))

    def action(self):
        choice(self.booms).play()