Play Pong Game Against Cozmo

Kinvert made another custom game using the Anki Cozmo SDK. This time we made a Pong Cozmo Game. See how you match up against Cozmo.

Use this code with the Anki Cozmo SDK to play the game.

Want to learn Python? Cozmo and Vector are a great way to do just that. Cozmo vs Vector is one of our most popular articles.

This is one of many Anki Cozmo SDK Examples.

Playing Pong Game With Cozmo

https://youtu.be/-XsSq-W8FSg
The wonderful people in our mailing list wanted some more SDK content. So we came up with this idea and made it happen.

We programmed Cozmo so he can play you in Pong. Let’s talk a little bit about it before copying the code.

Pong Commands – Pongmands

Time to take a look at some of the pongmands we use for Cozmo:

  • robot.set_head_angle()
  • robot.say_text()
  • variables
  • for loops
  • time.sleep()robot.pose_pitch.degrees
  • if
  • elif
  • cozmo.oled_face.convert_image_to_screen_data()
  • robot.display_oled_face_image()
  • random.randint()
  • PIL.Image.new()
  • PIL.ImageDraw.Draw()
  • PIL ellipse
  • PIL rectangle
  • abs()

Don’t worry if you don’t understand the code.

First of all, we’re creating an online course for Cozmo. It will help you understand code like this and enable you to make your own games.

Second, you can just copy and paste this code. Even if you don’t understand it, it will still work with the SDK.

The Pong Code

This isn’t the shortest code we’ve written. So I won’t have time to go in depth with how the code works.

So I will explain. Wait, there is too much. Let me sum up:

We create an X and Y position and velocity for the ball. If a bounce is detected on a wall or paddle, the values will change.

Cozmo is totally OP right now with the way I programmed him. You won’t stand a chance. Prepare to get pongslaughtered.

Anki Cozmo custom game with the SDK play Pong with Cozmo and Kinvert


"""
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 cozmo
from cozmo.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_cozmo(bx,by,bvx,bvy):
    return by + random.randint(-5,5)
    
def draw_face(x, y, bx, by, px, py, vx, vy):
    dimensions = (128, 64)
    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, 255, 255))
    dc.rectangle([px-3, py-10, px, py+10], fill=(255, 255, 255, 255))
    dc.rectangle([vx+3, vy-10, vx, vy+10], fill=(255, 255, 255, 255))
    return face_image
 
def impact(bx,by,bvx,bvy,paddleY, robot):    
    if abs(paddleY-by) < 10:
        robot.play_audio(cozmo.audio.AudioEvents.SfxGameWin)
        cozmo.audio.AudioEvents.MusicCubeWhack
        bvx = bvx * -1
        bvy += (0.5 * (by - paddleY))
        if abs(bvy) < 0.2:
            bvy = 0.5
        bvx = bvx * 1.1
    return bvx, bvy

def kinvert_pong(robot: cozmo.robot.Robot):
    robot.set_head_angle(degrees(45)).wait_for_completed()

    over = 0
    bx = 90
    by = 40
    bvx = -2
    bvy = -1
    px = 5
    vx = 123
    while not over:
        py = 65-robot.pose_pitch.degrees * 2
        vy = npc_cozmo(bx,by,bvx,bvy)
        if by <= 2: bvy = bvy * -1 if by > 61:
            bvy = bvy * -1
        if bx <= px and bx >= 0:
            bvx, bvy = impact(bx,by,bvx,bvy,py, robot)
        if bx >= vx and bx <= 128:
            bvx, bvy = impact(bx,by,bvx,bvy,vy, robot)
        bx += bvx
        by += bvy
        if bx < 0:
            time.sleep(0.5)
            robot.say_text("I win").wait_for_completed()
            robot.play_anim_trigger(cozmo.anim.Triggers.CodeLabWin).wait_for_completed()
            over = 1
        elif bx > 130:
            time.sleep(0.5)
            robot.say_text("You win").wait_for_completed()
            robot.play_anim_trigger(cozmo.anim.Triggers.CodeLabLose).wait_for_completed()
            over = 1
        
        face_image = draw_face(0, 0, bx, by, px, py, vx, vy)
        screen_data = cozmo.oled_face.convert_image_to_screen_data(face_image)
        robot.display_oled_face_image(screen_data, 0.1)
        if bvx < 0:
            time.sleep(0.1)
        else:
            time.sleep(0.01)
            
cozmo.run_program(kinvert_pong)

Improvements for Python Code

How could we make Cozmo less difficult to beat?

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

How could we change the code to help it keep score?

Bounce Around Our Site

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

If Python seems a little too advanced at the moment try looking at What is Robotics and Kinvert’s Ultimate Guide to Block Coding.

I want all the Python Kinvert can give me! OK head over to Anki Vector Examples and Anki Cozmo Examples. Oh and here is another plug for Vector Play Games.

Please note that Anki Has Closed Here’s How You Can Keep Anki Cozmo Safe.

We have a lot more content like this in our mailing list. If you want to be kept up to date please fill out the form below. You can unsubscribe at any time.

Please let us know in the comments below if this was useful or fun 🙂

Leave a Reply

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