Table of Contents

gotoxy()

Function Definition: gotoxy(x = 100, y = 100)

Parameters

NameTypeDescriptionExpected ValuesDefault Value
xintThe x coordinate where the sprite need to be moved.-240 to 240100
yintThe x coordinate where the sprite need to be moved.-180 to 180100

Description

The function sets its sprite’s X and Y position to the specified value. This block has no animation in its movement — it is the simplest way to move a sprite around the screen without displaying any animation (i.e. gliding). Therefore, this block is used whenever a sprite needs to jump to another spot.

Example

The example demonstrates using key sensing to control the sprite's movement in Python.

Code

sprite = Sprite('Beetle')

sprite.setdirection(90)
sprite.gotoxy(0, 0)

while True:
  if sprite.iskeypressed('up arrow'):
    sprite.move(3)
  if sprite.iskeypressed('down arrow'):
    sprite.move(-3)
  if sprite.iskeypressed('left arrow'):
    sprite.left(3)
  if sprite.iskeypressed('right arrow'):
    sprite.right(3)

Output

Read More
The example demonstrates the sprite direction in Python.

Code

sprite = Sprite('Arrow1')

sprite.gotoxy(0, 0)
sprite.setsize(300)

while True:
  if sprite.iskeypressed("left arrow"):
    sprite.left(3)
  if sprite.iskeypressed("right arrow"):
    sprite.right(3)
  sprite.say("Direction is " + str(sprite.direction()))

Output

Read More
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