首页 > 解决方案 > 如何在任何位置用鼠标射击弹丸?

问题描述

我用按钮左右射击了我的弹丸,但是点击很多按钮会很无聊如何用我的鼠标射击?在任意位置 x,y

 # projectile class
    if keys[pygame.K_f]:     
        for bullet in bullets:
            if bullet.x < 500 and bullet.x > 0:
                bullet.x += bullet.speed 
            else:
                bullets.pop(bullets.index(bullet))
        if len(bullets) < 2:  
                bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
    if keys[pygame.K_g]:     
        for bullet in bullets:
            if bullet.x < 500 and bullet.x > 0:
                bullet.x -= bullet.speed 
            else:
                bullets.pop(bullets.index(bullet))
        if len(bullets) < 2:  
            bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
    # Jump and Collisions

这是我的弹丸课

class projectile(object):
   def __init__(self, x, y,color):
       self.x = x
       self.y = y
       self.slash = pygame.image.load("heart.png")
       self.rect  = self.slash.get_rect()
       self.rect.topleft = ( self.x, self.y )
       self.speed = 10
       self.color = color

   def draw(self, window):
       self.rect.topleft = ( self.x,self.y )

       window.blit(slash, self.rect)

标签: pythonhtmlpygame

解决方案


您必须将移动方向 ( dirx, diry) 添加到类射弹中。进一步添加一个移动子弹的方法:

class projectile(object):
   def __init__(self, x, y, dirx, diry, color):
       self.x = x
       self.y = y
       self.dirx = dirx
       self.diry = diry
       self.slash = pygame.image.load("heart.png")
       self.rect  = self.slash.get_rect()
       self.rect.topleft = ( self.x, self.y )
       self.speed = 10
       self.color = color

   def move(self):
       self.x += self.dirx * self.speed
       self.y += self.diry * self.speed

   def draw(self, window):
       self.rect.topleft = (round(self.x), round(self.y))

       window.blit(slash, self.rect)

计算按下鼠标按钮时从玩家到鼠标的方向并生成一个新子弹。方向由玩家到缪斯位置 ( mouse_x - start_x, mouse_y - start_y) 的向量给出。必须通过将向量分量除以欧几里得距离来对向量进行归一化(单位向量) :

for event in pygame.event.get():
    # [...]

    if event.type == pygame.MOUSEBUTTONDOWN:

        if len(bullets) < 2:  

            start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
            mouse_x, mouse_y = event.pos

            dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
            distance = math.sqrt(dir_x**2 + dir_y**2)
            if distance > 0:
                new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
                bullets.append(new_bullet)

在主应用程序循环中循环移动项目符号,如果项目符号超出窗口,则将其移除

run = True
while run:

    # [...]

    for event in pygame.event.get():
        # [...]

    for bullet in bullets[:]:
        bullet.move()

        if bullet.x < 0 or bullet.x > 500 or bullet.y < 0 or bullet.y > 500:
            bullets.pop(bullets.index(bullet))

    # [...]

示例代码

runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False


        if event.type == pygame.MOUSEBUTTONDOWN:

            if len(bullets) < 2:  

                start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
                mouse_x, mouse_y = event.pos

                dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
                distance = math.sqrt(dir_x**2 + dir_y**2)
                if distance > 0:
                    new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
                    bullets.append(new_bullet)

    for bullet in bullets[:]:
        bullet.move()
        if bullet.x < 0 or bullet.x > 800 or bullet.y < 0 or bullet.y > 800:
            bullets.pop(bullets.index(bullet))

    # [...]

推荐阅读