当前位置: 首页>>代码示例>>Python>>正文


Python Ball.getCenter方法代码示例

本文整理汇总了Python中Ball.Ball.getCenter方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.getCenter方法的具体用法?Python Ball.getCenter怎么用?Python Ball.getCenter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Ball.Ball的用法示例。


在下文中一共展示了Ball.getCenter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import getCenter [as 别名]
class Pong:

    def __init__(self):
        #creates the window, the ball, the two paddles, and draws the background and the scoreboard.
        self.win = GraphWin("Pong", 800, 400)
        self.background = Image(Point(400,200),"pingpong.gif")
        self.background.draw(self.win)
        self.ball=Ball(self.win)
        self.paddle = Paddle(self.win,740,150,750,250,"red")
        self.paddle1 = Paddle(self.win, 60, 150,70,250,"blue")
        self.radius = self.ball.getRadius()

        self.scoreboard = Text(Point(400, 50), "")
        self.scoreboard.setSize(12)
        self.scoreboard.setTextColor("white")
        self.scoreboard.draw(self.win)
              
        y = 200
        self.middle = self.paddle.move(self.win, y)
        self.middle1 = self.paddle1.move(self.win,y)

                   
    def checkContact(self):
          #gets the values for the top and bottom of the paddles
          self.top = self.paddle.getMiddle()- (self.paddle.getHeight()/2)
          self.bot = self.paddle.getMiddle() +(self.paddle.getHeight()/2)

          self.top1 = self.paddle1.getMiddle() - (self.paddle1.getHeight()/2)
          self.bot1 = self.paddle1.getMiddle() + (self.paddle1.getHeight()/2)
          
          #gets the values of the left and right edges of the ball
          right = self.ball.getCenter().getX()+self.radius
          left = self.ball.getCenter().getX()-self.radius
          ballHeight = self.ball.getCenter().getY()

          touch = right - self.frontedge
          touch1 = self.frontedge1 - left
          
          #if the ball touches either paddle it returns true
          if (0 <= touch <= 10) or (0<= touch1 <= 10):
              if(self.top < ballHeight  <self.bot) and self.ball.moveRight():
                  return True
              elif (self.top1 < ballHeight < self.bot1) and self.ball.moveLeft():
                  return True
              else:
                  return False

    def gameOver(self):
        
        self.frontedge = self.paddle.getEdge()
        self.frontedge1 = self.paddle1.getEdge()
        ballWidth = self.ball.getCenter().getX()
        #returns true if the ball passes either of the paddles
        if (ballWidth > self.frontedge): 
            return True
        elif(ballWidth < self.frontedge1):
            return True
        else:
            return False

    
    def play(self):

        click = self.win.getMouse()
        y = click.getY()
        end = self.gameOver()
        contact = self.checkContact()
        self.hits = 0
        self.level = 1
       
       
        while not end:
          #moves the paddles based on the user's click point
          #if the ball is moving right the right paddle moves
          #if the ball is moving left the left paddle moves
          click = self.win.checkMouse()
          if click != None and  self.ball.moveRight():
            y = click.getY()
            self.paddle.move(self.win, y)
          elif click != None and self.ball.moveLeft():
            y = click.getY()
            self.paddle1.move(self.win, y)

          #moves the ball and reverses the X direction of the ball
          self.ball.move(self.win)
          sleep(0.025)
          contact = self.checkContact()
          if contact == True :
              self.ball.reverseX()
              self.hits = self.hits+1
              #increases ball speed after every 5 hits
              if self.hits%5==0:
                  self.ball.goFaster()
                  self.level=self.level+1

                  
          self.scoreboard.setText(("Hits:",self.hits, "Level:", self.level))   
          end = self.gameOver()

        self.scoreboard = Text(Point(400, 100),"You Lost")
#.........这里部分代码省略.........
开发者ID:cwormsl2,项目名称:2PlayerPongGame,代码行数:103,代码来源:cwormsl2Pong.py

示例2: __init__

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import getCenter [as 别名]
class Pong:

    def __init__(self):
        
        w = 500
        h = 500
        self.pWin = GraphWin("Pong", w, h )
        myIMG = Image(Point(250, 250), "linesfinished.gif")
        myIMG.draw(self.pWin)
        self.b = Ball(self.pWin)
        self.p = Paddle(self.pWin)
        self.hits = 0
        self.score = 0
        self.level = 1
        
        self.scoreTitle = Text(Point(350, 25), "Score:")
        self.scoreTitle.setTextColor("white")
        self.scoreTitle.setSize(25)
        
        self.userScore = Text(Point(405, 25), self.score)
        self.userScore.setTextColor("white")
        self.userScore.setSize(25)
        
        self.levelTitle = Text(Point(100, 25), "Level:")
        self.levelTitle.setTextColor("white")
        self.levelTitle.setSize(25)
        
        self.userLevel = Text(Point(150, 25), self.level)
        self.userLevel.setTextColor("white")
        self.userLevel.setSize(25)
        
        self.scoreTitle.draw(self.pWin)
        self.userScore.draw(self.pWin)
        self.levelTitle.draw(self.pWin)
        self.userLevel.draw(self.pWin)

    def checkContact(self):
        
        xMiddle = self.p.getMiddle().getX()
        xPadLeft = xMiddle - 5
        xPadRight = xMiddle + 5
        xCenter = self.b.getCenter().getX()
        xBallRight = xCenter + self.b.getRadius()
        xBallLeft = xCenter - self.b.getRadius()

        yMiddle = self.p.getMiddle().getY()
        yPadTop = yMiddle - 50
        yPadBottom = yMiddle + 50
        yCenter = self.b.getCenter().getY()
        yBallTop = yCenter - self.b.getRadius()
        yBallBottom = yCenter + self.b.getRadius()

#       If the x value of right side of the ball is = to the x value of the left side of the paddle,
#       and the y value of the top/bottom of the ball is not outside the y value of the top/bottom of the paddle,
#       then the ball has made contact

        if (xBallRight >= xPadLeft and yBallBottom >= yPadTop) and (xBallRight >= xPadLeft and yBallTop <= yPadBottom):
            self.hits += 1
            self.score += 1
            self.userScore.setText(self.score)
            if self.hits % 2 == 0:
                self.level += 1
                self.userLevel.setText(self.level)
                self.b.goFaster()

                if self.level % 4 == 0:
                    self.tier = Text(Point(250, 250), "You have reached a new tier")
                    self.tier.setTextColor("red")
                    self.tier.setSize(20)
                    self.tier.draw(self.pWin)
                    self.wow = Text(Point(250, 81), "WOW!")
                    self.wow.setSize(30)
                    self.wow.setTextColor("red")
                    self.wow.draw(self.pWin)
                    sleep(3)
                    self.tier.undraw()
                    self.wow.undraw()

            return True
        else:
            return False          


    def checkContactShrink(self):
        
        xMiddle = self.p.getMiddle().getX()
        xPadLeft = xMiddle - 5
        xPadRight = xMiddle + 5
        xCenter = self.b.getCenter().getX()
        xBallRight = xCenter + self.b.getRadius()
        xBallLeft = xCenter - self.b.getRadius()

        yMiddle = self.p.getMiddle().getY()
        yPadTop = yMiddle - 50
        yPadBottom = yMiddle + 50
        yCenter = self.b.getCenter().getY()
        yBallTop = yCenter - self.b.getRadius()
        yBallBottom = yCenter + self.b.getRadius()

#       If the x value of right side of the ball is = to the x value of the left side of the paddle,
#.........这里部分代码省略.........
开发者ID:MichaelGardiner97,项目名称:Pong,代码行数:103,代码来源:Pong.py


注:本文中的Ball.Ball.getCenter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。