Advertisement
Windspar

Pygame Example Mass Bullet

Jan 7th, 2023 (edited)
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | Gaming | 0 0
  1. import pygame
  2. import random
  3. from pygame.sprite import Group, Sprite
  4.  
  5. class Entity(Sprite):
  6.     def __init__(self, image, position, anchor="center"):
  7.         super().__init__()
  8.         self.image = image
  9.         self.rect = image.get_rect()
  10.         setattr(self.rect, anchor, position)
  11.         self.center = pygame.Vector2(self.rect.center)
  12.  
  13.     def move(self, vector):
  14.         self.center += vector
  15.         self.rect.center = self.center
  16.  
  17. class Bullet(Entity):
  18.     def __init__(self, vector, speed, image, position, anchor="center"):
  19.         super().__init__(image, position, anchor)
  20.         self.vector = vector
  21.         self.speed = speed
  22.  
  23.     def update(self, delta, area):
  24.         self.move(self.vector * self.speed * delta)
  25.         if not self.rect.colliderect(area):
  26.             self.kill()
  27.  
  28. class Shooter:
  29.     def __init__(self, position, color, direction):
  30.         self.bullets = Group()
  31.         self.vector = pygame.Vector2(direction) * 8
  32.         self.position = pygame.Vector2(position)
  33.         self.create_bullet(color)
  34.         # Higher number is slower
  35.         self.rate_of_fire = 30
  36.         self.ticks = -1
  37.  
  38.     def create_bullet(self, color):
  39.         self.image = pygame.Surface((4, 1), pygame.SRCALPHA)
  40.         self.image.fill(color)
  41.  
  42.     def shoot(self):
  43.         position = self.position + self.vector
  44.         angle = self.vector.as_polar()[1]
  45.         image = pygame.transform.rotate(self.image, -angle)
  46.         bullet = Bullet(self.vector.copy(), 90, image, position)
  47.         self.bullets.add(bullet)
  48.         # Added Second Bullet
  49.         offset = self.vector.rotate(90)
  50.         position += offset
  51.         bullet = Bullet(self.vector.copy(), 90, image, position)
  52.         self.bullets.add(bullet)
  53.  
  54.     def update(self, delta, ticks):
  55.         if self.ticks == -1:
  56.             self.ticks = ticks
  57.         else:
  58.             while ticks > self.ticks:
  59.                 self.shoot()
  60.                 self.ticks += self.rate_of_fire
  61.                 self.vector.rotate_ip(2)
  62.  
  63. class Pen:
  64.     def __init__(self, font, color):
  65.         self.font = font
  66.         self.color = color
  67.  
  68.     def render(self, text):
  69.         return self.font.render(text, 1, self.color)
  70.  
  71. class Label(Sprite):
  72.     def __init__(self, name, pen, position):
  73.         super().__init__()
  74.         self.pen = pen
  75.         self.name = name
  76.         self.image = pen.render(name)
  77.         self.rect = self.image.get_rect(topleft=position)
  78.  
  79.     def update_value(self, value):
  80.         self.image = self.pen.render(self.name + " {}".format(value))
  81.         self.rect = self.image.get_rect(topleft = self.rect.topleft)
  82.  
  83. def main():
  84.     pygame.display.set_caption("Shooters")
  85.     surface = pygame.display.set_mode((800, 600))
  86.     clock = pygame.time.Clock()
  87.     rect = surface.get_rect()
  88.     running = True
  89.     delta = 0
  90.     fps = 60
  91.  
  92.     shooters = []
  93.     colors = list(pygame.color.THECOLORS.keys())
  94.     direction = pygame.Vector2()
  95.     number_of_shooters = 100
  96.     for i in range(number_of_shooters):
  97.         position = random.randint(50, rect.w - 50), random.randint(50, rect.h - 50)
  98.         direction.from_polar((1, random.randint(0, 360)))
  99.         shooters.append(Shooter(position,
  100.                                 random.choice(colors),
  101.                                 direction.copy()))
  102.  
  103.     lab_interval = 100
  104.     lab_ticks = 0
  105.     pen = Pen(pygame.font.Font(None, 30), 'lightblue')
  106.     lab_count = Label("Bullet Count", pen, (10, 10))
  107.     lab_fps =Label("Fps", pen, (rect.centerx, 10))
  108.     labels = Group(lab_count, lab_fps)
  109.  
  110.     while running:
  111.         for event in pygame.event.get():
  112.             if event.type == pygame.QUIT:
  113.                 running = False
  114.  
  115.         surface.fill('black')
  116.         for shooter in shooters:
  117.             shooter.bullets.draw(surface)
  118.             shooter.bullets.update(delta, rect)
  119.  
  120.         labels.draw(surface)
  121.  
  122.         ticks = pygame.time.get_ticks()
  123.         for shooter in shooters:
  124.             shooter.update(delta, ticks)
  125.  
  126.         if ticks > lab_ticks:
  127.             lab_ticks = ticks + lab_interval
  128.             count = sum([len(s.bullets) for s in shooters])
  129.             lab_count.update_value(count)
  130.             lab_fps.update_value(int(clock.get_fps()))
  131.  
  132.         pygame.display.flip()
  133.         delta = clock.tick(fps) * 0.001
  134.  
  135. pygame.init()
  136. main()
  137. pygame.quit()
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement