Windspar

Pygame Example Mass Bullet

Jan 7th, 2023 (edited)
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 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.  
  49.         # Added Second Bullet
  50.         offset = self.vector.rotate(90)
  51.         position += offset
  52.         bullet = Bullet(self.vector.copy(), 90, image, position)
  53.         self.bullets.add(bullet)
  54.  
  55.     def update(self, delta, ticks):
  56.         if self.ticks == -1:
  57.             self.ticks = ticks
  58.         else:
  59.             while ticks > self.ticks:
  60.                 self.shoot()
  61.                 self.ticks += self.rate_of_fire
  62.                 self.vector.rotate_ip(2)
  63.  
  64. class Pen:
  65.     def __init__(self, font, color):
  66.         self.font = font
  67.         self.color = color
  68.  
  69.     def render(self, text):
  70.         return self.font.render(text, 1, self.color)
  71.  
  72. class Label(Sprite):
  73.     def __init__(self, name, pen, position):
  74.         super().__init__()
  75.         self.pen = pen
  76.         self.name = name
  77.         self.image = pen.render(name)
  78.         self.rect = self.image.get_rect(topleft=position)
  79.  
  80.     def update_value(self, value):
  81.         self.image = self.pen.render(self.name + " {}".format(value))
  82.         self.rect = self.image.get_rect(topleft = self.rect.topleft)
  83.  
  84. def main():
  85.     pygame.display.set_caption("Shooters")
  86.     surface = pygame.display.set_mode((800, 600))
  87.     clock = pygame.time.Clock()
  88.     rect = surface.get_rect()
  89.     running = True
  90.     delta = 0
  91.     fps = 60
  92.  
  93.     shooters = []
  94.     colors = list(pygame.color.THECOLORS.keys())
  95.     direction = pygame.Vector2()
  96.     number_of_shooters = 100
  97.     for i in range(number_of_shooters):
  98.         position = random.randint(50, rect.w - 50), random.randint(50, rect.h - 50)
  99.         direction.from_polar((1, random.randint(0, 360)))
  100.         shooters.append(Shooter(position,
  101.                                 random.choice(colors),
  102.                                 direction.copy()))
  103.  
  104.     lab_interval = 100
  105.     lab_ticks = 0
  106.     pen = Pen(pygame.font.Font(None, 30), 'lightblue')
  107.     lab_count = Label("Bullet Count", pen, (10, 10))
  108.     lab_fps =Label("Fps", pen, (rect.centerx, 10))
  109.     labels = Group(lab_count, lab_fps)
  110.  
  111.     while running:
  112.         for event in pygame.event.get():
  113.             if event.type == pygame.QUIT:
  114.                 running = False
  115.  
  116.         surface.fill('black')
  117.         for shooter in shooters:
  118.             shooter.bullets.draw(surface)
  119.             shooter.bullets.update(delta, rect)
  120.  
  121.         labels.draw(surface)
  122.  
  123.         ticks = pygame.time.get_ticks()
  124.         for shooter in shooters:
  125.             shooter.update(delta, ticks)
  126.  
  127.         if ticks > lab_ticks:
  128.             lab_ticks = ticks + lab_interval
  129.             count = sum([len(s.bullets) for s in shooters])
  130.             lab_count.update_value(count)
  131.             lab_fps.update_value(int(clock.get_fps()))
  132.  
  133.         pygame.display.flip()
  134.         delta = clock.tick(fps) * 0.001
  135.  
  136. pygame.init()
  137. main()
  138. pygame.quit()
  139.  
Advertisement
Add Comment
Please, Sign In to add comment