Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import pygame
- from pygame import Vector2, Rect
- from pygame.locals import *
- class bullet:
- def __init__(s, source, target):
- degs = Vector2().angle_to(target - source)
- s.velocity = Vector2(1, 0).rotate(degs) * 5
- s.pos = Vector2(source)
- s.dead = False
- def draw(s):
- global PDS, RED
- pygame.draw.circle(PDS, RED, s.pos, 6)
- def update(s):
- global deltatime
- s.pos += s.velocity * deltatime
- if s.pos.x < -3 or s.pos.x > PDR.w + 3 or s.pos.y < -3 or s.pos.y > PDR.h + 3:
- s.dead = True
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- pygame.init()
- PDR = Rect(0, 0, 1280, 720)
- PDS = pygame.display.set_mode(PDR.size)
- FPS = 120
- base = Vector2(PDR.center)
- bullets = []
- bullet_timestamp = time.time() - 0.5
- deltatime_timestamp = time.time()
- exit_demo = False
- while not exit_demo:
- for e in pygame.event.get():
- if e.type == KEYUP and e.key == K_ESCAPE:
- exit_demo = True
- now = time.time()
- deltatime = (now - deltatime_timestamp) * FPS
- deltatime_timestamp = now
- PDS.fill(BLACK)
- for b in bullets:
- b.draw()
- pygame.draw.circle(PDS, WHITE, base, 50)
- pygame.display.update()
- deadbullets = []
- for b in bullets:
- b.update()
- if b.dead:
- deadbullets.append(b)
- for b in deadbullets:
- bullets.remove(b)
- mp = Vector2(pygame.mouse.get_pos())
- mb = pygame.mouse.get_pressed()
- if mb[0] and now - bullet_timestamp > 0.1:
- bullets.append(bullet(base, mp))
- bullet_timestamp = now
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement