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


Python Ball.reverseX方法代码示例

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


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

示例1: __init__

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

#.........这里部分代码省略.........
                    sleep(3)
                    self.tier.undraw()
                    self.wow.undraw()

            return True
        else:
            return False
        
    def gameOver(self):
#       If the ball goes past the paddle, the game is over and the user is asked to play again

        xCenter = self.b.getCenter().getX()
        xBallLeft = xCenter - self.b.getRadius()

        xCenter2 = self.b.getCenter2().getX()
        xBallLeft2 = xCenter2 - self.b.getRadius2()

        xMiddle = self.p.getMiddle().getX()
        xPadRight = xMiddle + 5
        
        if (xBallLeft > xPadRight) or (xBallLeft2 > xPadRight):
            loser = Text(Point(250, 150), "GAME OVER!")
            loser.setSize(25)
            loser.setTextColor("red")
            
            again = Text(Point(250, 200), "PLAY AGAIN?")
            again.setSize(25)
            again.setTextColor("red")

            loser.draw(self.pWin)
            again.draw(self.pWin)

            return True

    def restart(self):
#       If the player types 'yes', the game will restart completely
        
        restart = input("\nWould You Like To Restart (yes/no)? ")
        if restart.lower() == "yes":
            self.pWin.close()
            newGame = Pong()
            print("WARNING!! IN SHRINKING MODE, PADDLE RESETS TO MIDDLE AFTER EACH LEVEL")       
            game = input("Which version would you like to play: 'shrinking mode' (s), 'multiple mode' (m), or 'normal mode' (n)? ")
            newGame.play(game)
        else:
            self.pWin.close()
       
    def play(self, ans):
#       The user will be given 3 seconds after asked to play until the game begins

        sleep(1)

        self.ready = Text(Point(250, 81), "Are You Ready?")
        self.ready.setSize(30)
        self.ready.setTextColor("red")
        self.ready.draw(self.pWin)
        
        self.three = Text(Point(250, 200), "THREE")
        self.three.setSize(35)
        self.three.setTextColor("white")
        self.three.draw(self.pWin)
        sleep(1)
        self.three.undraw()

        self.two = Text(Point(250, 200), "TWO")
        self.two.setSize(35)
        self.two.setTextColor("white")
        self.two.draw(self.pWin)
        sleep(1)
        self.two.undraw()

        self.one = Text(Point(250, 200), "ONE")
        self.one.setSize(35)
        self.one.setTextColor("white")
        self.one.draw(self.pWin)
        sleep(1)
        self.one.undraw()

        self.ready.undraw()
        while True:
            click = self.pWin.checkMouse()
            if click != None:
                self.p.movePad(click)
            if ans.lower() == 's':
                if self.checkContactShrink():
                    self.b.reverseX()
            elif ans.lower() == 'm':
                if self.checkContactBallA():
                    self.b.reverseX()
                if self.checkContactBallB():
                    self.b.reverse2X()
                self.b.moveBall2(self.pWin)
            elif ans.lower() == 'n':
                if self.checkContact():
                    self.b.reverseX()
            else:
                print("\nOh Well, I Didn't Want To Play With You Anyway!")
            self.b.moveBall(self.pWin)
            if self.gameOver():
                self.restart()
开发者ID:MichaelGardiner97,项目名称:Pong,代码行数:104,代码来源:Pong.py


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