且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在屏幕上显示游戏?

更新时间:2023-11-14 12:06:16

好!您的程序中有很多错误,我不得不做很多改进. (我是初学者,所以我理解)

Well! there were a lot of errors in your program and I had to make ton's of improvement. (I understand it as you are beginner)

首先,请阅读完整的猕猴桃语言文档,可以清楚地看到,您无需掌握基础知识即可直接开始进行编码. 您可能会制作几款出色的游戏,但从长远来看,您将面临无法明确概念无法解决的问题. 不幸的是,您将无法发现 kivy 的真正威力. :)

First of all, please read complete kivy language documentation, as I can clearly see that you directly started with coding without grasping the basics. You may make couple of good games but in the long run you will face such problems which can't be solved without clear concepts. And unfortunately you won't be able to discover the true power of kivy. :)

您可能还想对python概念进行修订.

You might also wanna do revision of your python concepts.

有些改进不值得一提,但很重要,阅读代码后您会有所了解.

Some improvements aren't worth mentioning but were important, you will get an idea when you read the code.

改进1:

如果在build()上返回窗口小部件,或者设置了self.root,则可以构建应用程序(但是不能再次使该应用程序成为n). 就像您在这里所做的一样:

An application can be built if you return a widget on build(), or if you set self.root.(But you cannot make the application again n again) as you did here:

<ScreenTwo>:
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game

改进2:

点击屏幕游戏的打乒乓球按钮.您的比赛始于发球.

As you click on button play ping pong which is of screen game. your game starts with serve of ball.

on_release: root.current = 'game';game.serve_ball()

(用于知识)

如果仍然出现黑屏,则可能要检查kivy文件的名称,为此,您可以转到kivy文档或

If you still get black screen you might want to check the name of kivy file, for that you could either go to kivy documentation or this link

 class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.1
            ball.velocity = vel.x, vel.y + offset


class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(PongGame, self).__init__(*args, **kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)

    def serve_ball(self, vel=(4, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        #bounce of paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

    #bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
        self.ball.velocity_y *= -1

    #went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

class Manager(ScreenManager):
    pass

class ScreensApp(App):
    def build(self):
        self.load_kv('t6.kv')
        return Manager(transition=WipeTransition())

if __name__ == '__main__':
    ScreensApp().run()

这是kv文件.

<PongBall>:
    size: 50, 50
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size

<PongPaddle>:
    size: 25, 200
    canvas:
        Rectangle:
            pos:self.pos
            size:self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right

    canvas:
        Rectangle:
            pos: self.center_x-5, 0
            size: 10, self.height

    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)

    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)

    PongBall:
        id: pong_ball
        center: self.parent.center

    PongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y

    PongPaddle:
        id: player_right
        x: root.width-self.width
        center_y: root.center_y

<Manager>:
    id: screen_manager


    Screen:
        name: 'home'
        Button:
            text: 'Play Ping Pong'
            halign: 'center'
            valign: 'middle'
            font_size: 100
            text_size: self.size
            on_release: root.current = 'game';game.serve_ball()

    Screen:
        name: 'game'
        PongGame:
            id: game