我试图用pygame group模块射出多颗子弹,但我不知道如何正确地完成

2024-04-26 04:26:29 发布

您现在位置:Python中文网/ 问答频道 /正文

问题1:我尝试使用pygame group类模块最多发射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

下面:我把“子弹”设为精灵

^{pr2}$

就在主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>问题: 当我打印的时候(群精灵)'然后按'空格键',我每次按下它就会得到更多的精灵。。。在

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

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

请尝试下面的代码,以可视化我的问题

图片:

Tankturretbullet

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

Tags: rectimageselfeventgroupchangepygamepoint
1条回答
网友
1楼 · 发布于 2024-04-26 04:26:29

你只创建了一个bullet实例-你需要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():

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


while loop中,您创建了新的项目符号bullet = Bullets()(变量名bullet不重要,id(bullet)不重要)并尝试删除它(remove(bullet)),但此实例不在组中,因此它不会删除任何内容。然后添加bullet(add(bullet)),它会添加它,因为这个实例还不在组中。在

相关问题 更多 >