# CircuitPython Neko Kitty with Displayio

## Overview

https://www.youtube.com/watch?v=cXOQv8H5Vgo

This project is a CircuitPython `displayio` port of an old school [animated screenmate application](https://en.wikipedia.org/wiki/Neko_(software)) that ran on mac and other platforms. The sprite sheet included in this project was obtained [from here.](https://www.spriters-resource.com/pc_computer/neko/sheet/106984/)

The cute little kitty will wander around the display and scratch at the edges when it gets close enough. The original ones followed your computer mouse. Our CircuitPython version will chase after a laser dot that you can place by touching the screen. The Neko kitty has a mind of its own, like all cats - it will decide to stop and sleep or clean its paws whenever it wants to.

Whether you just want this cute kitty to keep you company, or you're interested in making you're own custom `displayio` based animated virtual pet, this guide is a great starting point.

## Parts

This project works on the following Adafruit PyPortal smart displays - take your pick.

Featured
### Adafruit PyPortal - CircuitPython Powered Internet Display

[Adafruit PyPortal - CircuitPython Powered Internet Display](https://www.adafruit.com/product/4116)
 **PyPortal** , our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Make custom touch screen interface GUIs, all open-source, and Python-powered using&nbsp;tinyJSON / APIs to get news, stock, weather, cat photos,...

In Stock
[Buy Now](https://www.adafruit.com/product/4116)
[Related Guides to the Product](https://learn.adafruit.com/products/4116/guides)
![Front view of a Adafruit PyPortal - CircuitPython Powered Internet Display with a pyportal logo image on the display. ](https://cdn-shop.adafruit.com/640x480/4116-00.jpeg)

Featured
### Adafruit PyPortal Titano

[Adafruit PyPortal Titano](https://www.adafruit.com/product/4444)
The **PyPortal Titano** is the big sister to our [popular PyPortal](https://www.adafruit.com/product/4116) now with _twice as many pixels!_ The PyPortal is our easy-to-use IoT device that allows you to create all the things for the “Internet of...

In Stock
[Buy Now](https://www.adafruit.com/product/4444)
[Related Guides to the Product](https://learn.adafruit.com/products/4444/guides)
![Hand holding PyPortal Titano development board with SAMD51, ESP32 Wifi, and 3.5" touchscreen TFT display.](https://cdn-shop.adafruit.com/640x480/4444-10.jpg)

Featured
### Adafruit PyPortal Desktop Stand Enclosure Kit

[Adafruit PyPortal Desktop Stand Enclosure Kit](https://www.adafruit.com/product/4146)
PyPortal is&nbsp;our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Create little pocket universes of joy that connect to something good.

And now that you've made a cool internet-connected project...

In Stock
[Buy Now](https://www.adafruit.com/product/4146)
[Related Guides to the Product](https://learn.adafruit.com/products/4146/guides)
![Demo Shot of the Assembled Adafruit PyPortal Desktop Stand Enclosure Kit.](https://cdn-shop.adafruit.com/640x480/4146-03.jpg)

# CircuitPython Neko Kitty with Displayio

## Project Setup

Are you new to using CircuitPython? No worries,&nbsp;[there is a full getting started guide here](https://learn.adafruit.com/welcome-to-circuitpython "Welcome to CircuitPython").

Plug the PyPortal into your computer with a known good USB cable (not a tint charge only cable). The PyPortal will appear to your computer as a flash drive named&nbsp; **CIRCUITPY**. If the drive does not appear you can [install CircuitPython](https://learn.adafruit.com/adafruit-pyportal/install-circuitpython) on your PyPortal and then return here.

Download the project files with the Download Project Bundle button below.&nbsp;Unzip the file and copy/paste the&nbsp; **code.py** &nbsp;and other project files to your&nbsp; **CIRCUITPY** &nbsp;drive using File Explorer or Finder (depending on your operating system).

[Download Project Bundle](https://learn.adafruit.com/pages/23663/elements/3109598/download?type=zip)
## Drive Structure

After copying the files, your drive should look like the listing below. It can contain other files as well, but must contain these at a minimum:

![Project](https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/folder-images/CircuitPython_Neko_Cat.png?raw=true )

## Code

The project code is shown below:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Neko_Cat/code.py

# CircuitPython Neko Kitty with Displayio

## Code Walk-Through

The code for this project relies heavily on [CircuitPython displayio fundamentals](https://learn.adafruit.com/circuitpython-display-support-using-displayio/introduction). If you're not familiar with displayio `TileGrid` and `Group` basics you should take a moment to bush up in the displayio guide and then come back here.

## Configuration

There are a few variables that you can set to modify certain things about the appearance and behavior of the cat and background.

### Application behavior:

- `BACKGROUND_COLOR` - Hex color code to be used for the background of the display. Default is a light blue color, change to whatever color you like!
- `ANIMATION_TIME` - This is the number of seconds between animation sprites. The Default is `0.3` seconds. You can leave it as is, or experiment with faster or slower times.
- `USE_TOUCH_OVERLAY` - Boolean value that enables the touch interaction. Set this to `False` if your device doesn't have a touch screen. Leave it `True` on PyPortal.
- `TOUCH_COOLDOWN` - The time in seconds to wait before registering new touch events. Default is `0.1` seconds, so it will allow up to 10 touch events per second.
- `LASER_DOT_COLOR` - Hex color code for the color of the laser dot when you touch the display. Default is red, feel free to change it.

### Neko Cat Behavior:

- `CONFIG_STEP_SIZE` - How many pixels the cat will move with each step. Default is `10`, you can speed up or slow down if you like.
- `CONFIG_STOP_CHANCE_FACTOR` -&nbsp; How likely the cat is to stop moving and clean its paws or sleep while it's walking. Default is `30`. Lower numbers make the cat more likely to stop walking.
- `CONFIG_START_CHANCE_FACTOR` - How likely the cat is to start moving after scratching at a wall. Default is `10`. Lower numbers make it more likely to stop scratching and start walking.
- `CONFIG_MIN_SCRATCH_TIME` - The minimum time in seconds the cat will spend scratching the wall. Default is `2`.

## Main Program Loop

Inside the main loop of the program two primary tasks occur.

1. call `neko.update()` to process all animation and behavior logic on the cat sprite.
2. Check the touch screen for any incoming touch events. If there is one, place the laser dot circle at the touch location and set the `neko.moving_to` property to the same location.

## NekoAnimatedSprite Class

This class contains all of the logic that controls Neko's behavior. It is extending the `displayio.TileGrid` class so it can control which sprite from the spritesheet is showing when it's time to change them for animating. The core mechanism within this class is a [state machine.](https://learn.adafruit.com/circuitpython-101-state-machines)

### State Objects

There are tuple variables representing each state that Neko can be in. Each one contains:

- A number ID
- A tuple containing 1 or more sprites to show for this state. If multiple it will animate between them.
- A tuple containing the delta values for x and y movements for this state.

There is a state object for each possible moving direction, each possible scratching direction, plus sleeping, cleaning paws, and sitting.

The `current_state` property on the&nbsp;`NekoAnimatedSprite` class allows you to get or set which state Neko is in. Ordinarily this will happen inside of the `update()` function based on the behavior logic and what Neko is doing at the time.

### Update Function

This is the highest level behavior function. It gets called once per iteration inside of the main loop. Within this function these main tasks will occur:

- If we are moving to the specific location of the laser dot (from user touch event), check where we are currently at in relation to the target location and change the moving direction to head towards it if needed. Changing directions is done by changing the `current_state` property.
- Attempt an animation frame. If it has been long enough since the previously drawn animation frame, then it will change the currently showing sprite to the next one in the animation list and advance the index for next time.
- Decide on the next behavior. If we are currently walking, we could decide to stop and sleep, or clean our paws. If we are currently scratching a wall, we could decide to stop scratching and start walking. These behaviors are random chance based on a modifier from the configuration. If we are done sleeping or cleaning, we will decide to start walking. The most likely chance is that we will keep doing whatever we already are.
- Process movement. If there is room in the direction we are heading to take a step, then do so. If we run into a wall, then change into the scratching state that matches the direction of the wall we hit.

### Animation

The `animation()`function is what handles the animation logic. It uses the `LAST_ANIMATION_TIME` variable and the `ANIMATION_TIME` configuration to determine whether it's time to change sprites or not. When it is time to change sprites it's done by changing the index from within the source spritesheet. The NekoAnimatedSprite class extends TileGrid so this occurs by sub-scripting `self` with square brackets like:  
`self[0] = 5`

except instead of `5` it would pick whatever index is currently up in the list of sprites for the current animation based on the state. Our sprite is only a single tile so we always use `self[0]`. If we had more tiles, we might use higher numbers inside the square brackets. Learn more about TileGrids In the [DisplayIO Guide](https://learn.adafruit.com/circuitpython-display-support-using-displayio/tilegrid-and-group).

The indexes represent the position of the desired sprite within the full sprite sheet. Starting with 0 in the top left and moving right, then down in the same order and directions as you would read English words on a page.

![projects_neko_cat_spritesheet.bmp](https://cdn-learn.adafruit.com/assets/assets/000/108/510/medium640/projects_neko_cat_spritesheet.bmp?1643643842)

## Further Detail

The source code is thoroughly commented to explain what the statements are doing. You can read through the code and comments to gain a deeper understanding of how it functions, or modify parts of it to suit your needs.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Neko_Cat/code.py

# CircuitPython Neko Kitty with Displayio

## Other Devices

The Neko kitty program can run on other CircuitPython devices too! For any device with a built-in display and touch overlay you can run the project code as-is. If your device doesn't have a built-in touch overlay there is a variable to disable the touch functionality. Be sure to set `USE_TOUCH_OVERLAY` to `False` as shown below:

```auto
# whether to use a touch overlay
USE_TOUCH_OVERLAY = False
```

![](https://cdn-learn.adafruit.com/assets/assets/000/108/509/medium800/lcds___displays_neko_other_devices_1.png?1643642290)


## Guide Products

### Adafruit PyPortal - CircuitPython Powered Internet Display

[Adafruit PyPortal - CircuitPython Powered Internet Display](https://www.adafruit.com/product/4116)
 **PyPortal** , our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Make custom touch screen interface GUIs, all open-source, and Python-powered using&nbsp;tinyJSON / APIs to get news, stock, weather, cat photos,...

In Stock
[Buy Now](https://www.adafruit.com/product/4116)
[Related Guides to the Product](https://learn.adafruit.com/products/4116/guides)
### Adafruit PyPortal Titano

[Adafruit PyPortal Titano](https://www.adafruit.com/product/4444)
The **PyPortal Titano** is the big sister to our [popular PyPortal](https://www.adafruit.com/product/4116) now with _twice as many pixels!_ The PyPortal is our easy-to-use IoT device that allows you to create all the things for the “Internet of...

In Stock
[Buy Now](https://www.adafruit.com/product/4444)
[Related Guides to the Product](https://learn.adafruit.com/products/4444/guides)
### Adafruit PyPortal Pynt - CircuitPython Powered Internet Display

[Adafruit PyPortal Pynt - CircuitPython Powered Internet Display](https://www.adafruit.com/product/4465)
The **PyPortal Pynt** is the little&nbsp;sister to our [popular PyPortal](https://www.adafruit.com/product/4116) - zapped with a shrink ray to take the design from a 3.2" diagonal down to 2.4" diagonal screen - but otherwise the same! The PyPortal is&nbsp;our...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4465)
[Related Guides to the Product](https://learn.adafruit.com/products/4465/guides)
### Adafruit PyPortal Desktop Stand Enclosure Kit

[Adafruit PyPortal Desktop Stand Enclosure Kit](https://www.adafruit.com/product/4146)
PyPortal is&nbsp;our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Create little pocket universes of joy that connect to something good.

And now that you've made a cool internet-connected project...

In Stock
[Buy Now](https://www.adafruit.com/product/4146)
[Related Guides to the Product](https://learn.adafruit.com/products/4146/guides)

## Related Guides

- [Adafruit PyPortal - IoT for CircuitPython](https://learn.adafruit.com/adafruit-pyportal.md)
- [Adafruit PyPortal Titano](https://learn.adafruit.com/adafruit-pyportal-titano.md)
- [CircuitPython Turtle Graphics](https://learn.adafruit.com/circuitpython-turtle-graphics.md)
- [Where's My Friend? A Location-Aware Display with PyPortal and ItsASnap](https://learn.adafruit.com/where-s-my-friend-a-location-display-frame-with-pyportal.md)
- [Saving CircuitPython Bitmaps and Screenshots](https://learn.adafruit.com/saving-bitmap-screenshots-in-circuitpython.md)
- [PyPortal Event Count-Up Clock](https://learn.adafruit.com/pyportal-event-count-up-clock.md)
- [Melting Picture Frame for PyPortal IoT images](https://learn.adafruit.com/pyportal-art-display.md)
- [TFT Spirit Board](https://learn.adafruit.com/tft-spirit-board.md)
- [A Floppy Thumb Drive with a Color File Icon Display](https://learn.adafruit.com/a-floppy-thumb-drive-with-a-color-file-icon-display.md)
- [Using Dashblock to Create Custom APIs for PyPortal](https://learn.adafruit.com/using-dashblock-to-create-custom-apis-for-pyportal.md)
- [PyPortal 2FA TOTP Authentication Friend](https://learn.adafruit.com/pyportal-2fa-totp-authentication-friend.md)
- [PyPortal Voice Controlled Smart Switch and Time Display](https://learn.adafruit.com/pyportal-voice-controlled-smart-switch-and-time-display.md)
- [PyPortal Air Quality Display](https://learn.adafruit.com/pyportal-air-quality-display.md)
- [PyPortal Astronauts in Space](https://learn.adafruit.com/pyportal-astronauts-in-space.md)
- [Infinite Text Adventure](https://learn.adafruit.com/infinite-text-adventure.md)
- [CircuitPython Your Own Adventure](https://learn.adafruit.com/circuit-python-your-own-adventure.md)
- [PyPortal IoT Plant Monitor with Google Cloud IoT Core and CircuitPython](https://learn.adafruit.com/pyportal-iot-plant-monitor-with-google-cloud-iot-core-and-circuitpython.md)
