Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 从旋转枪中发射子弹_Python 2.7_Pygame - Fatal编程技术网

Python 2.7 从旋转枪中发射子弹

Python 2.7 从旋转枪中发射子弹,python-2.7,pygame,Python 2.7,Pygame,我的问题与这个问题有关: 你需要做什么计算才能让子弹从枪口射出 我的代码如下 def gun_radar(self): for p in self.gameobjects: if "R" in p.name or "L" in p.name: if abs(p.rect.centerx - self.hero.rect.centerx) < p.radar and abs(p.rect.centery - self.hero.rect

我的问题与这个问题有关:

你需要做什么计算才能让子弹从枪口射出

我的代码如下

def gun_radar(self):

    for p in self.gameobjects:

        if "R" in p.name or "L" in p.name:

            if abs(p.rect.centerx - self.hero.rect.centerx) < p.radar and abs(p.rect.centery - self.hero.rect.centery) < p.radar:  # if hero within radar

                p.vec_to_target = pygame.math.Vector2(self.hero.rect.center) - p.rect.center
                p.direction = p.orig_direction.rotate(p.current_angle)
                p.orientation = p.vec_to_target.dot(p.direction)

                if p.orientation > 2:
                    p.current_angle += 1
                elif p.orientation < -2:
                    p.current_angle -= 1

                p.current_angle = p.clamp(p.current_angle, p.clamp_min, p.clamp_max)

                p.gun_rotate(-p.current_angle)

                self.blt_timer -= 1  #count down the timer. when zero calculate vector and add bullet to fired_blts

                if self.blt_timer<= 0:
                    w, h = p.rect.center
                    angle_in_rad = p.current_angle * (math.pi) / 180
                    w = w + math.cos(angle_in_rad)
                    h = h + math.sin(-angle_in_rad)
                    bullet = Bombullet(bulletimage, w, h)


                    bullet.xvel = bullet.speed * math.cos(angle_in_rad)
                    bullet.yvel = bullet.speed * math.sin(angle_in_rad)

                    bullet.rect.x += bullet.xvel
                    bullet.rect.y += bullet.yvel

                    self.fired_blts.add(bullet)
                    self.blt_timer = 100

子弹更新并朝正确的方向射击,但从枪的中心射击。如何将射击点移动到枪口?

这是一个射击加农炮的示例。您只需将当前角度和加农炮的直角中心传递给新创建的子弹,然后在
\uuuuu init\uuu
方法中旋转其图像和速度向量

import math
import pygame as pg
from pygame.math import Vector2


pg.init()
screen = pg.display.set_mode((640, 480))
FONT = pg.font.Font(None, 24)
BLACK = pg.Color('black')
BG_COLOR = pg.Color('darkseagreen4')


class Bullet(pg.sprite.Sprite):

    def __init__(self, pos, angle):
        super(Bullet, self).__init__()
        self.image = pg.Surface((20, 11), pg.SRCALPHA)
        pg.draw.rect(self.image, pg.Color('grey11'), [0, 0, 13, 11])
        pg.draw.polygon(
            self.image, pg.Color('grey11'), [(13, 0), (20, 5), (13, 10)])
        self.image = pg.transform.rotate(self.image, -angle)
        self.rect = self.image.get_rect(center=pos)
        # To apply an offset to the start position,
        # create another vector and rotate it as well.
        offset = Vector2(80, 0).rotate(angle)
        # Use the offset to change the starting position.
        self.pos = Vector2(pos) + offset
        self.velocity = Vector2(5, 0)
        self.velocity.rotate_ip(angle)

    def update(self):
        self.pos += self.velocity
        self.rect.center = self.pos


def main():
    clock = pg.time.Clock()
    # The cannon image and rect.
    surf = pg.Surface((40, 22), pg.SRCALPHA)
    surf.fill(pg.Color('grey27'))
    pg.draw.rect(surf, pg.Color('grey11'), [30, 6, 10, 10])
    orig_surf = surf
    rect = surf.get_rect(center=(320, 240))
    angle = 0  # Angle of the cannon.
    # Add bullets to this group.
    bullet_group = pg.sprite.Group()

    playing = True
    while playing:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                playing = False
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:  # Left button fires bullet.
                    # Fire a bullet from cannon center with current angle.
                    bullet_group.add(Bullet(rect.center, angle))

        bullet_group.update()
        # Find angle to target (mouse pos).
        x, y = Vector2(pg.mouse.get_pos()) - rect.center
        angle = math.degrees(math.atan2(y, x))
        # Rotate the cannon image.
        surf = pg.transform.rotate(orig_surf, -angle)
        rect = surf.get_rect(center=rect.center)

        # Draw
        screen.fill(BG_COLOR)
        bullet_group.draw(screen)
        screen.blit(surf, rect)
        txt = FONT.render('angle {:.1f}'.format(angle), True, BLACK)
        screen.blit(txt, (10, 10))
        pg.draw.line(
            screen, pg.Color(150, 60, 20), rect.center, pg.mouse.get_pos(), 2)
        pg.display.update()

        clock.tick(30)

if __name__ == '__main__':
    main()
    pg.quit()
还可以使用math.cos和sin计算偏移量

run = math.cos(math.radians(angle)) * 80
rise = math.sin(math.radians(angle)) * 80
offset = run, rise

我用下面的代码实现了我需要做的事情,与上面的代码相同,只是做了一个非常轻微的修改,代码中对此进行了解释@skrx的回答和Nick A.的评论帮助我实现了这一点

def gun_radar(self):

    for p in self.gameobjects:

        if "R" in p.name or "L" in p.name:

            if abs(p.rect.centerx - self.hero.rect.centerx) < p.radar and abs(p.rect.centery - self.hero.rect.centery) < p.radar:  # if hero within radar

                p.vec_to_target = pygame.math.Vector2(self.hero.rect.center) - p.rect.center
                p.direction = p.orig_direction.rotate(p.current_angle)
                p.orientation = p.vec_to_target.dot(p.direction)

                if p.orientation > 2:
                    p.current_angle += 1
                elif p.orientation < -2:
                    p.current_angle -= 1

                p.current_angle = p.clamp(p.current_angle, p.clamp_min, p.clamp_max)

                p.gun_rotate(-p.current_angle)

                p.timer -= 1  #count down the timer. when zero calculate vector and add bullet to fired_blts

                if p.timer<= 0:

                    w, h = p.rect.center

                    # adjust for the distance fromm the gun center to the gun muzzle

                    w = w + math.cos(math.radians(p.current_angle)) * 28
                    h = h + math.sin(math.radians(p.current_angle)) * 28
                    bullet = Bombullet(bulletimage, w, h)  # create the bullet

                    # calculate the velocity

                    bullet.xvel = bullet.speed * math.cos(math.radians(p.current_angle))
                    bullet.yvel = bullet.speed * math.sin(math.radians(p.current_angle))

                    self.fired_blts.add(bullet)

                    p.timer = 100
def gun_雷达(自):
对于self.gameobjects中的p:
如果p.name中的“R”或p.name中的“L”:
如果abs(p.rect.centerx-self.hero.rect.centerx)2:
p、 电流角+=1
elif p.方向<-2:
p、 电流角-=1
p、 电流夹角=p夹钳(p电流夹角、p夹钳最小值、p夹钳最大值)
p、 枪旋转(-p.当前角度)
p、 计时器-=1#倒计时。当为零时,计算矢量并将项目符号添加到已发射的\u blts

如果你把角度转换成弧度,我把角度转换成弧度,然后子弹朝着正确的方向发射,但还是从枪的同一个地方发射。中右翼。修改后的代码w=w+math.cos(angle\u in_rad)h=h+math.sin(angle\u in_rad)bullet=Bombullet(bulletimage,w,h)bullet.xvel=bullet.speed*math.cos(angle\u in_rad)bullet.yvel=bullet.speed*math.sin(angle\u in_rad)请编辑您的帖子,并向我们展示一个我们可以运行的最小、完整的示例。@emorphos您需要使用矩形中心、枪管长度和角度来计算枪口的位置,然后设置起始位置,因为我编辑了代码以使用枪的中心作为起始点。然后,无论枪朝哪个方向射击,子弹总是从枪管中心射出。我编辑了上面的代码来显示这一点。如何让子弹从枪口末端的枪口点射出?我编辑了答案,以演示如何用另一个向量偏移起始位置。
def gun_radar(self):

    for p in self.gameobjects:

        if "R" in p.name or "L" in p.name:

            if abs(p.rect.centerx - self.hero.rect.centerx) < p.radar and abs(p.rect.centery - self.hero.rect.centery) < p.radar:  # if hero within radar

                p.vec_to_target = pygame.math.Vector2(self.hero.rect.center) - p.rect.center
                p.direction = p.orig_direction.rotate(p.current_angle)
                p.orientation = p.vec_to_target.dot(p.direction)

                if p.orientation > 2:
                    p.current_angle += 1
                elif p.orientation < -2:
                    p.current_angle -= 1

                p.current_angle = p.clamp(p.current_angle, p.clamp_min, p.clamp_max)

                p.gun_rotate(-p.current_angle)

                p.timer -= 1  #count down the timer. when zero calculate vector and add bullet to fired_blts

                if p.timer<= 0:

                    w, h = p.rect.center

                    # adjust for the distance fromm the gun center to the gun muzzle

                    w = w + math.cos(math.radians(p.current_angle)) * 28
                    h = h + math.sin(math.radians(p.current_angle)) * 28
                    bullet = Bombullet(bulletimage, w, h)  # create the bullet

                    # calculate the velocity

                    bullet.xvel = bullet.speed * math.cos(math.radians(p.current_angle))
                    bullet.yvel = bullet.speed * math.sin(math.radians(p.current_angle))

                    self.fired_blts.add(bullet)

                    p.timer = 100