Table of Contents

y()

Function Definition: y()

Parameters

Description

The function returns the sprite’s Y position.

Example

The example demonstrates how to add gravity to the project on a bouncing ball.

Code

sprite = Sprite('Ball')
import time
import random

sprite.setx(random.randrange(-200, 200))
sprite.sety(random.randrange(-100, 140))
gravity = -2
xpos = sprite.x()
ypos = sprite.y()
dx = random.randrange(-50, 50)
dy = random.randrange(-25, 25)

while True:
  dy = dy + gravity
  dx = dx * 0.98
  dy = dy * 0.98
  xpos = xpos + dx
  ypos = ypos + dy

  if ypos < -160:
    dy = -dy
    ypos = -160
  if ypos > 130:
    dy = -dy
    ypos = 130

  if xpos < -220:
    dx = -dx
    xpos = -220
  if xpos > 220:
    dx = -dx
    xpos = 220
  
  sprite.gotoxy(xpos, ypos)
  time.sleep(0.01)

Output

Read More
All articles loaded
No more articles to load