0

I've decided that I'm going to make a top down shooter game, but I can't figure out how to do omni directional shooting. Every website and video is based on pygame, not zero and this is my code so far:

#Imports
import pgzrun
import random

#Pygame Zero window size
WIDTH = 400
HEIGHT = 400

#Actors
floor = Actor("floor")
sadboy = Actor("sadboy")

#Variables
sadboy.x = WIDTH//2
sadboy.y = HEIGHT//2

#Lists
sadtears = []
        
def update():
    #Movement
    if keyboard.a:
        sadboy.x -= 2
    if keyboard.d:
        sadboy.x += 2
    if keyboard.w:
        sadboy.y -= 2
    if keyboard.s:
        sadboy.y += 2
            
#Drawing floor and character
def draw():
    screen.clear()
    screen.blit("floor", (0, 0))
    sadboy.draw()
    for sadtear in sadtears:
        sadtear.draw()
  
def on_key_down(key):
    if key == keys.UP:
        sadtears.append(Actor('sadtear'))
        sadtears[-1].x = sadboy.x
        sadtears[-1].y = sadboy.y
        sadtear.y -= 5
    if key == keys.DOWN:
        sadtears.append(Actor('sadtear'))
        sadtears[-1].x = sadboy.x
        sadtears[-1].y = sadboy.y
        sadtear.y += 5
    if key == keys.LEFT:
        sadtears.append(Actor('sadtear'))
        sadtears[-1].x = sadboy.x
        sadtears[-1].y = sadboy.y
        sadtear.x -= 5
    if key == keys.RIGHT:
        sadtears.append(Actor('sadtear'))
        sadtears[-1].x = sadboy.x
        sadtears[-1].y = sadboy.y
        sadtear.x += 5
        
pgzrun.go()

If anyone could help me, please do because I have been struggling on this for a while. Thanks a lot!

  • I don't know what means `omni directional shooting`. If you want to move in some direction then you need `angle` and then you can calculate `dx = speed * sin(angle)`, `dy = speed * cos(angle)` (or maybe first should be `cos()` and second `sin()`). And PyGame has `pygame.math` to make it simpler. And `pgzero` should also have it. – furas Jul 15 '21 at 04:52
  • and if you move only `left, right, up, down` then you can reduced it to values `dx = -speed, dy = 0` for `left`, `dx = 0, dy = -speed` for `up`, etc. And you should use them in `update` to change `x, y` – furas Jul 15 '21 at 05:01

1 Answers1

0

I don't know if I understand you and I can't test if it works but I would set speed_x, speed_y for every bullet

def on_key_down(key):
    if key == keys.UP:
        bullet = Actor('sadtear')
        bullet.x = sadboy.x
        bullet.y = sadboy.y
        bullet.speed_x = 0
        bullet.speed_y = -5
        sadtears.append(bullet)

(other directions need different values speed_x, speed_y)

and I would use it in update() to move it

def update():
       
    for bullet in sadtears:
        bullet.x += bullet.speed_x
        bullet.y += bullet.speed_y
furas
  • 134,197
  • 12
  • 106
  • 148