0

I am trying to write a game with artillery mortar fire I have another gun adjusting & firing towards the mouse, but would like to switch weapons.

I am having problems with how to fire a shell up in a curved arc and have it come back down on a target (the mouse position).

I have searched for some thing like what I'd like, but can't find anything.

This code is for my player gun rotating and firing toward the mouse:

import pygame, math, random
import os

pygame.init()

#Initialize variables: clock = pygame.time.Clock() screen_width = 960 screen_height = 720 screen = pygame.display.set_mode((screen_width,screen_height)) green = 0,255,0 red = 255,0,0 blue = 0,0,255 yellow = 255,255,0 white = 255,255,255 black = 0,0,0

gun_pos_x = 116 gun_pos_y = 589

class Player_gunner(pygame.sprite.Sprite):
    def __init__(self, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    super().__init__()
        self.image = pygame.image.load(os.path.join('sprites', 'player_gun.png'))
        self.rect = self.image.get_rect(center = (self.x, self.y))
    self.rotation = True
    self.weapon = "gun"

def update(self):
    player_pos  = self.image.get_rect(center = (self.x, self.y))
    player_rect = self.image.get_rect(center = (self.x, self.y))

    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - player_rect.centerx, my - player_rect.centery
    angle = math.degrees(math.atan2(-dy, dx))

    rot_image = pygame.transform.rotate(self.image, angle)
    angle2 = math.degrees(math.atan2(-dy, dx))
    rot_image = pygame.transform.rotate(self.image, angle2)

    if angle < 0 and angle > -179.99:
        angle = 0
        rot_image = pygame.transform.rotate(self.image, angle)
    if angle > 90 and angle < 180.99:
        angle = 90
        rot_image = pygame.transform.rotate(self.image, angle)
    rot_image_rect = rot_image.get_rect(center = player_rect.center)
    screen.blit(rot_image, rot_image_rect.topleft)

class Shell_circle:
    def __init__(self, color, x, y, width, height, speed):
    self.rect = pygame.Rect(x,y,width,height)
    self.color = color
    self.speed = speed
    self.radius = 2.8
    def draw(self, screen):
    pygame.draw.circle(screen,black,(self.x, self.y),self.radius) # Draw shell circle

class Shell(Shell_circle):
def __init__(self, color, x, y, width, height, speed, targetx,targety):
    super().__init__(color, x, y, width, height, speed)
    self.angle = math.atan2(targety-y, targetx-x) #get angle to target in radians
    self.speed = speed
    self.dx = math.cos(self.angle)*self.speed
    self.dy = math.sin(self.angle)*self.speed
    self.x = x
    self.y = y
def move(self):


    self.x = self.x + self.dx
    self.y = self.y + self.dy

    if self.x <= 120:
        self.x = self.x = 120

    if not screen.get_rect().collidepoint(self.x,self.y):
        shells.remove(s)

Object set up

player_gunner = Player_gunner(gun_pos_x, gun_pos_y) player_gunner.rect.x = gun_pos_x # go to x player_gunner.rect.y = gun_pos_y # go to y player_gun = pygame.sprite.Group() player_gun.add(Player_gunner(player_gunner.rect.x, player_gunner.rect.y))

#Sprite Group lists shells = []

#Main program loop done = False while not done:

screen.fill(white) #fill screen with black

#Get user input
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True

    if event.type == pygame.MOUSEBUTTONDOWN:
        x,y = pygame.mouse.get_pos()
        s = Shell(black, gun_pos_x, gun_pos_y, 4,4, 12, x,y)
        shells.append(s)

player_gun.update()

#Update game objects
for s in shells:
    s.move()
    s.draw(screen)

pygame.display.update()
clock.tick(30) #30 FPS

pygame.quit() exit()

DMGregory
  • 134,153
  • 22
  • 242
  • 357
Tomjtp
  • 1
  • "Can't find anything" really? I could swear I'd written at least a few answers on the [tag:projectile-physics] tag showing how to aim/determine the initial velocity of a lobbed projectile so it's parabolic arc hits a given target... Key words like "ballistic" and "parabola" help. When you search this past Q&A, are you able to find your solution, or do you need help adapting any particular parts to your use case? – DMGregory Oct 15 '22 at 12:20
  • @DMGregory I appreciate all the work you have done, but I just looked at your link and its for Unity I have no clue what I am looking at, I would like some thing I can understand, I have also searched with those keywords and still nothing, Thank you. – Tomjtp Oct 15 '22 at 13:48
  • Can you edit your question to walk through the example code and point out what parts you need help understanding? The more precisely you can identify the parts you need help with, the better folks can target answers to help you with those parts. – DMGregory Oct 15 '22 at 18:25

0 Answers0