Skip to content

Instantly share code, notes, and snippets.

@sathed
Created February 18, 2026 02:09
Show Gist options
  • Select an option

  • Save sathed/9e7ade1b232350ba323000ae1be60afe to your computer and use it in GitHub Desktop.

Select an option

Save sathed/9e7ade1b232350ba323000ae1be60afe to your computer and use it in GitHub Desktop.
# These functions are listeners. When two sprites collide, these functions run.
# When a collision happens, run this:
def on_on_overlap(sprite, other_sprite):
info.change_score_by(1)
coin.set_position(randint(10, 150), randint(10, 110))
sprites.on_overlap(SpriteKind.player, SpriteKind.food, on_on_overlap)
# When a collision happens, run this:
def on_on_overlap2(sprite2, other_sprite2):
info.change_life_by(-1)
pause(200)
mySprite.x += randint(-30, 30)
mySprite.y += randint(-30, 30)
enemy.set_position(randint(10, 150), randint(10, 110))
sprites.on_overlap(SpriteKind.player, SpriteKind.enemy, on_on_overlap2)
# Creates the variables for the enemy, coin, and player.
enemy: Sprite = None
coin: Sprite = None
mySprite: Sprite = None
# Create the player sprite and initialize the score to 0 and the number of lives to 3.
mySprite = sprites.create(img("""
. . . . f f f f f . . . . . . .
. . . f e e e e e f . . . . . .
. . f d d d d e e e f . . . . .
. c d f d d f d e e f f . . . .
. c d f d d f d e e d d f . . .
c d e e d d d d e e b d c . . .
c d d d d c d d e e b d c . f f
c c c c c d d d e e f c . f e f
. f d d d d d e e f f . . f e f
. . f f f f f e e e e f . f e f
. . . . f e e e e e e e f f e f
. . . f e f f e f e e e e f f .
. . . f e f f e f e e e e f . .
. . . f d b f d b f f e f . . .
. . . f d d c d d b b d f . . .
. . . . f f f f f f f f f . . .
"""),
SpriteKind.player)
controller.move_sprite(mySprite, 100, 100)
mySprite.set_stay_in_screen(True)
info.set_life(3)
info.set_score(0)
# Create the coin sprite in a random location within the game scene.
coin = sprites.create(img("""
. . . . . . . . .
. . 6 6 6 6 . . .
. 6 6 6 6 6 6 . .
. 6 6 6 6 6 6 . .
. 6 6 6 6 6 6 . .
. 6 6 6 6 6 6 . .
. . 6 6 6 6 . . .
. . . . . . . . .
"""),
SpriteKind.food)
coin.set_position(randint(10, 150), randint(10, 110))
# Create the enemy sprite(s) in a random location and set their speed.
enemy = sprites.create(img("""
. . . . . . . . .
. . 2 2 2 2 . . .
. 2 2 2 2 2 2 . .
. 2 2 f 2 2 f . .
. 2 2 2 2 2 2 . .
. . 2 2 2 2 . . .
. . . . . . . . .
"""),
SpriteKind.enemy)
enemy.set_position(randint(10, 150), randint(10, 110))
enemy.follow(mySprite, 35)
# Constantly running, over and over. We can use this function to make
# changes while the game is running.
def on_on_update():
if info.score() >= 10:
game.over(True)
game.on_update(on_on_update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment