Yeonje Kim Yeonje Kim - 14 days ago 5
Python Question

pygame: I am trying to shoot a bullet from my Tank but I can't find the start point of the bullet

part of the Tanks class:

(in the code below: if there is no word: 'turret' included in any variable down below then the variable is about the tank)

class Tanks(pygame.sprite.Sprite):
def __init__(self,image,location,angle,speed,x_change,y_change,turret_image,turret_angle):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.turret_image = pygame.image.load(turret_image)

#Trect is turret rect
self.Trect = self.turret_image.get_rect()

self.rect = self.image.get_rect()
self.rect.left,self.rect.top=location
self.Trect.centerx,self.Trect.bottom = self.rect.centerx,self.rect.centery


after some rotating code...

RealTank.image,rect,turret_image,Trect have changed to rotated image

screen.blit(RealTank.image,RealTank.rect)
screen.blit(RealTank.turret_image,RealTank.Trect)


angle of the turret is being changed by key 'a' and key 'd'

if event.key == pygame.K_a:
turret_angle_change = 5
elif event.key == pygame.K_d:
turret_angle_change = -5


for a test I have drawn a red circle in order to find the start point for my bullet

pygame.draw.circle(screen,red,[RealTank.Trect.centerx,RealTank.Trect.top],5,0)


but strangely, the circle rotates in a strange way

Does anyone know what I have done wrong?

===========================================================================

all of my code down below (not necessary)

pictures:

Tank
Turret

import pygame,math
pygame.init()
red = (155,0,0)
class Tanks(pygame.sprite.Sprite):
def __init__(self,image,location,angle,speed,x_change,y_change,turret_image,turret_angle):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.turret_image = pygame.image.load(turret_image)
#Trect is turret rect
self.Trect = self.turret_image.get_rect()

self.rect = self.image.get_rect()
self.rect.left,self.rect.top=location
self.Trect.centerx,self.Trect.bottom = self.rect.centerx,self.rect.centery
self.angle = angle
self.turret_angle = turret_angle
self.speed = speed
self.x_change = x_change
self.y_change = y_change

if self.turret_angle == 360:
self.turret_angle = 0
if self.angle == 360:
self.angle = 0

def rotate(self):
rot_image = pygame.transform.rotate(self.image,self.angle)
rot_rect = rot_image.get_rect(center = self.rect.center)
return rot_image,rot_rect

def turret_rotate(self):
turret_rot_image = pygame.transform.rotate(self.turret_image,self.turret_angle)
turret_rot_rect = turret_rot_image.get_rect(center = self.Trect.midbottom)
return turret_rot_image,turret_rot_rect

def moving_after_angle_change(self):
x=round(math.cos(math.radians(self.angle+90)),1)*self.speed
y=round(math.sin(math.radians(self.angle-90)),1)*self.speed
return x,y

def shoot(self):
#print(self.turret_angle)
print(self.Trect.x,self.Trect.y)

print(pygame.mouse.get_pos())

#initial
player_image_str = 'Tank.png'
player_turret_str = 'turret - Copy.png'

clock = pygame.time.Clock()
FPS = 30

display_width,display_height = 900,600
screen = pygame.display.set_mode([display_width,display_height])

player_location = [600,300]
player_angle = 0
player_angle_change = 0
player_speed = 0
player_x_change=0
player_y_change=0
RealTank_x_change_store=0
RealTank_y_change_store=0

turret_angle = 0
turret_angle_change = 0
#main
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
turret_angle_change = 5
elif event.key == pygame.K_d:
turret_angle_change = -5
elif event.key == pygame.K_UP:
player_speed=2
elif event.key == pygame.K_DOWN:
player_speed=-2
elif event.key == pygame.K_LEFT:
player_angle_change = 2
elif event.key == pygame.K_RIGHT:
player_angle_change = -2
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
turret_angle_change = 0
elif event.key == pygame.K_UP:
player_speed = 0
elif event.key == pygame.K_DOWN:
player_speed = 0
elif event.key == pygame.K_LEFT:
player_angle_change = 0
elif event.key == pygame.K_RIGHT:
player_angle_change = 0

player_angle+=player_angle_change
turret_angle+=turret_angle_change
RealTank = Tanks(player_image_str,player_location,player_angle,player_speed,
player_x_change,player_y_change,player_turret_str,turret_angle)

RealTank.image,RealTank.rect=RealTank.rotate()

RealTank.turret_image,RealTank.Trect = RealTank.turret_rotate()

RealTank.x_change,RealTank.y_change=RealTank.moving_after_angle_change()

RealTank_x_change_store += RealTank.x_change
RealTank_y_change_store += RealTank.y_change
RealTank.Trect.centerx +=RealTank_x_change_store
RealTank.Trect.centery +=RealTank_y_change_store
RealTank.rect.centerx += RealTank_x_change_store
RealTank.rect.centery += RealTank_y_change_store
RealTank.shoot()
screen.fill([0,0,0])
screen.blit(RealTank.image,RealTank.rect)
screen.blit(RealTank.turret_image,RealTank.Trect)
######### for test only
pygame.draw.circle(screen,red,[RealTank.Trect.centerx,RealTank.Trect.top],5,0)
######
pygame.display.update()
clock.tick(FPS)

pygame.quit()
quit()

Answer

You need something like this

a = RealTank.Trect.centerx + math.cos(math.radians(RealTank.turret_angle+90))*55
b = RealTank.Trect.centery + math.sin(math.radians(RealTank.turret_angle-90))*55

pygame.draw.circle(screen,red,[int(a), int(b)],5,0)

You have to use turret length instead of 100

And maybe it need some calculations because it doesn't move smoothly.


EDIT: I removed round() and now it moves smoother. (And I changed 100 into 55).

Comments