简体   繁体   English

Python kivy 足球杂耍比赛

[英]Python kivy soccer juggling game

I'm making a game in kivy.我正在 kivy 中制作游戏。 It's a soccer juggling game where whenever the soccer ball is clicked on the ball moves up and then comes down automatically in a straight line.这是一个足球杂耍游戏,只要点击足球,球就会向上移动,然后自动直线下降。 What I'm trying to do is whenever I click on the left side of the soccer ball I want the ball to move up and move to the right and whenever I click on the right side of the ball I want the ball to move up and move to the left side.我想做的是每当我点击足球的左侧时,我希望球向上移动并向右移动,每当我点击球的右侧时,我希望球向上移动并且移动到左侧。 How can I do that?我怎样才能做到这一点? any help is very much appreciated!很感谢任何形式的帮助! Below is my code!下面是我的代码!

Also, here's a link to a game similar to the one I'm trying to make https://s3-eu-west-1.amazonaws.com/apps.playcanvas.com/ajGKB8bb/index.html另外,这是一个游戏的链接,类似于我正在尝试制作的游戏 https://s3-eu-west-1.amazonaws.com/apps.playcanvas.com/ajGKB8bb/index.html

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
        self.root.ids.game_screen.ids.over.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"
        self.root.ids.game_screen.ids.over.opacity = 0

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



MainApp().run()

homescreen.kv主屏幕.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游戏画面.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: "white"
            outline_color: "black"
            outline_width: 1.5
            pos_hint: { "center_x": 0.5, "center_y": 0.6}
            opacity: 0


        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

I can't test it but you should check something like self.center.x < touch.pos.x to detect click on right side and set velocity_x我无法对其进行测试,但您应该检查self.center.x < touch.pos.x类的内容以检测右侧的点击并设置velocity_x

velocity_x = NumericProperty(0)

def on_touch_down(self, touch):
    if Vector(self.center).distance(touch.pos) <= 33:

        if   self.center.x < touch.pos.x:  # click on right side
           self.velocity_x = -0

        elif self.center.x > touch.pos.x:  # click on left side
           self.velocity_x = +10

        else:   # click center
           self.velocity_x = 0

and later use velocity_x to move it然后使用velocity_x移动它

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

        ball.x = ball.x + ball.velocity_x * time_passed

To make simpler to click center you can use some range on both sides - ie.为了使点击中心更简单,您可以在两侧使用一些范围 - 即。 5 pixels 5 像素

        if   self.center.x + 5 < touch.pos.x:    # click on right side
           self.velocity_x = -10

        elif self.center.x - 5 > touch.pos.x:  # click on left side
           self.velocity_x = +10

        else:   # click center
           self.velocity_x = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM