ERROR_ERRORs 2023-01-01 20:57 采纳率: 25%
浏览 164
已结题

python飞机大战: Fatal Python error: (pygame parachute) Segmentation Fault

python飞机大战运行时总会抛出:
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized
在程序运行不到半分钟就报错,而且在此期间每一次游戏运行越来越慢
不知为何,求解

Alien.py
import pygame
import sys
import ship
import settings
from enemy import Enemy
from Bullet import Bullet


class Alien_invasion:
    """游戏主程序"""

    def __init__(self):
        """初始化"""

        pygame.init()

        self.settings = settings.Settings()

        # self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        # self.settings.screen_width = self.screen.get_rect().width
        # self.settings.screen_height = self.screen.get_rect().height
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))

        pygame.display.set_caption('Alien_invasion')

        self.ship = ship.Ship(self)

        self.bullets = pygame.sprite.Group()
        self.enemies = pygame.sprite.Group()

    def run_game(self):
        """运行游戏"""
        while True:
            self._check_event()
            self._update_screen()
            self.ship.update()
            self._update_bullet()
            self._create_enemy()

    def _check_event(self):
        """检索事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self, event):  # 传入参数event供访问event.key 属性
        """响应按键"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        if event.key == pygame.K_q:
            sys.exit()
        if event.key == pygame.K_SPACE:
            self._fire_bullet()

    def _check_keyup_events(self, event):  # 传入参数event供访问event.key 属性
        """响应按键"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def _fire_bullet(self):
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)

    def _update_bullet(self):
        self.bullets.update()
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

    def _create_enemy(self):
        enemy = Enemy(self)
        enemy_width = enemy.rect.width
        availzble_space_x = self.settings.screen_width - (2 * enemy_width)
        number_enemy_x = availzble_space_x // (2 * enemy_width)

        for enemy_number in range(number_enemy_x):
            enemy = Enemy(self)
            enemy.x = enemy_width + 2 * enemy_width * enemy_number
            enemy.rect.x = enemy.x
            self.enemies.add(enemy)

    def _update_screen(self):
        """翻新屏幕"""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullets()
        self.enemies.draw(self.screen)
        pygame.display.flip()


if __name__ == '__main__':
    ai = Alien_invasion()
    ai.run_game()

############ship.py#############
import pygame


class Ship:
    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        self.image = pygame.image.load('images/Ship_right.bmp')  # 加载飞船图片
        self.rect = self.image.get_rect()

        self.rect.midbottom = self.screen_rect.midbottom

        self.settings = ai_game.settings
        self.x = float(self.rect.x)

        # 移动标志
        self.moving_left = False
        self.moving_right = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """根据移动标志移动飞船"""
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed

        self.rect.x = self.x

    def blitme(self):
        """构建飞船图像"""
        self.screen.blit(self.image, self.rect)

###############Bullet.py#################
import pygame
from pygame.sprite import Sprite


class Bullet(Sprite):
    """管理飞船所发射子弹的类"""

    def __init__(self, ai_game):
        """在飞船当前位置传建一个子弹对象"""
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

        # 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
        self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
                                self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop

        # 储存用小数表示的子弹的位置
        self.y = float(self.rect.y)

    def update(self):
        """向上移动子弹"""
        # 更新表示子弹位置的小数值
        self.y -= self.settings.bullet_speed
        # 更新表示子弹的rect位置
        self.rect.y = self.y

    def draw_bullets(self):
        """在屏幕上绘制子弹"""
        pygame.draw.rect(self.screen, self.color, self.rect)

##########Enemy.py##########
import pygame
from pygame.sprite import Sprite


class Enemy(Sprite):
    def __init__(self, game):
        super().__init__()

        self.screen = game.screen
        self.screen_rect = game.screen.get_rect()

        self.image = pygame.image.load('images/enemy_ship.bmp')
        self.rect = self.image.get_rect()

        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        self.x = float(self.rect.x)


###########settings.py##########
class Settings:
    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)
        self.ship_speed = 3.3

        self.bullet_speed = 3.5
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = (60,60,60)
  • 写回答

2条回答 默认 最新

  • Flashang_sg 2023-01-01 23:25
    关注

    這段代碼可以修改為:

        def run_game(self):
            """运行游戏"""
            self._create_enemy()
            while True:
                self._check_event()
                self._update_screen()
                self.ship.update()
                self._update_bullet()
                # self._create_enemy()
    

    self._create_enemy() 一開始呼叫一次就好。
    否則會一直消耗記憶。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月10日
  • 已采纳回答 1月2日
  • 创建了问题 1月1日

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)