【问题标题】:Python kivy restart gamePython kivy 重启游戏
【发布时间】:2021-12-08 23:22:09
【问题描述】:

我正在用 kivy 制作游戏,这是一个足球杂耍游戏。我希望每当足球从屏幕上掉下来时游戏就结束了,我也希望在游戏结束时有一个重新启动按钮,这样我就可以再次玩了。我尝试了很多东西,但似乎没有用。任何帮助表示赞赏。下面是我的代码!谢谢!

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.game_over()

    def game_over(self):
        print("game over")


    def start_game(self):
        Clock.schedule_interval(self.move_ball, 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}

        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)

            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

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    首先,定义一个时钟名称,这样你就可以在游戏结束时停止它:

    self.control = Clock.schedule_interval(self.move_ball, 1/60.)
    

    在 game_over 函数中,让我们停止这个时钟

    self.control.cancel()
    

    现在我们需要为弹出窗口创建 BoxLayout:(game_over 函数

    from kivy.uix.label import Label
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.uix.button import Button
    
    bx = BoxLayout(orientation='vertical') 
    restart_but = Button(text='Restart')
    restart_but.bind(on_release=self.restart_game)
    bx.add_widget(Label(text='Score :'+self.root.ids.game_screen.ids.score.text))
    bx.add_widget(restart_but)
    self.popup = Popup(title='Game Over',content=bx,size_hint=(None, None), size=(400, 400))
    self.popup.open()
    

    现在我们将发布命令定义为restart_but,需要创建新函数

    def restart_game(self,*args):
        self.popup.dismiss()  #Close popup 
        #default settings here..
        self.control = Clock.schedule_interval(self.move_ball, 1/60.) #Start Game
    

    在restart_game功能中,您需要重置所有设置,如球的位置和得分。

    另外我建议你使用 KivyMD 来制作漂亮的弹出窗口和按钮。

    【讨论】:

    • 游戏结束后我每次点击“重启”都不行!
    • 你在同一个班级中添加了 def restart_game 吗?弹出窗口将关闭,游戏球将开始下降。
    • 和什么在同一班?
    • 因为你需要按照我说的重置设置。首先,如果你不重置球的位置,因为球的位置在页面下方,重新开始游戏后游戏将结束。所以将这些添加到 restart_game func 中:ball = self.root.ids.game_screen.ids.ballball.y = 350(找到你的默认 ball.y 位置并在此处输入)
    • 嘿,看看这个视频。 youtube.com/watch?v=2dn_ohAqkus&ab_channel=ErikSandberg我可以让它像他的一样吗,当他的游戏结束时,他只需再次点击播放就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多