Advertisement
cookertron

Luke James - Bullets Bullets Bullets

Mar 11th, 2022
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import time
  2. import pygame
  3. from pygame import Vector2, Rect
  4. from pygame.locals import *
  5.  
  6. class bullet:
  7.     def __init__(s, source, target):
  8.         degs = Vector2().angle_to(target - source)
  9.         s.velocity = Vector2(1, 0).rotate(degs) * 5
  10.         s.pos = Vector2(source)
  11.         s.dead = False
  12.  
  13.     def draw(s):
  14.         global PDS, RED
  15.  
  16.         pygame.draw.circle(PDS, RED, s.pos, 6)
  17.  
  18.     def update(s):
  19.         global deltatime
  20.  
  21.         s.pos += s.velocity * deltatime
  22.         if s.pos.x < -3 or s.pos.x > PDR.w + 3 or s.pos.y < -3 or s.pos.y > PDR.h + 3:
  23.             s.dead = True
  24.  
  25. WHITE = (255, 255, 255)
  26. BLACK = (0, 0, 0)
  27. RED = (255, 0, 0)
  28.  
  29. pygame.init()
  30. PDR = Rect(0, 0, 1280, 720)
  31. PDS = pygame.display.set_mode(PDR.size)
  32. FPS = 120
  33.  
  34. base = Vector2(PDR.center)
  35. bullets = []
  36.  
  37. bullet_timestamp = time.time() - 0.5
  38. deltatime_timestamp = time.time()
  39.  
  40. exit_demo = False
  41. while not exit_demo:
  42.     for e in pygame.event.get():
  43.         if e.type == KEYUP and e.key == K_ESCAPE:
  44.             exit_demo = True
  45.    
  46.     now = time.time()
  47.     deltatime = (now - deltatime_timestamp) * FPS
  48.     deltatime_timestamp = now
  49.  
  50.     PDS.fill(BLACK)
  51.    
  52.     for b in bullets:
  53.         b.draw()
  54.     pygame.draw.circle(PDS, WHITE, base, 50)
  55.     pygame.display.update()
  56.  
  57.     deadbullets = []
  58.     for b in bullets:
  59.         b.update()
  60.         if b.dead:
  61.             deadbullets.append(b)
  62.     for b in deadbullets:
  63.         bullets.remove(b)
  64.    
  65.     mp = Vector2(pygame.mouse.get_pos())
  66.     mb = pygame.mouse.get_pressed()
  67.  
  68.     if mb[0] and now - bullet_timestamp > 0.1:
  69.         bullets.append(bullet(base, mp))
  70.         bullet_timestamp = now
  71.  
  72. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement