-2

我无法让我的玩家朝玩家正在看的方向射击

class Player(pygame.sprite.Sprite):
    def __init__(self, screen, (posx, posy), shell):
        'Called the Sprite class constructor'
        pygame.sprite.Sprite.__init__(self)

        'Save a reference to the screen'
        self.screen = screen
        self.shell = shell
        'Create the image for the sprite'
        self.image = pygame.image.load("player0.png")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        self.charge = 50
        'Position the sprite'
        self.x = posx
        self.y = posy
        self.rect.center = (self.x, self.y)
        self.velocity = Vectors.Vector2D(2, 2)

        self.imgDown = []
        self.imgRight = []
        self.imgLeft = []
        self.imgUp = []

        self.frame = 0
        self.delay = 3
        self.pause = self.delay
        self.speed = 5
def update(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_a]:
            self.rect.centerx -= self.velocity.mulscaler(self.speed).x
            self.loadPicsLeft()
            self.calcDelay()
            self.image = self.imgLeft[self.frame]

        if keys[pygame.K_d]:
            self.rect.centerx += self.velocity.mulscaler(self.speed).x
            self.loadPicsRight()
            self.calcDelay()
            self.image = self.imgRight[self.frame]


        if keys[pygame.K_w]:
            self.rect.centery -= self.velocity.mulscaler(self.speed).y
            self.loadPicsUp()
            self.calcDelay()
            self.image = self.imgUp[self.frame]

        if keys[pygame.K_s]:
            self.rect.centery += self.velocity.mulscaler(self.speed).y
            self.loadPics()
            self.calcDelay()
            self.image = self.imgDown[self.frame]

        if self.velocity.x > self.speed:
            self.velocity.x = self.speed
        if self.velocity.x < -self.speed:
            self.velocity.x = -self.speed

        if self.velocity.y > self.speed:
            self.velocity.y = self.speed
        if self.velocity.y < -self.speed:
            self.velocity.y = -self.speed

        if keys[pygame.K_SPACE]:
                self.shell.x = self.rect.centerx
                self.shell.y = self.rect.centery
                self.shell.speed = self.charge

    def calcDelay(self):
        self.pause -= 1
        if self.pause <= 0:
            self.pause = self.delay

            self.frame += 1
            if self.frame > 2:
                self.frame = 0

    def loadPics(self):
        for i in range (7):
            imgName = "player%d.png"% i
            tmpImg = pygame.image.load(imgName)
            tmpImg.convert()
            tranColor = tmpImg.get_at((0,0))
            tmpImg.set_colorkey(tranColor)
            self.imgDown.append(tmpImg)

    def loadPicsRight(self):
        for i in range (7):
            imgName = "playerRight%d.png"% i
            tmpImg = pygame.image.load(imgName)
            tmpImg.convert()
            tranColor = tmpImg.get_at((0,0))
            tmpImg.set_colorkey(tranColor)
            self.imgRight.append(tmpImg)

    def loadPicsLeft(self):
        for i in range (7):
            imgName = "playerLeft%d.png"% i
            tmpImg = pygame.image.load(imgName)
            tmpImg.convert()
            tranColor = tmpImg.get_at((0,0))
            tmpImg.set_colorkey(tranColor)
            self.imgLeft.append(tmpImg)

    def loadPicsUp(self):
        for i in range (7):
            imgName = "playerUp%d.png"% i
            tmpImg = pygame.image.load(imgName)
            tmpImg.convert()
            tranColor = tmpImg.get_at((0,0))
            tmpImg.set_colorkey(tranColor)
            self.imgUp.append(tmpImg)

class Shell(pygame.sprite.Sprite):
    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = pygame.Surface((50, 50))
        self.image.fill((0xff, 0xff, 0xff))
        self.image.set_colorkey((0xff, 0xff, 0xff))
        pygame.draw.circle(self.image, (0, 0, 0), (25, 25), 25)
        self.image = pygame.transform.scale(self.image, (10, 10))
        self.rect = self.image.get_rect()
        self.rect.center = (-100, -100)

        self.speed = 0
        self.dir =0
        self.reset()

    def update(self):
        self.checkBounds()
        self.rect.center = (self.x, self.y)

    def reset(self):
        """ move off stage and stop"""
        self.x = -100
        self.y = -100
        self.speed = 0

这是我的玩家和我的 shell 类,目前我的所有 shell 都出现在我的玩家的位置,我正在寻找的是我的玩家在他向右看时向右侧发射子弹,向左看时向左射击等等。谁能给我任何关于我可以做些什么来实现这一目标的提示。我现在才开始在大学编码,所以我有点挣扎

4

1 回答 1

1

1 - 存储玩家的方向

2 - 使壳的速度像这样:

self.speedx = speed_length * cos(player.angle)
self.speedy = speed_length * sin(player.angle)

笔记:

Speed_length 是壳在一帧中行进的总欧几里得距离。

角度必须以弧度为单位,或者如果您这样做,您可以使用度数:

self.speedx = speed_length * cos(radians(player.angle))
self.speedy = speed_length * sin(radians(player.angle))
于 2013-04-01T17:32:10.520 回答