Yeonje Kim Yeonje Kim - 18 days ago 5
Python Question

I am trying to shoot multiple of bullets using pygame group module but I don't know how to do it properly

Problem 1: I am trying to shoot maximum of 5 bullets using pygame group class module but I can only shoot maximum of 1 bullet...

This is the bullet class:

class Bullets(pygame.sprite.Sprite):
def __init__(self,image,location):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect=self.image.get_rect()
self.location =location
self.rect.left,self.rect.top = self.location


Down below: I have set 'bullet' as a sprite

def bullet_group(group):
for bullet in group:
print(group.sprites)

group.remove(bullet)
# After shooting a bullet, I want to remove the bullet from the group
# in order to change the 'shoot_point'(which is the location of the bullet)
# back to where it starts

shoot_point_x = fixed_shoot_point_x #fixed_shoot_point is the start location
shoot_point_y = fixed_shoot_point_y #of the bullet

group.add(bullet)


Just before the main while loop:

group=pygame.sprite.Group()
for i in range(0,5):
group.add(bullet)


I made 5 bullet sprites

In the main code down below:

if event.type == pygame.KEYDOWN:
if #some code

elif event.key == pygame.K_SPACE:
fixed_bullet_speed_change = [round(-(math.cos(math.atan2(RealTank.Trect.centery-shoot_point_y,RealTank.Trect.centerx-shoot_point_x))),1)*10,round(-(math.sin(math.atan2(RealTank.Trect.centery-shoot_point_y,RealTank.Trect.centerx-shoot_point_x))),1)*10]
# the line of code above is just some calculations that is not related to the question

group.remove(bullet)
# I removed the sprite from the group after shooting some bullets, in order to
# shoot another one

if event.type == pygame.KEYUP:
if #some code

elif event.key == pygame.K_SPACE:
group.add(bullet)
# adding back another one


Does anyone know what I am doing wrong????

Problem 2:
When I do 'print(group.sprites)' and press 'SPACEBAR', I get more sprites every time I press it...

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

Here's all of my code down below:(you don't need to see it...it isn't necessary)

Try the code down below, to visualize my problem

pictures:

Tank
turretbullet

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,bullet_image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.turret_image = pygame.image.load(turret_image)
self.bullet_image = pygame.image.load(bullet_image)
self.Brect = self.bullet_image.get_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


print(self.angle)

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 bullet_rotate(self):
## bullet_rot_image = pygame.transform.rotate(self.bullet_image,self.turret_angle)
## bullet_rot_rect = bullet_rot_image.get_rect(center = )

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_point(self):
#print(self.turret_angle)
shoot_point_x = RealTank.Trect.centerx + math.cos(math.radians(RealTank.turret_angle+90))*55
shoot_point_y = RealTank.Trect.centery + math.sin(math.radians(RealTank.turret_angle-90))*55
return shoot_point_x,shoot_point_y

class Bullets(pygame.sprite.Sprite):
def __init__(self,image,location):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect=self.image.get_rect()
self.location =location
self.rect.left,self.rect.top = self.location

def bullet_group(group):
for bullet in group:
print(group.sprites)
group.remove(bullet)
shoot_point_x = fixed_shoot_point_x
shoot_point_y = fixed_shoot_point_y
group.add(bullet)

#initial
player_image_str = 'Tank.png'
player_turret_str = 'turret - Copy.png'
player_gold_bullet_str = 'bullet.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
bullet_speed_change = [0,0]
fixed_bullet_speed = [0,0]
fixed_bullet_speed_change = [0,0]
shoot_point_x = 0
shoot_point_y=0
fixed_shoot_point_x=0
fixed_shoot_point_y=0
#main
RealTank = Tanks(player_image_str,player_location,player_angle,player_speed,
player_x_change,player_y_change,player_turret_str,turret_angle,player_gold_bullet_str)
bullet=Bullets(RealTank.bullet_image,[shoot_point_x,shoot_point_y])
group=pygame.sprite.Group()
for i in range(0,5):
group.add(bullet)
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
elif event.key == pygame.K_SPACE:
fixed_bullet_speed_change = [round(-(math.cos(math.atan2(RealTank.Trect.centery-shoot_point_y,RealTank.Trect.centerx-shoot_point_x))),1)*10,round(-(math.sin(math.atan2(RealTank.Trect.centery-shoot_point_y,RealTank.Trect.centerx-shoot_point_x))),1)*10]
group.remove(bullet)
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
elif event.key == pygame.K_SPACE:
group.add(bullet)

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,player_gold_bullet_str)

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

shoot_point_x,shoot_point_y=RealTank.shoot_point()
fixed_shoot_point_x,fixed_shoot_point_y = RealTank.shoot_point()
screen.fill([0,0,0])
screen.blit(RealTank.image,RealTank.rect)

#bullet
print(fixed_bullet_speed_change)
fixed_bullet_speed[0]+=fixed_bullet_speed_change[0]
fixed_bullet_speed[1]+=fixed_bullet_speed_change[1]
shoot_point_x+=fixed_bullet_speed[0]
shoot_point_y+=fixed_bullet_speed[1]
#bullet end
bullet=Bullets(RealTank.bullet_image,[shoot_point_x,shoot_point_y])

#bullet group


bullet_group(group)
#bullet group end
screen.blit(bullet.image,bullet.location)
screen.blit(RealTank.turret_image,RealTank.Trect)

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

pygame.quit()

Answer

You create only one instance of bullet - you need 5 instances

group = pygame.sprite.Group()

for i in range(5):
    bullet = Bullets(RealTank.bullet_image, [shoot_point_x, shoot_point_y])
    group.add(bullet)

Doc: pygame.sprite.Group.add():

Add any number of Sprites to this Group. This will only add Sprites that are not already members of the Group.


Inside while loop you create new bullet bullet = Bullets() (variable name bullet doesn't matter, id(bullet) do matter) and you try to remove it (remove(bullet)) but this instance is not in group so it removes nothing. And later you add bullet (add(bullet)) and it adds it because this instance is not in group yet.