Python 在pygame中是否有重复?

Python 在pygame中是否有重复?,python,pygame,Python,Pygame,我试图让我的角色在pygame中拍摄。我不能让子弹离开他的位置,如果它击中屏幕边缘,我就不能停下来。我希望它继续移动,直到它与屏幕边缘碰撞。每当我运行以下代码时,它都会崩溃: class BULLET(pygame.sprite.Sprite): def __init__(self, speed, location): self.pos = [0,0] pygame.sprite.Sprite.__init__(self) self.image = SHOWBULLET

我试图让我的角色在pygame中拍摄。我不能让子弹离开他的位置,如果它击中屏幕边缘,我就不能停下来。我希望它继续移动,直到它与屏幕边缘碰撞。每当我运行以下代码时,它都会崩溃:

class BULLET(pygame.sprite.Sprite):
def __init__(self, speed, location):
    self.pos = [0,0]
    pygame.sprite.Sprite.__init__(self)
    self.image = SHOWBULLET
    self.rect = self.image.get_rect()
    self.rect.left, self.rect.top = location
    self.rect.center = location
    self.speed = speed
def move(self):
    if AMMO > 0:
        if event.type == pygame.MOUSEBUTTONDOWN and Gun.image == NOGUN:
            held_down = True
            if held_down == True:
                self.rect.centerx = Player.rect.centerx
                self.rect.centery = Player.rect.centery
                if Player.direction == 0:
                    self.direction = 0
                    while self.rect.top >= screen.get_rect().top:
                        self.rect.top = self.rect.top + 20
                        if self.rect.top <=   screen.get_rect().top:
                            break
类子弹(pygame.sprite.sprite):
定义初始(自身、速度、位置):
self.pos=[0,0]
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=SHOWBULLET
self.rect=self.image.get_rect()
self.rect.left,self.rect.top=位置
self.rect.center=位置
自身速度=速度
def移动(自我):
如果弹药>0:
如果event.type==pygame.MOUSEBUTTONDOWN和Gun.image==NOGUN:
抑制=正确
如果按住=True:
self.rect.centerx=Player.rect.centerx
self.rect.centery=Player.rect.centery
如果Player.direction==0:
自我定向=0
而self.rect.top>=screen.get_rect()。top:
self.rect.top=self.rect.top+20

如果self.rect.top你的移动功能应该忽略与玩家有关的一切。从零开始。它应该在每一个刻度上更新它的位置(可能通过调用
move()
every tick),并且它应该沿着自己的方向移动,并检查自己与墙/边的碰撞

修复了init方法

def __init__(self, speed, location, direction):
    self.pos = location
    pygame.sprite.Sprite.__init__(self)
    self.image = SHOWBULLET
    self.rect = self.image.get_rect()
    self.rect.left, self.rect.top = location
    self.rect.center = location
    self.speed = speed
    self.direction = direction
但是,您的移动方法可能需要从头开始重做

移动方法应包含两个部分:

1) 将项目符号沿所需方向移动一个增量

if (direction == 0) {
    self.pos = newPosition; // replace new position with a calculation for up
} else if (direction == 1) {
    self.pos = newPosition; // this new position should be one increment right
} etc...
2) 检查子弹是否离开屏幕

if (self.pos[0] > width || self.pos[0] < 0 || self.pos[1] > height || self.pos[1] < 0) {
    //return false;
}

return true;
if(self.pos[0]>宽度| self.pos[0]<0 | self.pos[1]>高度| self.pos[1]<0){
//返回false;
}
返回true;

我想你会希望以这种方式构建游戏。这是一个简略的版本,包含了我认为相关的内容,其中存在一些问题,但本质上是这样的

  • 你的子弹发射后根本不应该关心玩家
  • 子弹应该从HoldMouseButton事件发生时玩家站立的位置开始。在那之后,它应该在它的路径上不间断地继续
  • 我想如果鼠标被按住很长一段时间,你会希望玩家射出很多子弹。我不认为你现在的代码能做到这一点。实际上,子弹应该关心任何事件,除了帮助它在屏幕上移动的ticketvents。另一方面,玩家应该关心鼠标按钮是否被按下,只要鼠标按钮被按下,就应该发射子弹
  • 此外,了解一点MVC架构可能会有所帮助。看看这个
希望这有帮助

class Bullet:
    def __init__(self, velocity, start_position):
        self.pos = start_position
        self.velocity = velocity
        self.visible = True

    def notify(self, event):
        if isinstance(event, TickEvent):
            self.move(event.time_passed)

    def move(self, time):
        self.pos += velocity * time

class Charactor:
    def __init__(self, position, event_manager, sprite_manager, etc.):
        self.state = 'MOVING'
        self.pos = position
        self.direction = direction

    def notify(self, event):
        if isinstance(event, HoldMouseButtonEvent()):
            self.state = 'SHOOTING'
        elif isinstance(event, ReleaseMouseButtonEvent()):
            self.state = 'MOVING'
        elif isinstance(event, TickEvent()):
            if self.state == 'SHOOTING':
                self.shoot()
            elif self.state == 'MOVING':
                self.move()

    def shoot(self):
        sprite_manager.add(Bullet(BULLET_VELOCITY * self.direction, self.pos))

看,子弹的来源是玩家所在的位置,因此,开始从玩家的位置移动,就像一把真正的枪。你应该在
\uuuuu init\uuuuu()
中完成所有这一切,因为子弹就是从这里开始的@user2493744@user2493744-你试过了吗?它应该会起作用。子弹被射中后不应该再关心球员的位置了。虽然这会让一些很酷的射击技巧成为可能。子弹飞过时移到一边,你可以在拐角处射击…@user2493744当你制造子弹时,你应该通过玩家的位置,就像这样
bullet=bullet(10,player.pos)
我需要它移动到玩家的位置,当它第一次射击时,然后离开玩家,因此,模拟它离开枪管,并朝着球员所面对的方向移动直到片刻(不满足条件)才会重复:?