Saturday, October 15, 2022
HomeGame Developmentpython - Mouse place as goal, with arced (curved) shot

python – Mouse place as goal, with arced (curved) shot


I’m attempting to write down a sport with artillery mortar hearth I’ve one other gun adjusting & firing
in direction of the mouse, however want to change weapons.

I’m having issues with learn how to hearth a shell up in a curved arc and have it come again down on a goal (the mouse place).

I’ve looked for some factor like what I might like, however cannot discover something.

This code is for my participant gun rotating and firing towards the mouse:

import pygame, math, random
import os


pygame.init()

#Initialize variables:
clock = pygame.time.Clock()
screen_width = 960
screen_height = 720
display = pygame.show.set_mode((screen_width,screen_height))
inexperienced = 0,255,0
crimson = 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
        tremendous().__init__()
            self.picture = pygame.picture.load(os.path.be a part of('sprites', 'player_gun.png'))
            self.rect = self.picture.get_rect(heart = (self.x, self.y))
        self.rotation = True
        self.weapon = "gun"

    def replace(self):
        player_pos  = self.picture.get_rect(heart = (self.x, self.y))
        player_rect = self.picture.get_rect(heart = (self.x, self.y))

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

        rot_image = pygame.remodel.rotate(self.picture, angle)
        angle2 = math.levels(math.atan2(-dy, dx))
        rot_image = pygame.remodel.rotate(self.picture, angle2)

        if angle < 0 and angle > -179.99:
            angle = 0
            rot_image = pygame.remodel.rotate(self.picture, angle)
        if angle > 90 and angle < 180.99:
            angle = 90
            rot_image = pygame.remodel.rotate(self.picture, angle)
        rot_image_rect = rot_image.get_rect(heart = player_rect.heart)
        display.blit(rot_image, rot_image_rect.topleft)

    class Shell_circle:
        def __init__(self, shade, x, y, width, peak, velocity):
        self.rect = pygame.Rect(x,y,width,peak)
        self.shade = shade
        self.velocity = velocity
        self.radius = 2.8
        def draw(self, display):
        pygame.draw.circle(display,black,(self.x, self.y),self.radius) # Draw shell circle

    class Shell(Shell_circle):
    def __init__(self, shade, x, y, width, peak, velocity, targetx,targety):
        tremendous().__init__(shade, x, y, width, peak, velocity)
        self.angle = math.atan2(targety-y, targetx-x) #get angle to focus on in radians
        self.velocity = velocity
        self.dx = math.cos(self.angle)*self.velocity
        self.dy = math.sin(self.angle)*self.velocity
        self.x = x
        self.y = y
    def transfer(self):


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

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

        if not display.get_rect().collidepoint(self.x,self.y):
            shells.take away(s)
#   Object arrange
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 = []

#Principal program loop
carried out = False
whereas not carried out:

    display.fill(white) #fill display with black

    #Get consumer enter
    for occasion in pygame.occasion.get():
        if occasion.kind == pygame.QUIT:
            carried out = True

        if occasion.kind == 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.replace()

    #Replace sport objects
    for s in shells:
        s.transfer()
        s.draw(display)

    pygame.show.replace()
    clock.tick(30) #30 FPS
pygame.give up()
exit()

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments