SHARE
TWEET

Untitled

a guest Aug 13th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import math
  3. pygame.init()
  4.  
  5. def degrees_to_radians(degrees):
  6.     return degrees * (math.pi / 180.0)
  7.  
  8. class Projectile(pygame.sprite.Sprite):
  9.     def __init__(self, boss, ddx, ddy):
  10.         self.shotsound = pygame.mixer.Sound('src/sound/weapons/rifle.ogg')
  11.         pygame.sprite.Sprite.__init__(self)
  12.  
  13.         self.load_image = pygame.image.load("src/images/projectile.png")
  14.         """Owner of the shot"""
  15.         self.boss = boss
  16.         clock = pygame.time.Clock()
  17.         self.bulletspeed = 20.0 # pixel per second ?
  18.         self.ddx = ddx
  19.         self.ddy = ddy
  20.         self.pos = [0,0]
  21.  
  22.         self.rect = self.load_image.get_rect()
  23.         self.lifetime = 5 # 5 seconds
  24.         self.angle = self.boss.angle_to_pointer
  25.  
  26.         self.pos[0] = self.boss.rect.centerx
  27.         self.pos[1] = self.boss.rect.centery
  28.         self.calculate_origin()
  29.         self.image = pygame.transform.rotate(self.load_image, self.angle)
  30.  
  31.         self.startlength = 0
  32.         self.maxlength = 5
  33.         self.dx = self.ddx * self.bulletspeed
  34.         self.dy = self.ddy * self.bulletspeed
  35.  
  36.     def calculate_origin(self):
  37.         # - spawn bullet at end of turret barrel instead tank center -
  38.         # cannon is around Tank.side long, calculatet from Tank center
  39.         # later subtracted 20 pixel from this distance
  40.         # so that bullet spawns closer to tank muzzle
  41.         self.pos[0] +=  math.cos(degrees_to_radians(self.boss.angle_to_pointer)) #* (self.boss.rect.width)
  42.         self.pos[1] +=  math.sin(degrees_to_radians(-self.boss.angle_to_pointer)) #* (self.boss.rect.width)
  43.  
  44.  
  45.     def update(self, block_group):
  46.         self.block_group = block_group
  47.         self.pos[0] += self.dx * 0.2 #0.016
  48.         self.pos[1] += self.dy * 0.2 #0.016
  49.  
  50.         self.rect.centerx = round(self.pos[0],0)
  51.         self.rect.centery = round(self.pos[1],0)
  52.         self.image = pygame.transform.rotate(self.load_image, self.angle)
  53.         if self.rect.x >= 720:
  54.             self.kill()
  55.         elif self.rect.x <= 0:
  56.             self.kill()
  57.         elif self.rect.y >= 560:
  58.             self.kill()
  59.         elif self.rect.y <= 0:
  60.             self.kill()
  61.  
  62.         if pygame.sprite.spritecollide(self, self.block_group, False):
  63.            self.kill()
RAW Paste Data
Top