[ad_1]
My first time posting, if I’m posting in flawed place, I apologize and please let me
know. I’m new to Python and simply doing it as passion.
I’m making an attempt to jot down a recreation 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 (I simply can’t appear to do it) on the best way to hearth a shell up in a curved arc and have it come again down on a goal (the mouse place) any enable you guys may give on this might be nice.
I’ve looked for some factor like what I’d like, however cannot discover something.
The 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(middle = (self.x, self.y))
self.rotation = True
self.weapon = "gun"
def replace(self):
player_pos = self.picture.get_rect(middle = (self.x, self.y))
player_rect = self.picture.get_rect(middle = (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(middle = player_rect.middle)
display.blit(rot_image, rot_image_rect.topleft)
class Shell_circle:
def __init__(self, coloration, x, y, width, top, velocity):
self.rect = pygame.Rect(x,y,width,top)
self.coloration = coloration
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, coloration, x, y, width, top, velocity, targetx,targety):
tremendous().__init__(coloration, x, y, width, top, 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 = []
#Main program loop
carried out = False
whereas not carried out:
display.fill(white) #fill display with black
#Get person enter
for occasion in pygame.occasion.get():
if occasion.sort == pygame.QUIT:
carried out = True
if occasion.sort == 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()
#Update recreation objects
for s in shells:
s.transfer()
s.draw(display)
pygame.show.replace()
clock.tick(30) #30 FPS
pygame.stop()
exit()
[ad_2]