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


Python Ball.moveRight方法代码示例

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


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

示例1: __init__

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import moveRight [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


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