Esempio n. 1
0
 def update(self, seconds):
     # --- animate ---
     i = self.age *3 % len(self.images)
     self.image = self.images[int(i)]
     # --- chance to throw bomb ---
     if random.random() < 0.015:
         m = v.Vec2d(0, -random.random()*75)
         m.rotate(random.randint(-90,90))
         Bomb(pos=self.pos, move=m,
              gravity = v.Vec2d(0,0.7))
     # --- chance to change move vector ---
     if random.random() < 0.05:
          m = v.Vec2d(0, random.randint(-10, 10))
          m.rotate(random.randint(-120, 120))    
          self.move += m     
          #self.move=v.Vec2d(random.randint(-80,80),     
          #                  random.randint(-80,80))
     # --- bounce on screen edge ---
     if self.pos.x < 0:
         self.pos.x = 0
         self.move.x *= -1
     elif self.pos.x > PygView.width:
         self.pos.x = PygView.width
         self.move.x *= -1
     if self.pos.y < 0:
         self.pos.y = 0
         self.move.y *= -1
     elif self.pos.y > PygView.height // 2:
         self.pos.y = PygView.height // 2
         self.move.y *= -1
     VectorSprite.update(self, seconds)
Esempio n. 2
0
 def update(self, seconds):
     # --- animate ---
     i = self.age * 3 % len(self.images)
     self.image = self.images[int(i)]
     # --- chance to throw bomb ---
     if random.random() < 0.015:
         m = v.Vec2d(0, -random.random() * 75)
         m.rotate(random.randint(-90, 90))
         Bomb(pos=v.Vec2d(self.pos.x, self.pos.y),
              move=m,
              gravity=v.Vec2d(0, 0.7),
              kill_on_edge=True,
              mass=1800,
              hitpoints=200)
     # --- chance to change move vector ---
     if random.random() < 0.05:
         m = v.Vec2d(0, random.randint(-10, 10))
         m.rotate(random.randint(-120, 120))
         self.move += m
     if self.pos.x < 0:
         self.pos.x = 0
         self.move.x *= -1
     elif self.pos.x > PygView.width:
         self.pos.x = PygView.width
         self.move.x *= -1
     if self.pos.y < 0:
         self.pos.y = 0
         self.move.y *= -1
     elif self.pos.y > PygView.height // 2:
         self.pos.y = PygView.height // 2
         self.move.y *= -1
     VectorSprite.update(self, seconds)
Esempio n. 3
0
    def update(self, seconds):
        # --- animate ---
        i = self.age * 3 % len(self.images)
        self.image = self.images[int(i)]
        # --- chance to throw bomb ---
        #if random.random() < 0.015:
        #    m = v.Vec2d(0, -random.random()*75)
        #    m.rotate(random.randint(-90,90))
        #    Bomb(pos=self.pos, move=m,
        #         gravity = v.Vec2d(0,0.7))
        #------------------chance to spawn Ufo-------------------
        if random.random() < 0.009:
            Ufo(pos=v.Vec2d(self.pos.x, self.pos.y + 50))

        # --- chance to change move vector ---
        if random.random() < 0.05:
            m = v.Vec2d(0, random.randint(-10, 10))
            m.rotate(random.randint(-120, 120))
            self.move += m
        if self.pos.x < 0:
            self.pos.x = 0
            self.move.x *= -1
        elif self.pos.x > PygView.width:
            self.pos.x = PygView.width
            self.move.x *= -1
        if self.pos.y < 0:
            self.pos.y = 0
            self.move.y *= -1
        elif self.pos.y > PygView.height // 2:
            self.pos.y = PygView.height // 2
            self.move.y *= -1
        VectorSprite.update(self, seconds)
Esempio n. 4
0
 def __init__(self,
              pos=v.Vec2d(100, 100),
              move=None,
              color=None,
              gravity=None,
              lifetime=None,
              clone=False,
              radius=2):
     self.radius = radius
     if gravity is not None:
         self.gravity = v.Vec2d(gravity.x, gravity.y)
     else:
         self.gravity = gravity
     if color is not None:
         self.color = color
     else:
         self.color = (random.randint(0, 255), random.randint(0, 255),
                       random.randint(0, 255))
     if move is not None:
         self.move = v.Vec2d(move.x, move.y)
     else:
         self.move = v.Vec2d(0, random.randint(5, 250))
         self.move.rotate(random.randint(0, 360))
     self.clone = clone
     VectorSprite.__init__(self, pos, self.move)
     if lifetime is not None:
         self.lifetime = lifetime
     else:
         self.lifetime = 0.1 + random.random() * 3
 def __init__(self,
              screen,
              startpoint,
              pointlist,
              zoom=1,
              angle=0,
              color=(255, 0, 0),
              width=1,
              borderBounce=True,
              friction=0.5,
              move=v.Vec2d(0, 0)):
     self.startpoint = startpoint
     self.pointlist = pointlist
     self.rotationpoint = v.Vec2d(0, 0)
     self.zoom = zoom
     self.angle = angle
     self.color = color
     self.width = width
     self.screen = screen
     self.hitpoints = 1000
     self.number = Shape.number
     Shape.number += 1
     self.borderBounce = borderBounce
     #--- friction: 0 means no frictoin, 1 means no gliding
     self.friction = friction  #0.1 # 0 or False means no friction
     self.move = v.Vec2d(move.x, move.y)
Esempio n. 6
0
 def __init__(self, width=1440, height=800, fps=30):
     """Initialize pygame, window, background, font,...
        default arguments 
     """
     pygame.init()
     pygame.display.set_caption("Press ESC to quit.")
     PygView.width = width    # also self.width 
     PygView.height = height  # also self.height
     self.screen = pygame.display.set_mode((self.width, self.height), pygame.DOUBLEBUF)
     self.background = pygame.Surface(self.screen.get_size()).convert()  
     self.background.fill((255, 255, 255)) # yannik hier hintergrund färben
     # fill background white
     self.clock = pygame.time.Clock()
     self.fps = fps
     self.playtime = 0.0
     self.font = pygame.font.SysFont('mono', 24, bold=True)
     self.allgroup = pygame.sprite.LayeredUpdates()
     VectorSprite.groups = self.allgroup
     Fragment.groups = self.allgroup
     
     # ------ background images ------
     self.backgroundfilenames = [] # every .jpg file in folder 'data'
     for root, dirs, files in os.walk("data"):
         for file in files:
             if file[-4:] == ".jpg" or file[-5:] == ".jpeg":
                 self.backgroundfilenames.append(file)
     random.shuffle(self.backgroundfilenames) # remix sort order
     if len(self.backgroundfilenames) == 0:
         print("Error: no .jpg files found")
         pygame.quit
         sys.exit()
     self.level = 1
     self.loadbackground()
     Ufo(v.Vec2d(PygView.width, 50), v.Vec2d(-50,0))
Esempio n. 7
0
    def __init__(self, layer=4, **kwargs):
        """create a (black) surface and paint a blue ball on it"""
        self._layer = layer  # pygame Sprite layer
        pygame.sprite.Sprite.__init__(
            self, self.groups)  #call parent class. NEVER FORGET !
        # self groups is set in PygView.paint()
        self.number = FlyingObject.number  # unique number for each sprite
        FlyingObject.number += 1
        FlyingObject.numbers[self.number] = self
        # get unlimited named arguments and turn them into attributes
        for key, arg in kwargs.items():
            #print(key, arg)
            setattr(self, key, arg)
        # --- default values for missing keywords ----
        #if "pos" not in kwarts
        #pos=v.Vec2d(50,50), move=v.Vec2d(0,0), radius = 50, color=None,
        #         , hitpoints=100, mass=10, damage=10, bounce_on_edge=True, angle=0
        if "pos" not in kwargs:
            self.pos = v.Vec2d(50, 50)
        if "move" not in kwargs:
            self.move = v.Vec2d(0, 0)
        if "radius" not in kwargs:
            self.radius = 50
        if "width" not in kwargs:
            self.width = self.radius * 2
        if "height" not in kwargs:
            self.height = self.radius * 2
        if "color" not in kwargs:
            self.color = (random.randint(0, 255), random.randint(0, 255),
                          random.randint(0, 255))
        if "hitpoints" not in kwargs:
            self.hitpoints = 100
        self.hitpointsfull = self.hitpoints  # makes a copy
        if "mass" not in kwargs:
            self.mass = 10
        if "damage" not in kwargs:
            self.damage = 10
        if "bounce_on_edge" not in kwargs:
            self.bounce_on_edge = False
        if "kill_on_edge" not in kwargs:
            self.kill_on_edge = False
        if "angle" not in kwargs:
            self.angle = 0  # facing right?
        if "max_age" not in kwargs:
            self.max_age = None
        if "max_distance" not in kwargs:
            self.max_distance = None
        if "picture" not in kwargs:
            self.picture = None
        if "bossnumber" not in kwargs:
            self.bossnumber = None
        # ---
        self.age = 0  # in seconds
        self.distance_traveled = 0  # in pixel

        self.create_image()

        self.rect.center = (-300, -300
                            )  # avoid blinking image in topleft corner
Esempio n. 8
0
    def paint(self):
        """painting on the surface and create sprites"""
        # ---- playing field decoration ----
        # vertical middle line
        pygame.draw.line(self.background,
                         (10,10,10), 
                         (PygView.width // 2, 0),
                         (PygView.width // 2, PygView.height),
                         3)
        # middle circle
        pygame.draw.circle(self.background,
                           (10,10,10),
                           (PygView.width // 2, PygView.height//2),
                           200, 1)
        # half circle left goal
        pygame.draw.circle(self.background, (10,10,10),
                           (0, PygView.height // 2), 200, 1)
        # half circle right goal
        pygame.draw.circle(self.background, (10,10,10),
                           (PygView.width, PygView.height // 2), 200, 1)
                
        self.allgroup =  pygame.sprite.LayeredUpdates() # for drawing
        self.ballgroup = pygame.sprite.Group()          # for collision detection etc.
        self.bulletgroup = pygame.sprite.Group()
        self.cannongroup = pygame.sprite.Group()
        self.bonusgroup = pygame.sprite.Group()
        self.speedbonusgroup = pygame.sprite.Group()
        self.goalgroup = pygame.sprite.Group()
        self.expandergroup = pygame.sprite.Group()
        Ball.groups = self.allgroup, self.ballgroup # each Ball object belong to those groups
        Goal.groups = self.allgroup, self.goalgroup
        Flytext.groups = self.allgroup
        Bonus.groups = self.allgroup, self.bonusgroup
        SpeedBonus.groups = self.allgroup, self.speedbonusgroup
        Expander.groups = self.allgroup, self.expandergroup
        #Cannon.groups = self.allgroup, self.cannongroup
        VectorSprite.groups = self.allgroup
        
        self.ball1 = Ball(pos=v.Vec2d(PygView.width//2-200,PygView.height//2), move=v.Vec2d(0,0), bounce_on_edge=True, upkey=pygame.K_w, downkey=pygame.K_s, leftkey=pygame.K_a, rightkey=pygame.K_d, mass=500, color=(255,100,100), leftside=True, rightside = False) # creating a Ball Sprite
        #self.cannon1 = Cannon(bossnumber = self.ball1.number)
        self.ball2 = Ball(pos=v.Vec2d(PygView.width//2+200,PygView.height//2), move=v.Vec2d(0,0), bounce_on_edge=True, upkey=pygame.K_UP, downkey=pygame.K_DOWN, leftkey=pygame.K_LEFT, rightkey=pygame.K_RIGHT, mass=333, color=(100,100,255),  leftside=False, rightside = True)
        #self.cannon2 = Cannon(bossnumber = self.ball2.number)
        #self.cannona = Upercannon(pos=v.Vec2d(20,20), color=(255,0,0))
        #self.cannona2 = Lowercannon(pos=v.Vec2d(20,20), color=(255,0,0))
        #self.cannonb = Upercannon(pos=v.Vec2d(PygView.width-20,20), color=(255,255,0))
        #self.cannonb2 = Lowercannon(pos=v.Vec2d(PygView.width-20,20), color=(255,255,0))
        #self.cannonc = Upercannon(pos=v.Vec2d(20,PygView.height-20), color=(0,255,0))
        #self.cannonc2 = Lowercannon(pos=v.Vec2d(20,PygView.height-20), color=(0,255,0))
        #self.cannond = Upercannon(pos=v.Vec2d(PygView.width-20,PygView.height-20), color=(0,0,255))
        #self.cannond2 = Lowercannon(pos=v.Vec2d(PygView.width-20,PygView.height-20), color=(0,0,255))
        self.ball3 = Ball(pos=v.Vec2d(PygView.width/2,PygView.height/2), move=v.Vec2d(0,0), bounce_on_edge=True, radius=30)
        self.seeker1 = Ball(pos = v.Vec2d(0,0), target = self.ball3, mass = 2000,
                            color = (0,200,0), radius = 15)
        self.seeker2 = Ball(pos = v.Vec2d(PygView.width, PygView.height),target = self.ball3, mass = 2000,
                            color = (0,100,0), radius = 15)

        self.goal1 = Goal(pos=v.Vec2d(25, PygView.height//2), side="left", width=50, height=250, color=(200,50,50))
        self.goal2 = Goal(pos=v.Vec2d(PygView.width - 25, PygView.height//2), side="right", width=50, height=250, color=(200,200,50
        ))
Esempio n. 9
0
 def __init__(self,
              pos=v.Vec2d(100, 100),
              move=v.Vec2d(50, 0),
              color=(255, 0, 0),
              gravity=None):
     VectorSprite.__init__(self, pos, move, color)
     self.move = v.Vec2d(0, 0)
     self.lifetime = 0.25 + random.random()
Esempio n. 10
0
 def __init__(self, pos=v.Vec2d(100, 100), move=v.Vec2d(50, 0)):
     self._layer = 1
     pygame.sprite.Sprite.__init__(self, self.groups)
     self.pos = v.Vec2d(pos.x, pos.y)
     self.move = v.Vec2d(move.x, move.y)
     self.create_image()
     self.rect = self.image.get_rect()
     self.rect.center = self.pos.x, self.pos.y
     self.lifetime = None
     self.age = 0
Esempio n. 11
0
 def __init__(self, pos, radius=15, delay=None, lifetime=None, expand=False):
     self.radius = radius
     Fragment.__init__(self, v.Vec2d(pos.x, pos.y), v.Vec2d(0,0),
                       color=(255,255,255), radius=self.radius)
     if delay is None:
         self.delay = random.random() * 0.25
     else:
         self.delay = delay
     if lifetime is None:
         self.lifetime = random.random() * 0.01 + self.delay
     else:
         self.lifetime = lifetime + self.delay
     self.expand = expand
Esempio n. 12
0
 def __init__(self, pos=v.Vec2d(100,100), move=v.Vec2d(50,0),
              color=(255,0,0), gravity=None):
     self._layer = 1
     pygame.sprite.Sprite.__init__(self, self.groups)
     self.pos = v.Vec2d(pos.x, pos.y)
     self.move = v.Vec2d(move.x, move.y)
     self.color = color
     self.create_image()
     self.rect = self.image.get_rect()
     self.rect.center = self.pos.x, self.pos.y
     self.lifetime = None
     self.gravity = gravity
     self.age = 0
     self.hp = 100
     self.hpfull = 100
Esempio n. 13
0
 def update(self, seconds):
     VectorSprite.update(self, seconds)
     pressedkeys = pygame.key.get_pressed()
     self.move = v.Vec2d(0, 0)
     if self.upkey is not None:
         if pressedkeys[self.upkey]:
             #self.move.y -= 5
             self.forward(200)
     if self.downkey is not None:
         if pressedkeys[self.downkey]:
             #self.move.y += 5
             self.forward(-200)
     if self.leftkey is not None:
         if pressedkeys[self.leftkey]:
             #self.move.x -= 5
             self.side_left(200)
     if self.rightkey is not None:
         if pressedkeys[self.rightkey]:
             #self.move.x += 5
             self.side_right(200)
     if self.rotleftkey is not None:
         if pressedkeys[self.rotleftkey]:
             self.rotate(5)
             print(self.angle)
     if self.rotrightkey is not None:
         if pressedkeys[self.rotrightkey]:
             self.rotate(-5)
             print(self.angle)
Esempio n. 14
0
    def paint(self):
        """painting on the surface and create sprites"""
        # make an interesting background
        #draw_examples(self.background)
        # create (pygame) Sprites.
        self.allgroup = pygame.sprite.LayeredUpdates()  # for drawing
        self.ballgroup = pygame.sprite.Group()  # for collision detection etc.
        self.bulletgroup = pygame.sprite.Group()
        self.cannongroup = pygame.sprite.Group()
        Ball.groups = self.allgroup, self.ballgroup  # each Ball object belong to those groups
        #Bullet.groups = self.allgroup, self.bulletgroup
        Cannon.groups = self.allgroup, self.cannongroup
        VectorSprite.groups = self.allgroup
        #Hitpointbar.groups = self.allgroup

        self.ball1 = Ball(pos=v.Vec2d(200, 150),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_w,
                          downkey=pygame.K_s,
                          leftkey=pygame.K_a,
                          rightkey=pygame.K_d,
                          mass=500)  # creating a Ball Sprite
        self.cannon1 = Cannon(bossnumber=self.ball1.number)
        self.ball2 = Ball(pos=v.Vec2d(600, 350),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_UP,
                          downkey=pygame.K_DOWN,
                          leftkey=pygame.K_LEFT,
                          rightkey=pygame.K_RIGHT,
                          mass=333)
        self.cannon2 = Cannon(bossnumber=self.ball2.number)
        self.cannona = Cannon(pos=v.Vec2d(20, 20), color=(255, 0, 0))
        self.cannonb = Cannon(pos=v.Vec2d(PygView.width - 20, 20),
                              color=(255, 255, 0))
        self.cannonc = Cannon(pos=v.Vec2d(20, PygView.height - 20),
                              color=(0, 255, 0))
        self.cannond = Cannon(pos=v.Vec2d(PygView.width - 20,
                                          PygView.height - 20),
                              color=(0, 0, 255))
        self.ball3 = Ball(pos=v.Vec2d(PygView.width / 2, PygView.height / 2),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          radius=30)
 def update(self, seconds):
     self.age += seconds
     self.pos += self.move * seconds
     self.rect.center = (self.pos.x, self.pos.y)
     self.faceto(v.Vec2d(self.move.x * -1, self.move.y).get_angle())
     if self.lifetime is not None:
         self.lifetime -= seconds
         if self.lifetime < 0:
             self.kill()
Esempio n. 16
0
    def update(self, seconds):
        """calculate movement, position and bouncing on edge"""
        # ----- kill because... ------
        if self.hitpoints <= 0:
            self.kill()
        if self.max_age is not None and self.age > self.max_age:
            self.kill()
        if self.max_distance is not None and self.distance > self.max_distance:
            self.kill()
        # ---- movement with/without boss ----
        if self.bossnumber is not None:
            if self.kill_with_boss:
                if self.bossnumber not in VectorSprite.numbers:
                    self.kill()
                #print(self.bossnumber)
            if self.sticky_with_boss:
                boss = VectorSprite.numbers[self.bossnumber]
                self.pos = v.Vec2d(boss.pos.x, boss.pos.y)

        self.pos += self.move * seconds
        if self.friction is not None:
            self.move *= self.friction  # friction between 1.0 and 0.1
        self.distance_traveled += self.move.length * seconds
        self.age += seconds
        # ---- bounce / kill on screen edge ----
        if self.pos.x < 0:
            if self.kill_on_edge:
                self.kill()
                #print("Wallkill x < 0")
            elif self.bounce_on_edge:
                self.pos.x = 0
                self.move.x *= -1
        if self.pos.y < 0:
            if self.kill_on_edge:
                self.kill()
                #print("Wallkill y < 0")
            elif self.bounce_on_edge:
                self.y = 0
                self.move.y *= -1

        if self.pos.x > PygView.width:
            if self.kill_on_edge:
                self.kill()
                #print("Wallkill x > w")
            elif self.bounce_on_edge:
                self.pos.x = PygView.width
                self.move.x *= -1
        #if self.pos.y + self.height //2 > PygView.height:
        if self.pos.y > PygView.height:
            if self.kill_on_edge:
                #Explosion(pos=self.pos, max_age=random.random()*10)
                self.hitpoints = 0
                self.kill()
            elif self.bounce_on_edge:
                self.pos.y = PygView.height
                self.move.y *= -1
        self.rect.center = (round(self.pos.x, 0), round(self.pos.y, 0))
Esempio n. 17
0
 def update(self, seconds):
     VectorSprite.update(self, seconds)
     if self.gravity is not None:
         self.move += self.gravity * seconds
     if self.clone and random.random() < 0.5:
         Smoke(pos=self.pos,
               move=v.Vec2d(0, 0),
               color=self.color,
               lifetime=0.8)
Esempio n. 18
0
    def update(self, seconds):
        VectorSprite.update(self, seconds)
        pressedkeys = pygame.key.get_pressed()
        if self.age > self.endOfSpeedBonusTime:
            speedfactor = 1
        else:
            speedfactor = self.speedBonus
            if random.random() < 0.3:
                m = v.Vec2d (random.randint(150,250),0)
                w=random.randint(0,360)
                m.rotate(w)
                Fragment(radius = 5, pos = v.Vec2d(self.pos.x, self.pos.y),
                                              move = v.Vec2d(m.x, m.y),
                                              max_age=random.random()+0.5, 
                                              color = self.color)
                       
        if self.upkey is not None:
            if pressedkeys[self.upkey]:
                self.move.y -= 5 * speedfactor
        if self.downkey is not None:
            if pressedkeys[self.downkey]:
                self.move.y += 5  * speedfactor
        if self.leftkey is not None:
            if pressedkeys[self.leftkey]:
                self.move.x -= 5 * speedfactor
        if self.rightkey is not None:
            if pressedkeys[self.rightkey]:
                self.move.x += 5 * speedfactor

        #----------seeking target------------
        if self.target is not None:
            
            m =self.target.pos - self.pos
            m = m.normalized() * self.seekspeed
            self.move = m
            if random.random() < 0.01:
                self.seekspeed = random.randint(20,60)
        
        # ------ speedlimit -----
        if self.move.length > PygView.speedlimit:
            if self.age < self.endOfSpeedBonusTime:
                self.move = self.move.normalized() * PygView.speedlimit * 2
            else:
                self.move = self.move.normalized() * PygView.speedlimit
Esempio n. 19
0
    def paint(self):
        """painting on the surface and create sprites"""
        # make an interesting background
        draw_examples(self.background)
        # create (pygame) Sprites.
        self.allgroup = pygame.sprite.LayeredUpdates()  # for drawing
        self.ballgroup = pygame.sprite.Group()  # for collision detection etc.
        self.bulletgroup = pygame.sprite.Group()
        self.cannongroup = pygame.sprite.Group()
        Ball.groups = self.allgroup, self.ballgroup  # each Ball object belong to those groups
        #Bullet.groups = self.allgroup, self.bulletgroup
        Cannon.groups = self.allgroup, self.cannongroup
        VectorSprite.groups = self.allgroup
        #Hitpointbar.groups = self.allgroup

        self.ball1 = Ball(pos=v.Vec2d(200, 150),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_h,
                          downkey=pygame.K_n,
                          leftkey=pygame.K_b,
                          rightkey=pygame.K_m,
                          mass=500)  # creating a Ball Sprite
        self.cannon1 = Cannon(bossnumber=self.ball1.number)
        self.ball2 = Ball(pos=v.Vec2d(600, 350),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_h,
                          downkey=pygame.K_n,
                          leftkey=pygame.K_b,
                          rightkey=pygame.K_m,
                          mass=333)
        self.cannon2 = Cannon(bossnumber=self.ball2.number)
        #self.ball2 = Ball(x=200, y=100) # create another Ball Sprite

        #VectorSprite(horst=14, jens="abc")
        self.player1 = Player(color=(0, 0, 255),
                              stop_on_edge=True,
                              upkey=pygame.K_w,
                              downkey=pygame.K_s,
                              leftkey=pygame.K_q,
                              rightkey=pygame.K_e,
                              rotleftkey=pygame.K_a,
                              rotrightkey=pygame.K_d)
Esempio n. 20
0
 def update(self, seconds):
     VectorSprite.update(self, seconds)
     if self.age < self.readytofire:
         delta = 1 / len(self.recoil)
         i = (self.readytofire - self.age) / delta
         m = self.recoil[int(-i)]
         o = v.Vec2d(-20 + m, 0)
         o = o.rotated(-self.angle)
         self.rect.centerx += o.x
         self.rect.centery += o.y
Esempio n. 21
0
    def paint(self):
        """painting on the surface and create sprites"""
        # make an interesting background
        draw_examples(self.background)
        # create (pygame) Sprites.
        self.allgroup = pygame.sprite.LayeredUpdates()  # for drawing
        self.ballgroup = pygame.sprite.Group()  # for collision detection etc.
        self.bulletgroup = pygame.sprite.Group()
        Ball.groups = self.allgroup, self.ballgroup  # each Ball object belong to those groups
        Bullet.groups = self.allgroup, self.bulletgroup
        FlyingObject.groups = self.allgroup
        Hitpointbar.groups = self.allgroup

        self.ball1 = Ball(pos=v.Vec2d(200, 150),
                          move=v.Vec2d(-20, -5),
                          bounce_on_edge=True)  # creating a Ball Sprite
        self.ball2 = Ball(pos=v.Vec2d(600, 350),
                          move=v.Vec2d(120, 50),
                          bounce_on_edge=True)
Esempio n. 22
0
 def update(self, seconds):
     if self.boss.hp <= 0:
         self.kill()
     VectorSprite.update(self, seconds)
     self.move = self.boss.move
     self.pos = self.boss.pos + v.Vec2d(0,-40)
     self.image.fill((0,0,0))
     pygame.draw.rect(self.image, (0,0,0), (0,0,100,5), 1)
     pygame.draw.rect(self.image, (0,200,0), (1,1,99,4), 1)
     pygame.draw.rect(self.image, (0,200,0), (1,1,self.boss.hp,4), 0)
     self.image.set_colorkey((0,0,0))
     self.image.convert_alpha()
 def __init__(self, width=1440, height=850, fps=30):
     """Initialize pygame, window, background, font,...
        default arguments 
     """
     pygame.init()
     pygame.display.set_caption("Press ESC to quit.")
     PygView.width = width  # also self.width
     PygView.height = height  # also self.height
     self.screen = pygame.display.set_mode((self.width, self.height),
                                           pygame.DOUBLEBUF)
     self.background = pygame.Surface(self.screen.get_size()).convert()
     self.background.fill((0, 0, 255))
     # fill background white
     self.clock = pygame.time.Clock()
     self.fps = fps
     self.playtime = 0.0
     self.font = pygame.font.SysFont('mono', 24, bold=True)
     self.allgroup = pygame.sprite.LayeredUpdates()
     VectorSprite.groups = self.allgroup
     Fragment.groups = self.allgroup
     #self.ufo1 = Ufo()
     self.ship1 = Ship1(pos=v.Vec2d(500, 500), move=v.Vec2d(100, 100))
Esempio n. 24
0
 def update(self, seconds):
     VectorSprite.update(self, seconds)
     self.pos = v.Vec2d(pygame.mouse.get_pos())
     # self.r can take the values of 255 until 101
     self.r += self.delta
     if self.r < 151:
         self.r = 151
         self.delta = 10
     elif self.r > 255:
         self.r = 255
         self.delta = -10
     
     self.create_image()
Esempio n. 25
0
   def update(self, seconds):
       """calculate movement, position and bouncing on edge"""
       # ----- kill because... ------
       if self.hitpoints <= 0:
           self.kill()
       if self.max_age is not None and self.age > self.max_age:
           self.kill()
       if self.max_distance is not None and self.distance > self.max_distance:
           self.kill()
       # ---- movement with/without boss ----
       if self.bossnumber is not None:
           if self.kill_with_boss:
               if self.bossnumber not in VectorSprite.numbers:
                   self.kill()
               #print(self.bossnumber)
           if self.sticky_with_boss:
               boss = VectorSprite.numbers[self.bossnumber]
               self.pos = v.Vec2d(boss.pos.x, boss.pos.y)
                  
           
       self.pos += self.move * seconds
       if self.friction is not None:
           self.move *= self.friction # friction between 1.0 and 0.1
       self.distance_traveled += self.move.length * seconds
       self.age += seconds
 
       # ---- bounce / kill on screen edge ----
       if self.bounce_on_edge: 
           if self.pos.x - self.width //2 < 0:
               self.pos.x = self.width // 2
               if self.kill_on_edge:
                   self.kill()
               self.move.x *= -1 
           if self.pos.y - self.height // 2 < 0:
               self.y = self.height // 2
               if self.kill_on_edge:
                   self.kill()
               self.move.y *= -1
           if self.pos.x + self.width //2 > PygView.width:
               self.pos.x = PygView.width - self.width //2
               if self.kill_on_edge:
                   self.kill()
               self.move.x *= -1
           if self.pos.y + self.height //2 > PygView.height:
               self.pos.y = PygView.height - self.height //2
               if self.kill_on_edge:
                   self.kill()
               self.move.y *= -1
       # update sprite position 
       self.rect.center = ( round(self.pos.x, 0), round(self.pos.y, 0) )
Esempio n. 26
0
    def paint(self):
        """painting on the surface and create sprites"""
        # make an interesting background
        draw_examples(self.background)
        # create (pygame) Sprites.
        self.allgroup = pygame.sprite.LayeredUpdates()  # for drawing
        self.ballgroup = pygame.sprite.Group()  # for collision detection etc.
        self.bulletgroup = pygame.sprite.Group()
        self.cannongroup = pygame.sprite.Group()
        Ball.groups = self.allgroup, self.ballgroup  # each Ball object belong to those groups
        #Bullet.groups = self.allgroup, self.bulletgroup
        Cannon.groups = self.allgroup, self.cannongroup
        VectorSprite.groups = self.allgroup
        #Hitpointbar.groups = self.allgroup

        self.ball1 = Ball(pos=v.Vec2d(200, 150),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_w,
                          downkey=pygame.K_s,
                          leftkey=pygame.K_a,
                          rightkey=pygame.K_d,
                          mass=0.0000001)  # creating a Ball Sprite
        self.cannon1 = Cannon(bossnumber=self.ball1.number)
        self.ball2 = Ball(pos=v.Vec2d(600, 350),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_UP,
                          downkey=pygame.K_DOWN,
                          leftkey=pygame.K_LEFT,
                          rightkey=pygame.K_RIGHT,
                          mass=333)
        self.cannon2 = Cannon(bossnumber=self.ball2.number)
        self.ball3 = Ball(pos=v.Vec2d(200, 450),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          upkey=pygame.K_i,
                          downkey=pygame.K_l,
                          leftkey=pygame.K_j,
                          rightkey=pygame.K_l,
                          mass=0.000001)
        self.cannon3 = Cannon(bossnumber=self.ball3.number)
        self.ball4 = Ball(pos=v.Vec2d(800, 150),
                          move=v.Vec2d(0, 0),
                          bounce_on_edge=True,
                          mass=333)
        self.cannon4 = Cannon(bossnumber=self.ball4.number)
Esempio n. 27
0
 def update(self, seconds):
     # --- animate ---
     i = (self.age * 10) % 4  # modulo = rest of division by 3
     self.image = self.images[int(i)]
     # --- chance to change move vector ---
     if random.random() < 0.001:
         self.move = v.Vec2d(random.randint(-80, 80),
                             random.randint(-80, 80))
     # --- bounce on screen edge ---
     if self.pos.x < 0:
         self.pos.x = 0
         self.move.x *= -1
     elif self.pos.x > PygView.width:
         self.pos.x = PygView.width
         self.move.x *= -1
     if self.pos.y < 0:
         self.pos.y = 0
         self.move.y *= -1
     elif self.pos.y > PygView.height:
         self.pos.y = PygView.height
         self.move.y *= -1
     VectorSprite.update(self, seconds)
Esempio n. 28
0
    def run(self):
        """The mainloop"""

        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                # ------- pressed and released key ------
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False
                    if event.key == pygame.K_b:
                        Ball(pos=v.Vec2d(self.ball1.pos.x, self.ball1.pos.y),
                             move=v.Vec2d(0, 0),
                             radius=5,
                             friction=0.995,
                             bounce_on_edge=True)  # add small balls!
                    if event.key == pygame.K_c:
                        m = v.Vec2d(60, 0)  # lenght of cannon
                        m = m.rotated(-self.cannon1.angle)
                        p = v.Vec2d(self.ball1.pos.x, self.ball1.pos.y) + m
                        #print(p, m)
                        Ball(pos=p,
                             move=m.normalized() * 15 + self.ball1.move,
                             radius=10,
                             color=(0, 0, 1))  # move=v.Vec2d(0,0),
                        #knockbackeffect
                        self.ball1.move += m.normalized() * -15
                        #m.rotate(self.cannon1.angle)
                        #Bullet(bossnumber = self.ball1.number, move=m, kill_on_edge = True)
                    if event.key == pygame.K_LEFT:
                        self.ball1.rotate(1)  #
                        print(self.ball1.angle)
                    if event.key == pygame.K_h:
                        m = v.Vec2d(60, 0)  # lenght of cannon
                        m = m.rotated(-self.cannon3.angle)
                        p = v.Vec2d(self.ball3.pos.x, self.ball3.pos.y) + m
                        #print(p, m)
                        Ball(pos=p,
                             move=m.normalized() * 15 + self.ball3.move,
                             radius=10,
                             color=(0, 0, 1))  # move=v.Vec2d(0,0),
                        #knockbackeffect
                        self.ball3.move += m.normalized() * -15
                        m = v.Vec2d(60, 0)  # lenght of cannon
                        m = m.rotated(-self.cannon3.angle)
                        p = v.Vec2d(self.ball3.pos.x, self.ball3.pos.y) + m
                        #print(p, m)
                        Ball(pos=p,
                             move=m.normalized() * 15 + self.ball3.move,
                             radius=10,
                             color=(0, 0, 0))  # move=v.Vec2d(0,0),
                        #knockbackeffect
                        self.ball3.move += m.normalized() * -15
                    #if event.key == pygame.K_m:
                    #   m = v.Vec2d(60,0) # lenght of cannon
                    #  m = m.rotated(-self.cannon2.angle)
                    #  p = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y) + m
                    #  #print(p, m)
                    #  Ball(pos=p, move=m.normalized()*2000 + self.ball2.move, radius=10) # move=v.Vec2d(0,0),
                    #  #knockbackeffect
                    #  self.ball3.move += m.normalized()* -15
                    # m = v.Vec2d(60,0) # lenght of cannon
                    #  m = m.rotated(-self.cannon2.angle)
                    # p = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y) + m
                    #print(p, m)
                    # Ball(pos=p, move=m.normalized()*2000 + self.ball2.move, radius=10) # move=v.Vec2d(0,0),
                    #knockbackeffect
                    # self.ball2.move += m.normalized()* -15
                    #if event.key == pygame.K_q:
                    #   m = v.Vec2d(60,0) # lenght of cannon
                    #  m = m.rotated(-self.cannon4.angle)
                    # p = v.Vec2d(self.ball4.pos.x, self.ball4.pos.y) + m
                    #print(p, m)
                    # Ball(pos=p, move=m.normalized()*1000 + self.ball4.move, radius=10) # move=v.Vec2d(0,0),
                    #knockbackeffect
                    # self.ball4.move += m.normalized()* -15
                    # m = v.Vec2d(60,0) # lenght of cannon
                    # m = m.rotated(-self.cannon4.angle)
                    # p = v.Vec2d(self.ball4.pos.x, self.ball4.pos.y) + m
                    # #print(p, m)
                    # Ball(pos=p, move=m.normalized()*2000 + self.ball4.move, radius=10) # move=v.Vec2d(0,0),
                    # #knockbackeffect
                    # self.ball4.move += m.normalized()* -15

                    #if event.key == pygame.K_t:
                    #    a = v.Vec2d(100,100)
                    #    b = v.Vec2d(100, 200)
                    #    print("a: {}, b:{}, Winkel: {}".format(a,b, v.Vec2d.get_angle(b-a)))
            # ------------ pressed keys ------
            pressed_keys = pygame.key.get_pressed()
            if pressed_keys[pygame.K_y]:
                self.cannon1.rotate(100)
            if pressed_keys[pygame.K_x]:
                self.cannon1.rotate(-100)
            if pressed_keys[pygame.K_u]:
                self.cannon3.rotate(100)
            if pressed_keys[pygame.K_z]:
                self.cannon3.rotate(-100)

            # --- auto aim cannon2 at ball1 ----
            vectordiff = self.ball2.pos - self.ball1.pos
            self.cannon2.set_angle(-vectordiff.get_angle() - 180)

            # --- auto aim cannon4 at ball3 ----
            vectordiff = self.ball4.pos - self.ball3.pos
            self.cannon4.set_angle(-vectordiff.get_angle() - 180)

            # - - - - - - - autoshooting - - - - - - -
            if random.random() < 0.002:
                m = v.Vec2d(60, 0)  # lenght of cannon
                m = m.rotated(-self.cannon4.angle)
                p = v.Vec2d(self.ball4.pos.x, self.ball4.pos.y) + m
                #print(p, m)
                Ball(pos=p,
                     move=m.normalized() * 0.001 + self.ball4.move,
                     radius=10)  # move=v.Vec2d(0,0),
                #knockbackeffect
                self.ball3.move += m.normalized() * -1
                m = v.Vec2d(60, 0)  # lenght of cannon
                m = m.rotated(-self.cannon4.angle)
                p = v.Vec2d(self.ball4.pos.x, self.ball4.pos.y) + m
                #print(p, m)
                Ball(pos=p,
                     move=m.normalized() * 0.0001 + self.ball4.move,
                     radius=10)  # move=v.Vec2d(0,0),
                #knockbackeffect
                self.ball4.move += m.normalized() * -1

            if random.random() < 0.002:
                m = v.Vec2d(60, 0)  # lenght of cannon
                m = m.rotated(-self.cannon2.angle)
                p = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y) + m
                #print(p, m)
                Ball(pos=p,
                     move=m.normalized() * 0.0001 + self.ball2.move,
                     radius=10)  # move=v.Vec2d(0,0),
                #knockbackeffect
                self.ball2.move += m.normalized() * -1
                m = v.Vec2d(60, 0)  # lenght of cannon
                m = m.rotated(-self.cannon2.angle)
                p = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y) + m
                #print(p, m)
                Ball(pos=p,
                     move=m.normalized() * 1 + self.ball2.move,
                     radius=10)  # move=v.Vec2d(0,0),
                #knockbackeffect
                self.ball2.move += m.normalized() * -15

            # - - - - - - - - - - - - - auto beaming ball2 - - - - - - - - - - - - - - - - - -
            if random.random() < 0.009:
                xx = random.randint(0, PygView.width)
                yy = random.randint(0, PygView.height)
                self.ball2.pos = v.Vec2d(xx, yy)

            milliseconds = self.clock.tick(self.fps)  #
            seconds = milliseconds / 1000
            self.playtime += seconds
            # delete everything on screen
            self.screen.blit(self.background, (0, 0))
            # write text below sprites
            write(
                self.screen, "FPS: {:6.3}  PLAYTIME: {:6.3} SECONDS".format(
                    self.clock.get_fps(), self.playtime))
            # you can use: pygame.sprite.collide_rect, pygame.sprite.collide_circle, pygame.sprite.collide_mask
            # the False means the colliding sprite is not killed
            # ---------- collision detection between ball and bullet sprites ---------
            #for ball in self.ballgroup:
            #   crashgroup = pygame.sprite.spritecollide(ball, self.bulletgroup, False, pygame.sprite.collide_circle)
            #   for bullet in crashgroup:
            #       elastic_collision(ball, bullet) # change dx and dy of both sprites
            #       ball.hitpoints -= bullet.damage
            # --------- collision detection between ball and other balls
            for ball in self.ballgroup:
                crashgroup = pygame.sprite.spritecollide(
                    ball, self.ballgroup, False, pygame.sprite.collide_circle)
                for otherball in crashgroup:
                    if ball.number > otherball.number:  # make sure no self-collision or calculating collision twice
                        elastic_collision(
                            ball,
                            otherball)  # change dx and dy of both sprites
            # ---------- collision detection between bullet and other bullets
            for bullet in self.bulletgroup:
                crashgroup = pygame.sprite.spritecollide(
                    bullet, self.bulletgroup, False,
                    pygame.sprite.collide_circle)
                for otherbullet in crashgroup:
                    if bullet.number > otherbullet.number:
                        elastic_collision(
                            bullet,
                            otherball)  # change dx and dy of both sprites
            # -------- remove dead -----
            #for sprite in self.ballgroup:
            #    if sprite.hitpoints < 1:
            #        sprite.kill()
            # ----------- clear, draw , update, flip -----------------
            #self.allgroup.clear(screen, background)
            self.allgroup.update(seconds)  # would also work with ballgroup
            self.allgroup.draw(self.screen)
            # write text over everything
            #write(self.screen, "Press b to add another ball", x=self.width//2, y=250, center=True)
            #write(self.screen, "Press c to add another bullet", x=self.width//2, y=350, center=True)
            # next frame
            pygame.display.flip()
            pygame.display.set_caption(
                "Press ESC to quit. Cannon angle: {}".format(
                    self.cannon1.angle))
            #a = v.Vec2d(self.ball1.pos.x, self.ball1.pos.y)
            #b = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y)
            #print("winkel:1:{} 2:{}  winkel:{}".format(a,b, v.Vec2d.get_angle_between(a,b)))
        pygame.quit()
Esempio n. 29
0
    def __init__(self, **kwargs):
        """create a (black) surface and paint a blue ball on it"""
        for key, arg in kwargs.items():
            setattr(self, key, arg)
        if "layer" not in kwargs:
            self._layer = 4

        #self._layer = layer   # pygame Sprite layer
        pygame.sprite.Sprite.__init__(
            self, self.groups)  #call parent class. NEVER FORGET !
        self.number = VectorSprite.number  # unique number for each sprite
        VectorSprite.number += 1
        VectorSprite.numbers[self.number] = self
        # get unlimited named arguments and turn them into attributes

        # --- default values for missing keywords ----
        if "pos" not in kwargs:
            self.pos = v.Vec2d(50, 50)
        if "move" not in kwargs:
            self.move = v.Vec2d(0, 0)
        if "radius" not in kwargs:
            self.radius = 50
        if "width" not in kwargs:
            self.width = self.radius * 2
        if "height" not in kwargs:
            self.height = self.radius * 2
        if "color" not in kwargs:
            self.color = (random.randint(0, 255), random.randint(0, 255),
                          random.randint(0, 255))
        if "hitpoints" not in kwargs:
            self.hitpoints = 100
        self.hitpointsfull = self.hitpoints  # makes a copy
        if "mass" not in kwargs:
            self.mass = 10
        if "damage" not in kwargs:
            self.damage = 10
        if "bounce_on_edge" not in kwargs:
            self.bounce_on_edge = False
        if "kill_on_edge" not in kwargs:
            self.kill_on_edge = False
        if "angle" not in kwargs:
            self.angle = 0  # facing right?
        if "max_age" not in kwargs:
            self.max_age = None
        if "max_distance" not in kwargs:
            self.max_distance = None
        if "picture" not in kwargs:
            self.picture = None
        if "bossnumber" not in kwargs:
            self.bossnumber = None
        if "kill_with_boss" not in kwargs:
            self.kill_with_boss = False
        if "sticky_with_boss" not in kwargs:
            self.sticky_with_boss = False
        if "mass" not in kwargs:
            self.mass = 15
        if "friction" not in kwargs:
            self.friction = None
        if "upkey" not in kwargs:
            self.upkey = None
        if "downkey" not in kwargs:
            self.downkey = None
        if "rightkey" not in kwargs:
            self.rightkey = None
        if "leftkey" not in kwargs:
            self.leftkey = None
        # ---
        self.age = 0  # in seconds
        self.distance_traveled = 0  # in pixel
        self.create_image()
        self.rect.center = (-300, -300
                            )  # avoid blinking image in topleft corner
Esempio n. 30
0
    def run(self):
        """The mainloop"""
        self.score1 = 0
        self.score2 = 0
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                # ------- pressed and released key ------
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False
                    if event.key == pygame.K_b:
                        Ball(pos=v.Vec2d(self.ball1.pos.x, self.ball1.pos.y),
                             move=v.Vec2d(0, 0),
                             radius=5,
                             friction=0.995,
                             bounce_on_edge=True)  # add small balls!
                    if event.key == pygame.K_c:
                        m = v.Vec2d(60, 0)  # lenght of cannon
                        #m = m.rotated(-self.cannon1.angle)
                        p = v.Vec2d(self.ball1.pos.x, self.ball1.pos.y) + m
                        Ball(pos=p, move=m.normalized() * 15,
                             radius=10)  # move=v.Vec2d(0,0),
                    if event.key == pygame.K_LEFT:
                        self.ball1.rotate(1)  #
                    if event.key == pygame.K_1:
                        PygView.friction = 0.995
                    if event.key == pygame.K_2:
                        PygView.friction = 0.9
                    if event.key == pygame.K_3:
                        PygView.friction = 1.01
                        #print(self.ball1.angle)
                    #if event.key == pygame.K_s:
                    #    m = v.Vec2d(60,0) # lenght of cannon
                    #    #m = m.rotated(-self.cannon1.angle)
                    #    #p = v.Vec2d(self.cannon1.pos.x, self.cannon1.pos.y) + m
                    #    Ball(pos=p, move=m.normalized()*100, radius=5,mass=100, color=(255,0,0))
                    #    #self.cannon1.readytofire = self.cannon1.age + 1

            # ------------ pressed keys ------
            pressed_keys = pygame.key.get_pressed()
            #if pressed_keys[pygame.K_y]:
            #self.cannon1.rotate(1)
            #if pressed_keys[pygame.K_x]:
            #self.cannon1.rotate(-1)

            # --- auto aim cannon2 at ball1 ----

            #vectordiff = self.cannon2.pos - self.ball1.pos
            #self.cannon2.set_angle(-vectordiff.get_angle()-180)
            # --- auto aim cannona at ball1 ---
            #d1 = self.cannona.pos - self.ball1.pos
            #d2 = self.cannona.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #d = d1
            #else:
            #d = d2
            #self.cannona.set_angle(-d.get_angle()-180)

            #d1 = self.cannona2.pos - self.ball1.pos
            #d2 = self.cannona2.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #d = d1
            #else:
            #d = d2
            #self.cannona2.set_angle(-d.get_angle()-180)

            # --- auto aim cannonb at ball1 ---
            #d1 = self.cannonb.pos - self.ball1.pos
            #d2 = self.cannonb.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #   d = d1
            #else:
            #    d = d2
            #self.cannonb.set_angle(-d.get_angle()-180)

            #d1 = self.cannonb2.pos - self.ball1.pos
            #d2 = self.cannonb2.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #   d = d1
            #else:
            #   d = d2
            #self.cannonb2.set_angle(-d.get_angle()-180)

            # --- auto aim cannonc at ball1 ---
            #d1 = self.cannonc.pos - self.ball1.pos
            #d2 = self.cannonc.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #  d = d1
            #else:
            #   d = d2
            #self.cannonc.set_angle(-d.get_angle()-180)

            #d1 = self.cannonc2.pos - self.ball1.pos
            #d2 = self.cannonc2.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #   d = d1
            #else:
            #    d = d2
            #self.cannonc2.set_angle(-d.get_angle()-180)
            # --- auto aim cannond at ball1 ---
            #d1 = self.cannond.pos - self.ball1.pos
            #d2 = self.cannond.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #   d = d1
            #else:
            #   d = d2
            #self.cannond.set_angle(-d.get_angle()-180)

            #d1 = self.cannond2.pos - self.ball1.pos
            #d2 = self.cannond2.pos - self.ball2.pos

            #if d1.get_length() < d2.get_length():
            #   d = d1
            #else:
            #   d = d2
            #self.cannond2.set_angle(-d.get_angle()-180)
            # --- auto aim cannon1 at ball2 ---
            #vectordiff = self.ball1.pos - self.ball2.pos
            #self.cannon1.set_angle(-vectordiff.get_angle()-180)

            #---- autofire cannon A ------
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,-15) # lenght of cannon
            #  m = m.rotated(-self.cannona.angle)
            # p = v.Vec2d(self.cannona.pos.x, self.cannona.pos.y) + m
            #Ball(pos=p, move=m.normalized()*100, radius=5,mass=100, color=(255,0,0), kill_on_edge=True)
            #self.cannona.readytofire = self.cannona.age + 1
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,15) # lenght of cannon
            #  m = m.rotated(-self.cannona2.angle)
            # p = v.Vec2d(self.cannona2.pos.x, self.cannona2.pos.y) + m
            #Bullet(pos=p, move=m.normalized()*100, radius=5,mass=100, color=(255,0,0), kill_on_edge=True)
            #self.cannona2.readytofire = self.cannona2.age + 1
            #---- autofire cannon B ------
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,-15) # lenght of cannon
            #  m = m.rotated(-self.cannonb.angle)
            # p = v.Vec2d(self.cannonb.pos.x, self.cannonb.pos.y) + m
            #Ball(pos=p, move=m.normalized()*100, radius=5,mass=200, color=(255,255,0), kill_on_edge=True)
            #self.cannonb.readytofire = self.cannonb.age + 1
            #if random.random() < 0.05:
            #    m = v.Vec2d(60,15) # lenght of cannon
            #    m = m.rotated(-self.cannonb2.angle)
            #    p = v.Vec2d(self.cannonb2.pos.x, self.cannonb2.pos.y) + m
            #    Ball(pos=p, move=m.normalized()*100, radius=5,mass=200, color=(255,255,0), kill_on_edge=True)
            #    self.cannonb2.readytofire = self.cannonb2.age + 1
            #---- autofire cannon C ------
            #if random.random() < 0.05:
            #    m = v.Vec2d(60,-15) # lenght of cannon
            #    m = m.rotated(-self.cannonc.angle)
            #    p = v.Vec2d(self.cannonc.pos.x, self.cannonc.pos.y) + m
            #    Ball(pos=p, move=m.normalized()*100, radius=5,mass=300, color=(0,255,0), kill_on_edge=True)
            #    self.cannonc.readytofire = self.cannonc.age + 1
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,15) # lenght of cannon
            #  m = m.rotated(-self.cannonc2.angle)
            # p = v.Vec2d(self.cannonc2.pos.x, self.cannonc2.pos.y) + m
            #Ball(pos=p, move=m.normalized()*100, radius=5,mass=300, color=(0,255,0), kill_on_edge=True)
            #self.cannonc2.readytofire = self.cannonc2.age + 1
            #---- autofire cannon D ------
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,-15) # lenght of cannon
            #  m = m.rotated(-self.cannond.angle)
            # p = v.Vec2d(self.cannond.pos.x, self.cannond.pos.y) + m
            #Ball(pos=p, move=m.normalized()*100, radius=5,mass=400, color=(0,0,255), kill_on_edge=True)
            #self.cannond.readytofire = self.cannond.age + 1
            #if random.random() < 0.05:
            #   m = v.Vec2d(60,15) # lenght of cannon
            #  m = m.rotated(-self.cannond2.angle)
            # p = v.Vec2d(self.cannond2.pos.x, self.cannond2.pos.y) + m
            #Ball(pos=p, move=m.normalized()*100, radius=5,mass=400, color=(0,0,255), kill_on_edge=True)
            #self.cannond2.readytofire = self.cannond2.age + 1

            milliseconds = self.clock.tick(self.fps)  #
            seconds = milliseconds / 1000
            self.playtime += seconds
            self.gametime -= seconds
            if self.gametime <= 0:
                if self.score1 == self.score2:
                    text = "Unentschieden!"
                if self.score1 > self.score2:
                    text = "Sieg für Rot!"
                if self.score1 < self.score2:
                    text = "Sieg für Blau!"
                write(self.screen,
                      text,
                      x=PygView.width // 2,
                      y=PygView.height // 2,
                      fontsize=150,
                      center=True)
                pygame.display.flip()
                time.sleep(3)
                break

            # delete everything on screen
            self.screen.blit(self.background, (0, 0))
            # write text below sprites
            write(
                self.screen, "FPS: {:6.3}  Gametime: {:6.3} SECONDS".format(
                    self.clock.get_fps(), self.gametime))
            self.allgroup.update(seconds)  # would also work with ballgroup

            # left score
            write(self.screen,
                  "{}".format(self.score1),
                  x=PygView.width // 100 * 25,
                  y=PygView.height // 2,
                  color=self.ball1.color,
                  center=True,
                  fontsize=100)
            write(self.screen,
                  "{}".format(self.score2),
                  x=PygView.width // 100 * 75,
                  y=PygView.height // 2,
                  color=self.ball2.color,
                  center=True,
                  fontsize=100)

            # you can use: pygame.sprite.collide_rect, pygame.sprite.collide_circle, pygame.sprite.collide_mask
            # the False means the colliding sprite is not killed
            # ---------- collision detection between ball and bullet sprites ---------
            #for ball in self.ballgroup:
            #   crashgroup = pygame.sprite.spritecollide(ball, self.bulletgroup, False, pygame.sprite.collide_circle)
            #   for bullet in crashgroup:
            #       elastic_collision(ball, bullet) # change dx and dy of both sprites
            #       ball.hitpoints -= bullet.damage

            # --------- collision detection between ball3 and goalgroup --------
            crash = pygame.sprite.spritecollideany(self.ball3, self.goalgroup)
            #collided = collide_mask)
            if crash is not None:
                if crash.side == "left":
                    self.score2 += 1
                elif crash.side == "right":
                    self.score1 += 1
                for b in [self.ball1, self.ball2, self.ball3]:
                    b.move = v.Vec2d(0, 0)
                self.ball1.pos = v.Vec2d(PygView.width // 2 - 100,
                                         PygView.height // 2)
                self.ball2.pos = v.Vec2d(PygView.width // 2 + 100,
                                         PygView.height // 2)
                self.ball3.pos = v.Vec2d(PygView.width // 2,
                                         PygView.height // 2)

            # --------- collision detection between ball and other balls
            for ball in self.ballgroup:
                crashgroup = pygame.sprite.spritecollide(
                    ball, self.ballgroup, False, pygame.sprite.collide_circle)
                for otherball in crashgroup:
                    if ball.number > otherball.number:  # make sure no self-collision or calculating collision twice
                        elastic_collision(
                            ball,
                            otherball)  # change dx and dy of both sprites
            # ---------- collision detection between bullet and other bullets
            #for bullet in self.bulletgroup:
            #    crashgroup = pygame.sprite.spritecollide(bullet, self.bulletgroup, False, pygame.sprite.collide_circle)
            #    for otherbullet in crashgroup:
            #        if bullet.number > otherbullet.number:
            #             elastic_collision(bullet, otherball) # change dx and dy of both sprites
            # -------- remove dead -----
            #for sprite in self.ballgroup:
            #    if sprite.hitpoints < 1:
            #        sprite.kill()
            # ----------- clear, draw , update, flip -----------------
            #self.allgroup.clear(screen, background)
            self.allgroup.draw(self.screen)
            # write text over everything
            #write(self.screen, "Press b to add another ball", x=self.width//2, y=250, center=True)
            #write(self.screen, "Press c to add another bullet", x=self.width//2, y=350, center=True)
            # next frame
            pygame.display.flip()
            #pygame.display.set_caption("Press ESC to quit. Cannon angle: {}".format(self.cannon1.angle))
            #a = v.Vec2d(self.ball1.pos.x, self.ball1.pos.y)
            #b = v.Vec2d(self.ball2.pos.x, self.ball2.pos.y)
            #print("winkel:1:{} 2:{}  winkel:{}".format(a,b, v.Vec2d.get_angle_between(a,b)))
        pygame.quit()