rectsの保存中に問題が発生すると、ゲームがクラッシュします

ディエゴフラジ

オブジェクト(通常は図面のように小さい)から位置と四角形を保存し、それらを画面に生成(ブリット)するこのコードがあります。オブジェクトを入れすぎたり大きすぎたりすると、rectが衝突してゲームがクラッシュするのではないかと思いますが、オブジェクトが少ない場合でも、このような事態が発生してクラッシュすることがあります。

どうすればこの問題を解決できますか?ゲームをクラッシュさせるほど近くないことを確認するためにif文を追加することを推測しています。画像の四角形を保存する場所は、self.game_imagesのforiにあります

class GameScene(Scene):
    def __init__(self, game, images, main_image, next_scene):
        super().__init__(next_scene)
        
        self.game = game
        self.main_image = main_image
        self.game_images = images

        # Fade effect set-up
        self.fade = False
        self.fade_time = 0
        self.current_alpha = 255
        self.part = 1

        self.record_text = font.render('Atiende',True, PURPLE)
        self.correct_image_rect = None

        # Trying to use colliderect so it doesnt overlap
        # this is the same code as before but adapted to use the gameimage class and the rects stored there
        self.rects = []
        for i in self.game_images:
            position_set = False 
            while not position_set:
                x = random.randint(100,950)
                y = random.randint(100,600) 

                i.rect.x = x
                i.rect.y = y

                margin = 5
                rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
                if len(self.rects) == 0 or i.rect.collidelist(rl) < 0:
                    self.rects.append(i.rect)
                    position_set = True

        # this makes a number and object pair, and allows us to set the correct rects for the correct gameimage classes
        for i, rect in enumerate(self.rects):
            self.game_images[i].rect = rect

    # this is the fade stuff from before that was in draw. It really belongs here tbh
    def update(self, dt):
        if self.part == 1 and self.fade:
            self.fade_time += dt
            if self.fade_time > fade_timer:
                self.fade_time = 0
                self.main_image.set_alpha(self.current_alpha)
                self.record_text.set_alpha(self.current_alpha)
                # Speed whichin the image dissapears
                self.current_alpha -= 5
                if self.current_alpha <= 0:
                    self.fade = False
                    self.part = 2

        else:
            # we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
            self.main_image.set_alpha(255)

    # draw is similar to before, but a bit more streamlined as the fade stuff is not in update
    def draw(self, screen):
        super().draw(screen)

        if self.part == 1:
            screen.blit(self.record_text, (550, 20))
            screen.blit(self.main_image.image, (580, 280)) 
        else:
            # Second half 
            text2 = font.render('¿Qué has visto?',True, PURPLE)
            screen.blit(text2, (400,5))

            # Show all similar images      
            for game_image in self.game_images:
                game_image.draw(screen)

            # We associate the correct rect to the correct image, to pass it later to the CORRECT Screen
            self.correct_image_rect = self.game_images[self.game_images.index(self.main_image)].rect

    # again we pass the event to the game object the same as with the other classes
    def get_event(self, event):
        if self.part == 2:
            if self.game.level == 13:
                self.game.game_over = True
            if self.correct_image_rect.collidepoint(event.pos):
                return 'CORRECT'
            for rect in self.rects:
                if not self.correct_image_rect.collidepoint(event.pos) and rect.collidepoint(event.pos):
                    return 'INCORRECT'    

Rabbid76

すべてのオブジェクトを一度に追加しようとするため、無限ループに陥ります。アルゴリズムが別のオブジェクトと衝突しないオブジェクトのランダムな位置を見つけることができない場合、ループは終了しません。

updateメソッド内でオブジェクトを1つずつ作成しますこのupdateメソッドは、アプリケーションループで継続的に呼び出されます。フレームごとにオブジェクト上に作成します。すべてのオブジェクトを生成できるとは限らない場合もありますが、無限ループを回避することはできます。

class GameScene(Scene):
    def __init__(self, game, images, main_image, next_scene):
        super().__init__(next_scene)
        
        self.game = game
        self.main_image = main_image
        self.game_images = images

        # Fade effect set-up
        self.fade = False
        self.fade_time = 0
        self.current_alpha = 255
        self.part = 1

        self.record_text = font.render('Atiende',True, PURPLE)
        self.correct_image_rect = None

        # Trying to use colliderect so it doesnt overlap
        # this is the same code as before but adapted to use the gameimage class and the rects stored there
        self.rects = []
        

    # this is the fade stuff from before that was in draw. It really belongs here tbh
    def update(self, dt):

        if len(self.rects) < len(self.game_images):
            i = len(self.rects)

            x = random.randint(100,950)
            y = random.randint(100,600) 
            
            self.game_images[i].rect.x = x
            self.game_images[i].rect.y = y

            margin = 5
            rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
            if len(self.rects) == 0 or self.game_images[i].rect.collidelist(rl) < 0:
                self.rects.append(self.game_images[i].rect)
                
        if self.part == 1 and self.fade:
            self.fade_time += dt
            if self.fade_time > fade_timer:
                self.fade_time = 0
                self.main_image.set_alpha(self.current_alpha)
                self.record_text.set_alpha(self.current_alpha)
                # Speed whichin the image dissapears
                self.current_alpha -= 5
                if self.current_alpha <= 0:
                    self.fade = False
                    self.part = 2

        else:
            # we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
            self.main_image.set_alpha(255)

    # [...]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

クラウドフォーメーションスタックの検証の問題を更新するときに問題が発生します

Swift:ハッシュ可能なプロトコルにクラスのObjectIDを使用すると、set.containsメソッドでランダムな動作が発生します。コードの何が問題になっていますか?

HTMLCollectionのループ中に要素をラップすると、問題が発生します

Spritekitゲームがクラッシュします(SKLightNodeスレッドの問題が原因ですか?)

Android:スペイン語:float値の解析中に問題が発生:アプリがクラッシュする

パッケージのターゲットフレームワークがアプリケーションのターゲットフレームワークと異なる場合、問題が発生しますか?

UISegmentControlをサブクラス化するときに `items`とターゲットアクションの設定中にエラーが発生しました

Herokuアプリがステータス503をクラッシュしました。どの問題が発生する可能性がありますか?

SpringIOプラットフォームの使用中に問題が発生する

古いPS2キーボードに水をこぼすと、システムクラッシュ、誤動作、BIOSの問題が発生する可能性がありますか?

より多くのdllをコードにロードするとクラッシュが発生します

キャンディークラッシュスタイルのJavaScriptゲームで問題が発生しました

OpenGL / SDLの問題により、起動時にバルブゲームがクラッシュする

xcode 8にアップグレードすると、UINavigationControllerプッシュアニメーションの問題が発生します

スプラッシュ画面の後にエラーが発生するネイティブナビゲーションの問題に対応

StringListをオブジェクトで解放しようとすると複雑な問題が発生し、アプリがクラッシュします

Perlの配列のハッシュを比較中に問題が発生する

トランザクションテーブルに値を入力しようとすると、このコードに問題が発生します

配列をobservableArrayにプッシュするときに問題が発生します

Symfony 5.1.3キャッシュのクリア中に発生する問題:名前空間からクラス「MappingDriverChain」を読み込もうとしました

イベントへのデータ保存私はプッシュキーを取得します。ただし、Event_keyがEventListを割り当てているときに問題が発生します{ref:Event_key} Firebase Real Time Database

複数のスレッドでViewContextにアクセスすると、クラッシュが発生します

Launch4jを使用してスプラッシュスクリーンを追加すると、起動エラー(「アプリケーションの起動中にエラーが発生しました」)が発生します

マスクを使用してsvgをアニメーション化すると、svgの外側でクリッピングに問題が発生します

SwiftUI:environmentObjectをシートに渡すと、更新の問題が発生します

ラップトップにアイスパックを適用すると問題が発生しますか?

sqliteデータベースから検索してEditTextにレコードを表示すると、クラッシュ/再起動が発生しますが、何が問題でしたか?

Xamarinフォーム:XamForms.Controls.Calendarで同じ日に複数のイベントをクリックすると問題が発生します

GitHubへの初期コミットにプッシュするときにエラーが発生しました

TOP 一覧

  1. 1

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  2. 2

    ソートされた検索、ターゲット値未満の数をカウント

  3. 3

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  4. 4

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  5. 5

    Ansibleで複数行のシェルスクリプトを実行する方法

  6. 6

    Reactでclsxを使用する方法

  7. 7

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  8. 8

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  9. 9

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  10. 10

    PowerShellの分割ファイルへのヘッダーの追加

  11. 11

    Chromeウェブアプリのウェブビューの高さの問題

  12. 12

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  13. 13

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  14. 14

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  15. 15

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  16. 16

    MLでのデータ前処理の背後にある直感

  17. 17

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  18. 18

    reCAPTCHA-エラーコード:ユーザーの応答を検証するときの「missing-input-response」、「missing-input-secret」(POSTの詳細がない)

  19. 19

    STSでループプロセス「クラスパス通知の送信」のループを停止する方法

  20. 20

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  21. 21

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

ホットタグ

アーカイブ