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

时间:2017-12-12 11:18:44

标签: python rotation pygame bullet

我终于想出了如何射击子弹,但现在我想在球员头部旋转时旋转子弹的原点。现在它只是在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()

所以它需要在玩家self.angle上旋转,按下按键或按键时会更新。

1 个答案:

答案 0 :(得分:2)

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

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)