代码之家  ›  专栏  ›  技术社区  ›  Ectrizz

如何根据玩家的旋转旋转子弹

  •  3
  • Ectrizz  · 技术社区  · 7 年前

    我终于想出了如何射击子弹,但现在我想在球员头部旋转的基础上旋转子弹的原点。现在它只能在x线上直接拍摄。暴徒们工作得很好。我只需要加上碰撞和在玩家角度上旋转的子弹。我会自己动手,不过如果有人能提示我,我将不胜感激。我现在主要关注的是根据玩家的角度旋转子弹,并在子弹飞出屏幕时“杀死”它。

    import pygame
    import random
    import math
    GRAD = math.pi / 180
    
    black = (0,0,0)
    
    class Config(object):
        fullscreen = True
        width = 1366
        height = 768
        fps = 60
    
    class Player(pygame.sprite.Sprite): #player class
        maxrotate = 180
        down = (pygame.K_DOWN)
        up = (pygame.K_UP)
    
        def __init__(self, startpos=(102,579), angle=0):
            super().__init__()
            self.pos = list(startpos)
            self.image = pygame.image.load('BigShagHoofdzzz.gif')
            self.orig_image = self.image
            self.rect = self.image.get_rect(center=startpos)
            self.angle = angle
    
        def update(self, seconds):
            pressedkeys = pygame.key.get_pressed()
            if pressedkeys[self.down]:
                self.angle -= 2
                self.rotate_image()
            if pressedkeys[self.up]:
                self.angle += 2
                self.rotate_image()
    
        def rotate_image(self):#rotating player image
            self.image = pygame.transform.rotate(self.orig_image, self.angle)
            self.rect = self.image.get_rect(center=self.rect.center)
    
    class Mob(pygame.sprite.Sprite):#monster sprite
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('Monster1re.png')
            self.rect = self.image.get_rect()
            self.rect.x = 1400
            self.rect.y = random.randrange(500,600)
            self.speedy = random.randrange(-8, -1)
    
        def update(self):
            self.rect.x += self.speedy
            if self.rect.x < -100 :
                self.rect.x = 1400
                self.speedy = random.randrange(-8, -1)
    
    class Bullet(pygame.sprite.Sprite):#bullet sprite needs to rotate according to player's angle. 
        """ This class represents the bullet . """
        def __init__(self):
            # Call the parent class (Sprite) constructor
            super().__init__()
    
            self.image = pygame.image.load('lols.png').convert()
            self.image.set_colorkey(black)
            self.rect = self.image.get_rect()
    
        def update(self):
            """ Move the bullet. """
            self.rect.x += 10
    
    
    
    #end class
    
    player = Player()
    
    mobs = []
    for x in range(0,10):
        mob = Mob()
        mobs.append(mob)
    
    print(mobs)
    
    all_sprites_list = pygame.sprite.Group()
    allgroup = pygame.sprite.LayeredUpdates()
    allgroup.add(player)
    
    for mob in mobs:
        all_sprites_list.add(mob)
    
    
    
    
    
    
    
    
    def main():
        #game 
        pygame.mixer.pre_init(44100, -16, 1, 512)
        pygame.mixer.init()
        pygame.init()
        screen=pygame.display.set_mode((Config.width, Config.height),         
        pygame.FULLSCREEN)
        background = pygame.image.load('BGGameBig.png')
        sound = pygame.mixer.Sound("shoot2.wav")
        bullet_list = pygame.sprite.Group
    
        clock = pygame.time.Clock()
        FPS = Config.fps
    
    
        mainloop = True
        while mainloop:
            millisecond = clock.tick(Config.fps)
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    mainloop = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        mainloop = False
                    if event.key == pygame.K_SPACE: #Bullet schiet knop op space
                        bullet = Bullet()
                        bullet.rect.x = player.rect.x
                        bullet.rect.y = player.rect.y
                        all_sprites_list.add(bullet)
                        bullet_list.add(bullet)
                        sound.play()
                    if event.key == pygame.K_ESCAPE:
                        mailoop = False 
    
    
            pygame.display.set_caption("hi")
            allgroup.update(millisecond)
            all_sprites_list.update()
            screen.blit(background, (0,0))
            allgroup.draw(screen)
            all_sprites_list.draw(screen)
            pygame.display.flip()
    
    
    if __name__ == '__main__':
        main()
        pygame.quit()
    

    所以它需要在球员身上轮换。按向上键或向下键时更新的角度。

    1 回复  |  直到 7 年前
        1
  •  2
  •   skrx    7 年前

    你需要使用三角或向量来计算子弹的速度(我在这里使用三角)。将球员的角度传递给 Bullet 然后使用 math.cos math.sin 以负角度获得子弹的方向,并按所需速度缩放。实际位置必须存储在列表或向量中,因为 pygame.Rect s只能将int作为其坐标。

    class Bullet(pygame.sprite.Sprite): 
        """This class represents the bullet."""
        def __init__(self, pos, angle):
            super().__init__()
            # Rotate the image.
            self.image = pygame.transform.rotate(your_bullet_image, angle)
            self.rect = self.image.get_rect()
            speed = 5  # 5 pixels per frame.
            # Use trigonometry to calculate the velocity.
            self.velocity_x = math.cos(math.radians(-angle)) * speed
            self.velocity_y = math.sin(math.radians(-angle)) * speed
            # Store the actual position in a list or a vector.
            self.pos = list(pos)
    
        def update(self):
            """ Move the bullet. """
            self.pos[0] += self.velocity_x
            self.pos[1] += self.velocity_y
            # Update the position of the rect as well.
            self.rect.center = self.pos
    
    
    # In the event loop.
    if event.key == pygame.K_SPACE:
        # Pass the position and angle of the player.
        bullet = Bullet(player.rect.center, player.angle)
        all_sprites_list.add(bullet)
        bullet_list.add(bullet)