鱼C论坛

 找回密码
 立即注册
查看: 2183|回复: 7

[已解决]小甲鱼飞机大战

[复制链接]
发表于 2021-3-19 12:09:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
不知道怎么回事飞机的子弹射出怎么这么短
求指点


                               
登录/注册后可看大图

最佳答案
2021-3-20 13:07:47
本帖最后由 小伤口 于 2021-3-20 13:09 编辑

我知道你的问题了
  1. # 检测子弹是否击中敌机
  2.                 for b in bullets:
  3.                     if b.active:
  4.                         b.move()
  5.                         screen.blit(b.image, b.rect)
  6.                         enemy_hit = pygame.sprite.spritecollide(
  7.                             b, enemies, False, pygame.sprite.collide_mask)
  8.                         if enemy_hit:
  9.                             b.active = False
  10.                             for e in enemy_hit:
  11.                                 if e in mid_enemies or e in big_enemies:
  12.                                     e.hit = True
  13.                                     e.energy -= 1
  14.                                     if e.energy == 0:
  15.                                         e.active = False
  16.                                 else:
  17.                                     e.active = False
复制代码


这段代码再缩进一次
for循环如果进入if里面
就会导致子弹还没运行完就被刷新
也就是会造成所谓的子弹距离变短
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-19 14:48:46 | 显示全部楼层
怎么说呢,你得把你自己写的代码发出来才行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-19 16:20:01 | 显示全部楼层
子弹模块里的

class Bullet1(pygame.sprite.Sprite):
    def __init__(self, position):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('飞机大战素材/images/bullet1.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.left, self.top = position
        self.active = True
        self.speed = 12
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        self.rect.bottom -= self.speed
        if self.rect.bottom < 0:
            self.active = False

    def reset(self, position):
        self.rect.left, self.rect.top = position
        self.active = True


main函数里的

# 发射子弹,每10帧反射一次
            if not (delay % 10):
                bullet_sound.play()
                if is_double_bullet:
                    bullets = bullet2
                    bullets[bullet2_index].reset((me.rect.centerx - 33, me.rect.centery))
                    bullets[bullet2_index + 1].reset((me.rect.centerx + 30, me.rect.centery))
                    bullet2_index = (bullet2_index + 2) % BULLET2_NUM
                else:
                    bullets = bullet1
                    bullets[bullet1_index].reset(me.rect.midtop)
                    bullet1_index = (bullet1_index + 1) % BULLET1_NUM
                # 检测子弹是否击中敌机
                for b in bullets:
                    if b.active:
                        b.move()
                        screen.blit(b.image, b.rect)
                        enemy_hit = pygame.sprite.spritecollide(
                            b, enemies, False, pygame.sprite.collide_mask)
                        if enemy_hit:
                            b.active = False
                            for e in enemy_hit:
                                if e in mid_enemies or e in big_enemies:
                                    e.hit = True
                                    e.energy -= 1
                                    if e.energy == 0:
                                        e.active = False
                                else:
                                    e.active = False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-19 22:55:06 | 显示全部楼层
nbddyf 发表于 2021-3-19 16:20
子弹模块里的

class Bullet1(pygame.sprite.Sprite):

子弹模块里的关于子弹移动move()函数
也就是这个
  1. self.rect.bottom -= self.speed
  2.         if self.rect.bottom < 0:
  3.             self.active = False
复制代码

这段函数大体意思就是当子弹到达背景顶部时子弹消失
但是你的move函数是用bottom来判断,bottom是底部的意思
所以子弹当然短了
改成这样试试
  1. #子弹移动
  2.     def move(self):
  3.         self.rect.top-=self.speed
  4.         if self.rect.top<0:
  5.             self.active=False
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-20 10:00:47 | 显示全部楼层
我改过用 top的 还是没一样的 不知道怎么回事
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 10:13:20 From FishC Mobile | 显示全部楼层
本帖最后由 小伤口 于 2021-3-20 10:15 编辑

嗯,那你试试把最开始定义的self.active = True,把这个改为False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 13:07:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 小伤口 于 2021-3-20 13:09 编辑

我知道你的问题了
  1. # 检测子弹是否击中敌机
  2.                 for b in bullets:
  3.                     if b.active:
  4.                         b.move()
  5.                         screen.blit(b.image, b.rect)
  6.                         enemy_hit = pygame.sprite.spritecollide(
  7.                             b, enemies, False, pygame.sprite.collide_mask)
  8.                         if enemy_hit:
  9.                             b.active = False
  10.                             for e in enemy_hit:
  11.                                 if e in mid_enemies or e in big_enemies:
  12.                                     e.hit = True
  13.                                     e.energy -= 1
  14.                                     if e.energy == 0:
  15.                                         e.active = False
  16.                                 else:
  17.                                     e.active = False
复制代码


这段代码再缩进一次
for循环如果进入if里面
就会导致子弹还没运行完就被刷新
也就是会造成所谓的子弹距离变短
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-20 14:26:14 | 显示全部楼层
对  找到啦 哈哈 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 19:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表