|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pygame |
| 4 | + |
| 5 | +# --- constants --- |
| 6 | + |
| 7 | +WIDTH = 800 |
| 8 | +HEIGHT = 800 |
| 9 | + |
| 10 | +FPS = 25 |
| 11 | + |
| 12 | +BLACK = (0, 0, 0) |
| 13 | +WHITE = (250, 250, 250) |
| 14 | + |
| 15 | +BULLET_SPEED = 5 |
| 16 | + |
| 17 | +# --- classes --- |
| 18 | + |
| 19 | +#empty |
| 20 | + |
| 21 | +# --- functions --- |
| 22 | + |
| 23 | +#empty |
| 24 | + |
| 25 | +# --- main --- |
| 26 | + |
| 27 | +# - init - |
| 28 | + |
| 29 | +pygame.init() |
| 30 | + |
| 31 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 32 | +screen_rect = screen.get_rect() |
| 33 | + |
| 34 | +# - objects - |
| 35 | + |
| 36 | +bullets = [] |
| 37 | + |
| 38 | +# center of screen as Vector2 |
| 39 | +#center = pygame.math.Vector2(WIDTH//2, HEIGHT//2) |
| 40 | +center = pygame.math.Vector2(screen.get_rect().center) |
| 41 | + |
| 42 | +shot = False |
| 43 | + |
| 44 | +# - mainloop - |
| 45 | + |
| 46 | +clock = pygame.time.Clock() |
| 47 | + |
| 48 | +running = True |
| 49 | + |
| 50 | +while running: |
| 51 | + |
| 52 | + for event in pygame.event.get(): |
| 53 | + if event.type == pygame.QUIT: |
| 54 | + running = False |
| 55 | + |
| 56 | + elif event.type == pygame.KEYDOWN: |
| 57 | + if event.key == pygame.K_ESCAPE: |
| 58 | + running = False |
| 59 | + |
| 60 | + elif event.type== pygame.MOUSEBUTTONDOWN: |
| 61 | + if event.button == 1: |
| 62 | + shot = event.pos |
| 63 | + |
| 64 | + elif event.type== pygame.MOUSEBUTTONUP: |
| 65 | + if event.button == 1: |
| 66 | + shot = None |
| 67 | + |
| 68 | + elif event.type== pygame.MOUSEMOTION: |
| 69 | + if shot: |
| 70 | + shot = event.pos |
| 71 | + |
| 72 | + |
| 73 | + # create new bullet |
| 74 | + if shot: |
| 75 | + # get vector from center to mouse position |
| 76 | + vector = shot - center |
| 77 | + # `center` is `Vector2` so `vector` will be `Vector2` too |
| 78 | + #print(type(vector)) |
| 79 | + |
| 80 | + # normalize |
| 81 | + normal = vector.normalize() |
| 82 | + |
| 83 | + # create speed vector |
| 84 | + speed = normal * BULLET_SPEED |
| 85 | + |
| 86 | + # move object (first move 15 times bigger then next moves ) |
| 87 | + pos = center# + (speed * 15) |
| 88 | + pos = pygame.math.Vector2(center) |
| 89 | + |
| 90 | + previous = pygame.math.Vector2(center) |
| 91 | + |
| 92 | + #pygame.draw.line(screen, (255,150,100), (pos.x, pos.y), (center.x, center.y)) |
| 93 | + pygame.draw.line(screen, (255,150,100), pos, previous) |
| 94 | + |
| 95 | + # remeber position and speed as one object |
| 96 | + bullets.append( [pos, speed, previous] ) |
| 97 | + |
| 98 | + # - updates (without draws) - |
| 99 | + |
| 100 | + new_bullets = [] |
| 101 | + |
| 102 | + for pos, speed, previous in bullets: |
| 103 | + #previous.x = pos.x |
| 104 | + #previous.y = pos.y |
| 105 | + |
| 106 | + # move |
| 107 | + pos += speed |
| 108 | + |
| 109 | + if screen_rect.collidepoint(pos): |
| 110 | + new_bullets.append( [pos, speed, previous] ) |
| 111 | + |
| 112 | + bullets = new_bullets |
| 113 | + |
| 114 | + # - draws - |
| 115 | + |
| 116 | + screen.fill(BLACK) |
| 117 | + |
| 118 | + for pos, speed, previous in bullets: |
| 119 | + # draw |
| 120 | + #pygame.draw.line(screen, (255,150,100), pos, previous) |
| 121 | + pygame.draw.rect(screen, WHITE, (pos.x, pos.y, 2, 2)) |
| 122 | + |
| 123 | + pygame.display.update() |
| 124 | + |
| 125 | + # - speed - |
| 126 | + |
| 127 | + clock.tick(FPS) |
| 128 | + |
| 129 | +# - end - |
| 130 | + |
| 131 | +pygame.quit() |
| 132 | +quit() |
| 133 | + |
0 commit comments