我试图使用pygame组模块拍摄多个子弹但我不知道如何正确地做到这一点

时间:2016-11-19 11:52:08

标签: python pygame

问题1:我正在尝试使用pygame组类模块最多射出5个子弹但我最多只能射出1个子弹......

这是子弹类:

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

下面:我已将'bullet'设为精灵

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)

在主要的while循环之前:

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

我制作了5个子弹精灵

在下面的主要代码中:

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

有谁知道我做错了什么????

问题2: 当我'打印(group.sprites)'并按'空格键'时,每次按下它都会得到更多的精灵......

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

以下是我的所有代码 :( 你不需要看到它......没有必要

尝试下面的代码,以查看我的问题

图片:

Tank turret bullet

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()

1 个答案:

答案 0 :(得分:1)

您只创建一个项目实例 - 您需要5个实例

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()

     

向此组添加任意数量的Sprite。 这只会添加精灵   尚未成为本集团成员的人。

while loop内,您创建新子弹bullet = Bullets()(变量名称bullet并不重要,id(bullet)无关紧要)并尝试将其删除({{ 1}})但是这个实例不在组中,因此它不会删除任何内容。然后你添加项目符号(remove(bullet))并添加它,因为这个实例还不在组中。