Python 绝望的游戏结束标签文本

Python kivy gameover label text

我正在 kivy 中制作游戏,我有一个标签,上面写着“GAME OVER”我希望这个文本在游戏结束时出现,在我点击“开始游戏”时消失。我怎样才能做到这一点?我尝试了很多但似乎找不到任何解决方案,感谢您的帮助!太感谢了!下面是我的代码!

main.py

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.image import Image
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.properties import NumericProperty
from kivy.vector import Vector


class HomeScreen(Screen):
    pass

    def play_sound(self):
        sound = SoundLoader.load('button press sound.wav.')
        if sound:
            sound.play()


sound = SoundLoader.load('Crowd sound effect.wav')
sound.loop = True
sound.play()


class GameScreen(Screen):
    pass

    def play_sound(self):
        sound = SoundLoader.load('button press sound.wav.')
        if sound:
            sound.play()


class Ball(Image):
    velocity = NumericProperty(0)



    def on_touch_down(self, touch):
        if Vector(self.center).distance(touch.pos) <= 33:
            label = App.get_running_app().root.get_screen('game_screen').ids.score
            label.text = str(int(label.text) + 1)
            sound = SoundLoader.load('Soccer ball sound.wav')
            sound.play()
            self.source = "icons/ball.png"
            self.velocity = 275
        return super().on_touch_down(touch)


    def on_touch_up(self, touch):
        if Vector(self.center).distance(touch.pos) <= 33:
            self.source = "icons/ball.png"
        return super().on_touch_up(touch)


class MainApp(App):
    GRAVITY = 300


    def move_ball(self, time_passed):
        ball = self.root.ids.game_screen.ids.ball
        ball.y = ball.y + ball.velocity * time_passed
        ball.velocity = ball.velocity - self.GRAVITY * time_passed
        self.check_collision()

    def check_collision(self):
        ball = self.root.ids.game_screen.ids.ball
        if ball.top < 96:
            self.root.ids.game_screen.ids.score.text = "0"
            self.game_over()


    def game_over(self):
        self.root.ids.game_screen.ids.ball.pos = (0, (0.5) )
        print("game over")
        self.frames.cancel()
        self.root.ids.game_screen.ids.start_button.disabled = False
        self.root.ids.game_screen.ids.start_button.opacity = 1


    def next_frame(self, time_passed):
        self.move_ball(time_passed)


    def start_game(self):
        #Clock.schedule_interval(self.move_ball, 1/60.)
        self.root.ids.game_screen.ids.ball.velocity = 275
        self.frames = Clock.schedule_interval(self.next_frame, 1/60.)
        self.root.ids.game_screen.ids.score.text = "0"


    def change_screen(self, screen_name):
        self.root.current = screen_name



MainApp().run()

homescreen.kv

#:import utils kivy.utils
#:import FadeTransition kivy.uix.screenmanager.FadeTransition


<HomeScreen>:
    FloatLayout:
        canvas:
            Color:
                rgb: utils.get_color_from_hex("#39B3F2")
            Rectangle:
                size: self.size
                pos: self.pos
        GridLayout:
            rows: 1
            pos_hint: {"top": 1, "left": 1}
            size_hint: 1, .9
            Image:
                source: "icons/keepyup.png"
        FloatLayout:
            Button:
                font_size: dp(20)
                font_name: 'SackersGothicStd-Medium.otf'
                text: "PLAY"
                color: "gold"
                pos_hint: { "center_x": .5, "center_y": .3}
                size: 80, 55
                size_hint: None, None
                background_normal: ''
                background_color: (57/255.0, 179/255.0, 242/255.0, .10)


                on_press:

                on_release:
                    root.play_sound()
                    root.manager.transition = FadeTransition()
                    app.change_screen("game_screen")

gamescreen.kv

#:import utils kivy.utils


<GameScreen>:
    FloatLayout:
        canvas:
            Color:
                rgb: utils.get_color_from_hex("#39B3F2")
            Rectangle:
                size: self.size
                pos: self.pos
        GridLayout:
            rows: 1
            pos_hint: {"top": 1, "left": 1}
            size_hint: 1, .1
            Image:
                source: "icons/sun.png"
        GridLayout:
            rows: 1
            pos_hint: {"top": 1, "left": 1}
            size_hint: 1, .2
            Image:
                source: "icons/clouds.png"
        GridLayout:
            rows: 1
            pos_hint: {"bottom": 1, "left": 1}
            size_hint: 1, .5
            Image:
                source: "icons/Field4.png"
                allow_stretch: True
                keep_ratio: False
                pos: self.pos

        Label:
            id: score
            size_hint: None, None
            font_size: dp(25)
            font_name: 'SackersGothicStd-Medium.otf'
            text: "0"
            color: "gold"
            pos_hint: { "center_x": 0.1, "center_y": 0.9}



        Label:
            id: over
            size_hint: None, None
            font_size: dp(25)
            font_name: 'SackersGothicStd-Medium.otf'
            text: "GAME OVER!"
            color: "gold"
            outline_color: "white"
            outline_width: 1
            pos_hint: { "center_x": 0.5, "center_y": 0.6}



        Button:
            size_hint: None, None
            font_size: dp(20)
            font_name: 'SackersGothicStd-Medium.otf'
            text: "Start Game"
            color: "gold"
            pos_hint: { "center_x": 0.5, "center_y": 0.3}
            size: 150, 55
            size_hint: None, None
            background_normal: ''
            background_color: (57/255.0, 179/255.0, 242/255.0, .10)
            id: start_button
            on_release:
                self.disabled = True
                self.opacity = 0
                root.play_sound()
                app.start_game()


        Ball:
            source: "icons/ball.png"
            size_hint: None, None
            size: 500, 500
            pos_hint: {"center_x": 0.5}
            id: ball

main.kv

#:include kv/homescreen.kv
#:include kv/gamescreen.kv


ScreenManager:
    id: screen_manager
    HomeScreen:
        name: "home_screen"
        id: home_screen
    GameScreen:
        name: "game_screen"
        id: game_screen

我建议不要使用标签,而是为此使用弹出窗口。 您可以定义弹出窗口将在单击后关闭,并将删除小部件并将其重新添加到所有其他与游戏相关的布局所在的主布局

这是我的例子: main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button


class GameOverApp(App):
    pass

# The main layout
class MyMainLayout(BoxLayout):

    def game_over(self):
        # creating a button
        btn = Button(text="Restart")
        # Creating a popup and adding the button to it
        # auto_dismiss means that clicking anywhere else won't close the popup
        my_popup = Popup(title="Game over!", content=btn, auto_dismiss=False)
        # binding two functions, one for restarting the game and one for closing the popup
        btn.bind(on_press=self.restart_game)
        btn.bind(on_press=my_popup.dismiss)
        # open the popup
        my_popup.open()

    def restart_game(self, event):
        # Clear all widgets from the mainlayout and re-initialize them
        self.clear_widgets()
        self.add_widget(GameLayout())


# the layout which holds all the game items
class GameLayout(BoxLayout):
    pass

GameOverApp().run()

gameover.kv

MyMainLayout:

<MyMainLayout>
    GameLayout:
    Button:
        text: "Click me for game over"
        on_press: self.parent.game_over()

<GameLayout>
    Label:
        text: "game data..."

Popups with floatlayouts

Kivy documentation

您可以只修改 Labelopacity。在您的 start_game() 方法中,将 opacity 设置为 0:

def start_game(self):
    #Clock.schedule_interval(self.move_ball, 1/60.)
    self.root.ids.game_screen.ids.ball.velocity = 275
    self.frames = Clock.schedule_interval(self.next_frame, 1/60.)
    self.root.ids.game_screen.ids.score.text = "0"
    self.root.ids.game_screen.ids.over.opacity = 0

然后在game_over()方法中,将不透明度设置为1:

def game_over(self):
    self.root.ids.game_screen.ids.ball.pos = (0, (0.5) )
    print("game over")
    self.frames.cancel()
    self.root.ids.game_screen.ids.start_button.disabled = False
    self.root.ids.game_screen.ids.start_button.opacity = 1
    self.root.ids.game_screen.ids.over.opacity = 1