English 中文(简体)
摘录
原标题:draw_text not working in arcade for a "GAME OVER" screen

我正在为我的班子开展各种女运动员游戏,并且主要工作。 然而,当我试图编造一个“GAME”栏目,以在最后生命丧失时创建“GAME”旗帜时,除了在屏幕上制作的文字外,一切都可行。 所有其他工作都是很短的拖延,才能回到比赛主菜园。

import random
import math
import time

import arcade
import os

from typing import cast
import arcade.gui

INITIAL_ASTEROID_COUNT = 3
SCALE = 0.5
BOUNDARY_PADDING = 300
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
SCREEN_TITLE = "Asteroid Smasher"
LEFT_BOUNDARY = -BOUNDARY_PADDING
RIGHT_BOUNDARY = WINDOW_WIDTH + BOUNDARY_PADDING
BOTTOM_BOUNDARY = -BOUNDARY_PADDING
TOP_BOUNDARY = WINDOW_HEIGHT + BOUNDARY_PADDING

MENU = "MENU"
GAME = "GAME"
HOW_TO = "HOW_TO"
GAME_OVER = "GAME_OVER"





class MyGame(arcade.Window):
    """Main application class"""

    def __init__(self):
        """Initialize game window"""
        super().__init__(WINDOW_WIDTH, WINDOW_HEIGHT, SCREEN_TITLE)
        self.scene = MENU
        self.background = arcade.load_texture("Resources/Background/stars.png")


        self.uimanager = arcade.gui.UIManager()
        self.uimanager.enable()

        self.v_box = arcade.gui.UIBoxLayout(space_between=20)

        button_style = {
            "font_name": ("Algerian"),
            "font_size": 25,
            "font_color": (61, 212, 45),
            "bg_color": (0, 0, 0)
        }

        start_button = arcade.gui.UIFlatButton(text="New Game", width=300, style=button_style)

        start_button.on_click = self.on_click_start

        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x= center_x ,
                anchor_y= center_y ,
                child=start_button
            )
        )

        how_to_button = arcade.gui.UIFlatButton(text="How To Play", width=300, style=button_style)

        how_to_button.on_click = self.how_to_play

        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x= center_x ,
                anchor_y= center_y ,
                child=how_to_button
            )
        )

        self.v_box.add(start_button)
        self.v_box.add(how_to_button)

        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x= center_x ,
                anchor_y= center_y ,
                child=self.v_box
            )
        )

        self.how_to_manager = arcade.gui.UIManager()
        self.how_to_manager.enable()
        return_button_style = {
            "font_name": ("Algerian"),
            "font_size": 25,
            "font_color": (61, 212, 45),
            "bg_color": (0, 0, 0)
        }
        back_button = arcade.gui.UIFlatButton(text="Back to Menu", width=300, style=return_button_style)

        back_button.on_click = self.return_to_menu

        self.how_to_manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x= left ,
                anchor_y= bottom ,
                child=back_button
            )
        )

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        self.frame_count = 0

        self.game_over = False

        # Sprite lists
        self.player_sprite_list = arcade.SpriteList()
        self.asteroid_list = arcade.SpriteList()
        self.bullet_list = arcade.SpriteList()
        self.ship_life_list = arcade.SpriteList()

        # Set up the player
        self.score = 0
        self.player_sprite = None
        self.lives = 3

        # Sounds
        self.laser_sound = arcade.load_sound(":resources:sounds/hurt5.wav")
        self.hit_sound1 = arcade.load_sound(":resources:sounds/explosion1.wav")
        self.hit_sound2 = arcade.load_sound(":resources:sounds/explosion2.wav")
        self.hit_sound3 = arcade.load_sound(":resources:sounds/hit1.wav")
        self.hit_sound4 = arcade.load_sound(":resources:sounds/hit2.wav")

    
    def on_draw(self):

        """Render screen"""
        self.clear()
        arcade.start_render()

        if self.scene == MENU:
            arcade.draw_lrwh_rectangle_textured(0, 0,
                                                WINDOW_WIDTH, WINDOW_HEIGHT,
                                                self.background)

            self.uimanager.draw()

        elif self.scene == GAME:

            arcade.draw_lrwh_rectangle_textured(0, 0,
                                                WINDOW_WIDTH, WINDOW_HEIGHT,
                                                self.background)

            self.asteroid_list.draw()
            self.ship_life_list.draw()
            self.bullet_list.draw()
            self.player_sprite_list.draw()

            output = f"Score: {self.score}"
            arcade.draw_text(output, 10, 70, arcade.color.WHITE, 13)

            output = f"Asteroid Count: {len(self.asteroid_list)}"
            arcade.draw_text(output, 10, 50, arcade.color.WHITE, 13)

        elif self.scene == HOW_TO:

            arcade.draw_lrwh_rectangle_textured(0, 0,
                                                WINDOW_WIDTH, WINDOW_HEIGHT,
                                                arcade.load_texture("Resources/Background/howto.png"))

            self.how_to_manager.draw()

        elif self.scene == GAME_OVER:
            text = "Game Over"
            arcade.draw_text(text, 250, 300, (61, 212, 45), 30,
                             400,  center , "Algerian", False, False,
                              center ,  center )

   
    def on_update(self, x):

        """Update game state"""
        self.frame_count += 1

        if not self.game_over:
            self.asteroid_list.update()
            self.bullet_list.update()
            self.player_sprite_list.update()

            for bullet in self.bullet_list:
                asteroids = arcade.check_for_collision_with_list(bullet,
                                                                 self.asteroid_list)

                for asteroid in asteroids:
                    # expected AsteroidSprite, got Sprite instead
                    self.split_asteroid(cast(AsteroidSprite, asteroid))
                    asteroid.remove_from_sprite_lists()
                    bullet.remove_from_sprite_lists()

                # Remove bullet if it goes off-screen
                size = max(bullet.width, bullet.height)
                if bullet.center_x < 0 - size:
                    bullet.remove_from_sprite_lists()
                if bullet.center_x > WINDOW_WIDTH + size:
                    bullet.remove_from_sprite_lists()
                if bullet.center_y < 0 - size:
                    bullet.remove_from_sprite_lists()
                if bullet.center_y > WINDOW_HEIGHT + size:
                    bullet.remove_from_sprite_lists()

            if not self.player_sprite.respawning:
                asteroids = arcade.check_for_collision_with_list(self.player_sprite,
                                                                 self.asteroid_list)
                if len(asteroids) > 0:
                    if self.lives > 0:
                        self.lives -= 1
                        self.player_sprite.respawn()
                        self.split_asteroid(cast(AsteroidSprite, asteroids[0]))
                        asteroids[0].remove_from_sprite_lists()
                        self.ship_life_list.pop().remove_from_sprite_lists()
                        print("Crash")
                    else:
                        self.game_over = True
        elif self.game_over:
            self.game_over_screen()

    def game_over_screen(self):

        self.scene = GAME_OVER
        time.sleep(10)
        self.scene = MENU
        self.start_new_game()


def main():
    """Main function to start game"""
    window = MyGame()
    window.start_new_game()
    arcade.run()


if __name__ == "__main__":
    main()

以上是我的一些法典,主要是MyGame init, 最新版,摘录和游戏_over_ Screen方法。 我可以分享整个源代码,但不想在这里太多,除非有必要,这样就与这方面工作直接改变的方法相左。 最初,我手提的是从游戏中直接写成的“透视”方法,但把它移到“筛选”上,因为假定这是最初的问题,而且不会改变其运作方式。

目前,在袭击了一名roid子并夺走了最后生命之后,它冻结了拖延的时间,然后是主要菜单检查返回。 任何建议都非常欢迎。

问题回答

time.sleep won t work with Arcade. You have to manage sleep yourself. Below is the simple example of displaying screen for a few seconds and then switching to another one:

import time
import arcade


class Game(arcade.Window):

    def __init__(self):
        super().__init__()
        self.scene =  Game Screen 
        self.start = time.time()

    def on_draw(self):
        self.clear()
        arcade.draw_text(self.scene, self.width/2, self.height/2, arcade.color.RED, 50, anchor_x= center )

    def on_update(self, delta_time):
        if time.time()-self.start > 5:
            self.scene =  Game Over 


Game()
arcade.run()

“enterography





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签