2014-10-20 10 views
0

kivy를 사용하여 pong 게임을 만들려고합니다. 그러나 누군가가 10 점을 얻은 후에 게임을 끝내려고하면 문제가 발생합니다. 레이블을 사용하여 게임 오버 텍스트를 표시하려는 경우를 제외하고는 모든 것이 잘 작동합니다. 레이블을 만들었고 현재 텍스트가 빈 문자열로 설정되어 있지만 누군가가 충분한 점수를 얻은 후에 변경해야합니다.Kivy 위젯 텍스트 변경

은 내가 kivy 문서를 통해보고 및 유래를 검색하지만 난 이것에 대한 해답을 발견하지 않았습니다했다 (내가 이해 적어도 하나 또는.) 누군가가 올바른 방향으로 날 지점 수 있다면

정말 것 고맙습니다. 여기

내 코드입니다 :

평 파일 :

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.label import Label 
from kivy.properties import NumericProperty, ReferenceListProperty,\ 
ObjectProperty 
from kivy.vector import Vector 
from kivy.clock import Clock 


count1 = 0 
count2 = 0 



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 serve_ball(self, vel=(4, 0)): 
     self.ball.center = self.center 
     self.ball.velocity = vel 

    def update(self, dt): 
     global count1 
     global count2 

     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.player1.score += 1 
      count2 = count2 + 1 
      self.serve_ball(vel=(4, 0)) 
     if self.ball.x > self.width: 
      self.player2.score += 1 
      self.serve_ball(vel=(-4, 0)) 

     if count1 or count2 >= 10: 
      # 
      # 
      # 
      #I don't know what needs to be placed here to change the third label's text 


    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 PongApp(App): 
    def build(self): 
     game = PongGame() 
     game.serve_ball() 
     Clock.schedule_interval(game.update, 1.0/60.0) 
     return game 



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

.kv 파일

#:kivy 1.0.9 

<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.player2.score) 

    Label: 
     font_size: 70 
     center_x: root.width * 3/4 
     top: root.top - 50 
     text: str(root.player1.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 

    Label: 
     id: game_over_label 
     text: " " 
     font_size: 100 
     center_x: root.width/2 
     center_y: root.height/2 

답변

1

당신은 당신이 사용하는 라벨을 통해 게임을 변경하는 같은 방법을 사용합니다 점수가 변경되면 점수 레이블을 변경하십시오. 당신이 main.py에서 PongGameObjectProperty를 추가하는 경우

:

end_label = ObjectProperty() 

을 그리고 PongGame에서 당신의 KV 파일에 쓰기 :

end_label: game_over_label # linking them up 

당신은 주에 PongGame 클래스에 기능을 추가 할 수 있습니다 .py :

def check_score(self): 
    scores = [self.player1.score, 
       self.player2.score] 
    for s in scores: 
     if s == 10: 
      self.end_label = "Game Over" 
      return 

update 기능, 당신은 예를 들어,이 기능을 점수가 증가되어 언제든지 호출 할 수 있습니다

self.player1.score += 1 
self.check_score() 

self.player2.score

0

하나의 방법에 대한 동일한 ID를 찾을 수있는 나무가 "리디렉터"를 넣어하는 것입니다 내부 오른쪽 아래 PongGame 위젯 .kv에서

의 루트 :

평 아래에서
 gameoverlabel: game_over_label 

은 "경우> = 10 COUNT1 또는 COUNT2" 이 도움이됩니다

 self.gameoverlabel.text = "win" 

희망을 추가