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


Python Ball.move方法代码示例

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


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

示例1: ellipseBound

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import move [as 别名]
def ellipseBound(parameters,coordinates):
    value = (coordinates[0]*coordinates[0])/(parameters[0]*parameters[0])+(coordinates[1]*coordinates[1])/(parameters[1]*parameters[1])
    if (value < 1):
        return True 
    else:
        return False



limits=np.array([10,7])
p= np.array([0.1,0.1])
v= np.array([1,0.3])


params = np.array([2,1])

b1 = Ball(1,1,p,v)
boundary = Boundary("Ellipse",ellipseBound,params)

dt=0.1
t=0

print ("#t position velocity")
while t < 2500:
    b1.move(dt)
    #b1.collide(limits)
    b1.checkState(boundary)
    print (str(t) + " " + str(b1.getPosition()) + " " + str(b1.getVelocity()) + " " + str(b1.checkState(boundary)))
    t+=dt;
开发者ID:Daniel-M,项目名称:BilliardsPy,代码行数:31,代码来源:billiard.py

示例2: __init__

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

示例3: Ship

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import move [as 别名]
class Ship(Screen):
	def __init__(self, name_image="ship-1.png"):
		Screen.__init__(self)
		self.keys = {
			"up"          : [K_UP],
			"down"        : [K_DOWN],
			"left"        : [K_LEFT],
			"right"       : [K_RIGHT],
			"rotate_left" : [K_a, K_q],
			"rotate_right": [K_d],
			"fire"        : [K_SPACE],

			# test
			"lose_life": [K_l] # c'est un L
		}

		self.image = pygame.image.load('img/' + name_image).convert_alpha()
		self.rect  = self.image.get_rect()

		self.rect.center = self.srect.center
		self.ball = Ball(default_center=self.rect.center)

		self.vitesse = 2
		self.vitesse_rotate = 2

		self.orginal = self.image.copy()
		self.current_angle = 0

		self.cheated = 0

		self.max_life = 50
		self.life = self.max_life

		self.score = Score()

	def _test_index(self, liste, index):
		""" 
			Cette fonction test si UN des element selectionne par les index sont vrai
			ex:
			liste = [0, 1, 0, 0, 0, 1]
			index = [1, 5] # test l index 1 et 5
		"""
		ok = 0
		for i in index:
			if liste[i]:
				ok = self.cheated if 0 else 1
		return ok

	def _move(self, x, y):

		self.rect.move_ip((x, y))
		self.ball.follow(self.rect.center)

	def _spin(self, angle):
		prev_rect_center = self.rect.center
		image = pygame.transform.rotate(self.orginal, self.current_angle + angle)
		self.rect = image.get_rect(center=prev_rect_center)
		self.current_angle += angle
		self.image = image

	def checker(self):
		self.is_rotate = False
		keystate = pygame.key.get_pressed()
		# bouger
		if self._test_index(keystate, self.keys["left"]):
			if self.rect.left > self.srect.left:
				self._move(-self.vitesse, 0)
		if self._test_index(keystate, self.keys["right"]):
			if self.rect.right < self.srect.right:
				self._move(self.vitesse, 0)
		if self._test_index(keystate, self.keys["up"]):
			if self.rect.top > self.srect.top:
				self._move(0, -self.vitesse)
		if self._test_index(keystate, self.keys["down"]):
			if self.rect.bottom < self.srect.bottom:
				self._move(0, self.vitesse)

		# tourner
		if self._test_index(keystate, self.keys["rotate_left"]):
			self._spin(self.vitesse_rotate)
			self.is_rotate = True
		if self._test_index(keystate, self.keys["rotate_right"]):
			self._spin(-self.vitesse_rotate)
			self.is_rotate = True

		# tirer
		if self._test_index(keystate, self.keys["fire"]):
			self.ball.fire(self.current_angle)


		# test
		if self._test_index(keystate, self.keys["lose_life"]):
			self.life -= 1


	def render(self):
		# on se sert de cette fonction pour faire bouger la balle
		# car elle est appele a chaque fois
		self.ball.move(self.srect, self.rect.center)

#.........这里部分代码省略.........
开发者ID:math2001,项目名称:space-war,代码行数:103,代码来源:Ship.py

示例4: run

# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import move [as 别名]
def run():
    
    CENTER         = [COMMONS.WINDOWWIDTH/2, COMMONS.WINDOWHEIGHT/2]
    DISK_HOLE      = 10
    RADIUS         = 15
    PADDLE_SPEED   = 7
    PADDLE_HEIGHT  = 100
    PADDLE_WIDTH   = 10

    BALL_VELS      = [[3,1], [2,1], [-3,1], [3,-1], [-3,-1], [-2,-2], [-2,1], [2,2]]
    BALL_VEL       = random.choice(BALL_VELS)
    WIN_BANNER_POS = {'left': [100, 50], 'right': [458, 50]}

    ball           = Ball(CENTER, RADIUS, BALL_VEL, DISK_HOLE, COMMONS.REDDISH)
    paddle_1       = Paddle(0, 150, PADDLE_WIDTH, PADDLE_HEIGHT, COMMONS.BLUEISH, PADDLE_SPEED, ball)
    paddle_2       = Paddle(COMMONS.WINDOWWIDTH - PADDLE_WIDTH, 150, PADDLE_WIDTH, PADDLE_HEIGHT, COMMONS.BLUEISH, PADDLE_SPEED, ball)
    ball_copy 	   = copy.deepcopy(ball)

    scoreLeft 	   = 0
    scoreRight     = 0

    pygame.init()
    pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])

    spaces         = "                            " # hack to display both scores at once (avoid double blitting the score Font objects)
    score_Font     = pygame.font.SysFont("Verdana", 35)
    windowSurface  = pygame.display.set_mode((COMMONS.WINDOWWIDTH, COMMONS.WINDOWHEIGHT), DOUBLEBUF, 32)

    pygame.display.set_caption('Pong')
    pygame.draw.circle(windowSurface, ball.color, (ball.x, ball.y), ball.radius)
    pygame.draw.circle(windowSurface, COMMONS.WHITE, (ball.x, ball.y), ball.radius + 2, ball.radius - DISK_HOLE)
    pygame.display.update()

    time.sleep(1)        

    while max(scoreLeft, scoreRight) < 3:
        in_game        = True
        paddle_1_down  = paddle_1_up = paddle_2_down = paddle_2_up = False
        mainCLock      = pygame.time.Clock()
        
        while in_game == True:
            for event in pygame.event.get():
                
                if event.type == KEYDOWN:
                    if event.key == K_DOWN:
                        paddle_2_down = True
                    elif event.key == K_UP:
                        paddle_2_up = True
                    elif event.key == ord('s'):
                        paddle_1_down = True
                    elif event.key == ord('w'):
                        paddle_1_up = True
                    elif event.key == ord('r'):
                        scoreRight  = scoreLeft = 0
                        ball.x      = COMMONS.WINDOWWIDTH/2
                        ball.y      = COMMONS.WINDOWHEIGHT/2
                        ball.vel    = random.choice(BALL_VELS)
                        in_game     = True
                    elif event.key == ord('q'):
                        pygame.quit()
                        sys.exit()

                elif event.type == KEYUP:
                    if event.key == K_DOWN:
                        paddle_2_down = False
                    elif event.key == K_UP:
                        paddle_2_up = False
                    elif event.key == ord('s'):
                        paddle_1_down = False
                    elif event.key == ord('w'):
                        paddle_1_up = False
                
                elif event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                    
            if paddle_1_down:
                paddle_1.move(1)
            elif paddle_1_up:
                paddle_1.move(-1)
            if paddle_2_down:
                paddle_2.move(1)
            elif paddle_2_up:
                paddle_2.move(-1)
                
            ball.check_board_bounce() 
            ball.move()   
            
            if ball.x - ball.radius <= 0: 
                if ball.y >= paddle_1.y and ball.y <= paddle_1.y + paddle_1.height:
                    ball.vel[0] = -int(math.floor(1.1*ball.vel[0]))
                else:
                    scoreRight += 1
                    in_game = False
                    
                    
            elif ball.x + ball.radius >= COMMONS.WINDOWWIDTH:
                if ball.y >= paddle_2.y and ball.y <= paddle_2.y + paddle_2.height:
                    ball.vel[0] = int(math.floor(-1.1*ball.vel[0]))
                else:
#.........这里部分代码省略.........
开发者ID:rectified95,项目名称:Pong,代码行数:103,代码来源:Pong.py


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