无法在pygame中使我的大炮射击子弹

rishi:

在我的游戏中,我可以将大炮放置在屏幕上的任何位置。我希望我的大炮能够发射子弹,并且子弹在移动100像素后应将其重置。下面给出的是我的大炮类,我还是OOP的新手,因此我需要一些帮助,但是我也无法使用列表来完成此任务。感谢您的帮助。

class Cannon():
    global cannon_list
    global bullet_list
    def __init__(self, x, y, track, old_x):
        self.x = x
        self.y = y
        self.track = track
        self.old_x = old_x

    def spawnBullet(self):
        for j in bullet_list:
            self.old_x = self.x
            self.track = j[2]
            screen.blit(bullet, (j[0], j[1]))

    def moveBullet(self):
        if self.x <= self.track:
            self.x += 3

    def resetBullet(self):
        if self.x >= self.track:
            self.x = self.old_x

    def spawnCannon(self):
        for i in cannon_list:
            screen.blit(cannon, i)

使用大炮类。这是在redrawGamewindow下。

for j in bullet_list:
    cannonsAndBullets = Cannon(j[0], j[1], j[2], j[0])
    cannonsAndBullets.spawnCannon()
    cannonsAndBullets.spawnBullet()
    cannonsAndBullets.moveBullet()
    cannonsAndBullets.resetBullet()

以下是我附加到bullet_list和中的内容cannon_listx和y是我的玩家的位置

            cannon_list.append([x,y])
            bullet_list.append([x,(y+25),100, x])

我班上的编辑

class Cannon():
    global cannon_list
    global bullet_list
    def __init__(self, x, y, track, old_x):
        self.x = x
        self.y = y
        self.track = track
        self.old_x = old_x

    def spawnBullet(self):
        # for j in bullet_list:
        # self.x, self.y, self.track, self.old_x = j
        screen.blit(bullet, (self.x, self.y))

    def moveBullet(self):
        # for j in bullet_list:
        # self.x, self.y, self.track, self.old_x = j
        if self.track <= 100:
            print(self.track)
            self.track += 3
            self.x += 3

    def resetBullet(self):
        # for j in bullet_list:
        # self.x, self.y, self.track, self.old_x = j
        if self.track >= 100:
            print(self.track)
            self.x = self.old_x

    def spawnCannon(self):
        for i in cannon_list:
            screen.blit(cannon, i)
懒惰

让我们抛弃一切,从头开始,利用pygame功能(如精灵和矢量数学)。

我们从一个pygame游戏的基本框架开始,一个简单的窗口:

import pygame

def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
        screen.fill(pygame.Color('grey'))
        pygame.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    main()

在此处输入图片说明

现在,我们要放置一些精灵。让我们创建一个Sprite代表大炮类,然后用鼠标放置它们:

import pygame

class Cannon(pygame.sprite.Sprite):
    def __init__(self, pos, *grps):
        super().__init__(*grps)
        self.image = pygame.Surface((32, 32))
        self.image.fill(pygame.Color('darkred'))
        self.rect = self.image.get_rect(center=pos)

def main():

    all_sprites = pygame.sprite.Group()

    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.MOUSEBUTTONDOWN:
                Cannon(e.pos, all_sprites)


        all_sprites.update()

        screen.fill(pygame.Color('grey'))
        all_sprites.draw(screen)
        pygame.display.flip()

        clock.tick(60)

if __name__ == '__main__':
    main()

在这里输入代码

现在,我们希望大炮发射子弹。为此,我们利用了一些OOP功能,例如多态性。子弹和大炮是不同的类型,但是它们提供相同的界面:updatedraw就是这样。注意我们的主循环如何不需要知道我们的精灵到底是什么类型。

我们还确保子弹的所有逻辑都在Bullet该类中,而大炮的所有逻辑都在Cannon该类中:

import pygame

class Bullet(pygame.sprite.Sprite):
    def __init__(self, pos, *grps):
        super().__init__(*grps)
        self.image = pygame.Surface((4, 4))
        self.image.fill(pygame.Color('black'))
        self.rect = self.image.get_rect(center=pos)
        self.pos = pygame.Vector2(pos)
        self.travelled = pygame.Vector2(0, 0)
        direction = pygame.Vector2(pygame.mouse.get_pos()) - self.pos
        if direction.length() > 0:
            self.direction = direction.normalize()
        else:
            self.kill()

    def update(self, dt):
        v = self.direction * dt / 5
        self.pos += v
        self.travelled += v
        self.rect.center = self.pos
        if self.travelled.length() > 100:
            self.kill()

class Cannon(pygame.sprite.Sprite):
    def __init__(self, pos, *grps):
        super().__init__(*grps)
        self.image = pygame.Surface((32, 32))
        self.image.fill(pygame.Color('darkred'))
        self.rect = self.image.get_rect(center=pos)
        self.timer = 200

    def update(self, dt):
        self.timer = max(self.timer - dt, 0)
        if self.timer == 0:
            self.timer = 200
            Bullet(self.rect.center, self.groups()[0])

def main():

    all_sprites = pygame.sprite.Group()

    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    dt = 1
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.MOUSEBUTTONDOWN:
                Cannon(e.pos, all_sprites)


        all_sprites.update(dt)

        screen.fill(pygame.Color('grey'))
        all_sprites.draw(screen)
        pygame.display.flip()

        dt = clock.tick(60)

if __name__ == '__main__':
    main()

在此处输入图片说明

使用向量并简单地将每帧的行进距离相加即可轻松检查每帧Bullet是否已行进100像素。如果为true,则只需调用即可kill将其删除。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章