不能在游戏中制造我的炮弹

2024-09-30 01:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的游戏中,我可以在屏幕上的任何地方放置大炮。我希望我的大炮能够发射子弹,子弹应该在移动100像素后重置。下面是我的cannon课程,我还是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)

使用cannon类。这是在重画游戏窗口下

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

下面是我在bullet_listcannon_list中添加的内容。x和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)

Tags: inselfforifdeftrackscreenglobal
1条回答
网友
1楼 · 发布于 2024-09-30 01:23:00

让我们放弃一切,从头开始,利用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()

enter image description here

现在,我们想放一些雪碧。让我们创建一个表示大炮的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()

enter code here

现在我们想让大炮发射子弹。为此,我们利用了一些面向对象的特性,比如多态性。子弹和大炮是不同的类型,但它们提供相同的接口: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()

enter image description here

使用一个向量并简单地将每个帧的移动距离相加,就可以很容易地检查每个Bullet是否已经移动了100个像素。如果为true,只需调用kill即可将其删除

相关问题 更多 >

    热门问题