Anki Vector Plays Pong With You

Kinvert made another custom game using the Anki Vector SDK. This time we made it so you can play a game of pong against Vector. Good luck beating him.

Copy and paste the code in this article to the Anki Vector SDK to play this game.

If you want to learn Python, Cozmo and Vector are great choices. One of our popular articles is Cozmo vs Vector.

Kinvert has a lot of other Vector Coding Examples.

Playing Pong With Vector

https://www.youtube.com/watch?v=DgAguGLkpCc
People in our mailing list have been requesting more SDK content, and more Game to Play With Vector.

This time we programmed Vector to play pong against us. Vector controls the red paddle, while we control the blue paddle by touching his back.

Pong Commands – Pongmands

Time to take a look at some of the pongmands:

  • robot.behavior.set_head_angle()
  • robot.say_text()
  • variables
  • for loops
  • time.sleep()
  • robot.touch.last_sensor_reading.is_being_touched
  • robot.touch.last_sensor_reading.raw_touch_value
  • if
  • elif
  • anki_vector.screen.convert_image_to_screen_data()
  • robot.screen.set_screen_with_image_data()
  • random.randint()
  • PIL.Image.new()
  • PIL.ImageDraw.Draw()
  • PIL ellipse
  • PIL rectangle
  • abs()

Though beginners might not fully understand this code, don’t let that worry you.

First, we’re making an online course!

Second, you can just copy and paste the code to run it in the SDK.

The Code That Makes the Ball Bounce

The code for this game is pretty long. I won’t have time to write the more full explanation like I usually do.

Long story short:

We make an X and Y velocity and position for the ball. These values will change depending on bounces off walls and paddles.

Vector is OP at the moment and is practically unbeatable. I might program in different logic for Vector to play with. For now, prepare to get pongslaughtered.

Program Vector Play Games with the SDK this time Kinvert made Vector play Pong with touch


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
Free for personal use
"""

import anki_vector
from anki_vector.util import degrees
import random
import sys
import time

try:
    from PIL import Image, ImageDraw
except ImportError:
    sys.exit("Cannot import from PIL. Do `pip3 install --user Pillow` to install")
 
def npc_vector(bx,by,bvx,bvy):
    return by + random.randint(-5,5)
    
def draw_face(x, y, bx, by, px, py, vx, vy):
    dimensions = (184, 96)
    face_image = Image.new(
        'RGBA', dimensions, (0, 0, 0, 255))
    dc = ImageDraw.Draw(face_image)
    dc.ellipse([bx-5, by-5, bx+5, by+5], fill=(255, 255, 0, 255))
    dc.rectangle([px-3, py-10, px, py+10], fill=(0, 0, 255, 255))
    dc.rectangle([vx+3, vy-10, vx, vy+10], fill=(255, 0, 0, 255))
    return face_image
 
def impact(bx,by,bvx,bvy,paddleY):    
    if abs(paddleY-by) < 10:
        bvx = bvx * -1
        bvy += (0.5 * (by - paddleY))
        if abs(bvy) < 0.2:
            bvy = 0.5
        bvx = bvx * 1.1
    return bvx, bvy

args = anki_vector.util.parse_command_args()
with anki_vector.Robot(args.serial) as robot:
    robot.behavior.set_head_angle(degrees(45.0))
    robot.say_text("Don't touch me yet")
    untouchTotal = 0
    for i in range(20):
        untouchTotal += robot.touch.last_sensor_reading.raw_touch_value
    untouched = untouchTotal/20.0
    time.sleep(1.0)
    robot.say_text("touch my whole back")
    while not robot.touch.last_sensor_reading.is_being_touched:
        time.sleep(0.1)
    time.sleep(1.0)
    touchTotal = 0
    for i in range(20):
        touchTotal += robot.touch.last_sensor_reading.raw_touch_value
    touched = touchTotal/20.0
    diff = touched - untouched
    robot.say_text("OK you can stop. Get ready. You are blue.")

    over = 0
    bx = 90
    by = 40
    bvx = -5
    bvy = -1
    px = 10
    vx = 173
    while not over:
        py = 95 * ( (robot.touch.last_sensor_reading.raw_touch_value - untouched) / diff )
        vy = npc_vector(bx,by,bvx,bvy)
        if by <= 0:
            bvy = bvy * -1
        if by > 95:
            bvy = bvy * -1
        if bx <= px and bx >= 0:
            bvx, bvy = impact(bx,by,bvx,bvy,py)
        if bx >= vx and bx <= 183:
            bvx, bvy = impact(bx,by,bvx,bvy,vy)
        bx += bvx
        by += bvy
        if bx < 0:
            robot.say_text("I win")
            over = 1
        elif bx > 183:
            robot.say_text("You win")
            over = 1
        
        face_image = draw_face(0, 0, bx, by, px, py, vx, vy)
        screen_data = anki_vector.screen.convert_image_to_screen_data(
            face_image)
        robot.screen.set_screen_with_image_data(
            screen_data, 0.1, interrupt_running=True)
        if bvx < 0 and bx < 90:
            time.sleep(0.1)
        else:
            time.sleep(0.01)


Python Code Improvements

Play a game of Pong with Vector using this Vector SDK program written in Python by Kinvert

Currently Vector is more OP than Legolas. How could we make him capable of losing?

What could we do to make the controls a bit more intuitive and smooth?

How could we keep score?

Also I look forward to when the SDK supports sound effects. That will make the game a lot more fun.

What would you do to make Vector rub it in when he inevitably stomps you at Pong?

Let Me Bounce These Other Ideas Off You

Cozmo drawing for online coding class learn to program anki cozmo robot in python

Python isn’t for everyone, and we all start somewhere. If this seems a bit over your head look at What is Robotics and Kinvert’s Ultimate Guide to Block Coding.

Keith, give me more Python! Sure thing Pong newb, check out Anki Vector Examples and Anki Cozmo Examples. Oh and here is another plug for Vector Play Games.

Want to know when the newest games come out? You won’t regret subscribing to our super duper mailing list. You can unsubscribe at any time. Just please don’t do it on my birthday.

Hope this game was useful and fun. Please let us know what you think in the comments 🙂

7 responses to “Anki Vector Plays Pong With You

    1. What is the error code?

      In the code you pasted it is missing a > greater than symbol. Perhaps that got lost in translation somewhere on my website?

      OK yes I see it. The formatting this website displayed the code in is wrong. Try fixing the indentation. I’ll try to fix it on my end.

      Thanks for finding this!

    2. OK I fixed that. When I pasted in the code block for the article it messed up the white space.

      Please let me know if this fixed it or if something else got goofed up when I pasted in the code to the WYSIWYG.

  1. Hi Keith, thanks for making this awesome Pong game. I was wondering if you could give me a hint to make Vector more defeatable(I only defeated him once). 🙁

  2. Thanks for programming this game, Keith. I was wondering if you could give me a hint as to how to make Vector more defeatable(I have only been able to beat him once). 🙁

    1. I could tell you, but it’s better if you try and figure it out.

      Try to find the portion of the code that is controlling Vector’s paddle, and see if you can make some modifications there 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *