哪种方法可以在图形化python应用程序中制作Cozmo的表达式? [英] Which approach for making Cozmo's expressions in a graphical python application?

查看:88
本文介绍了哪种方法可以在图形化python应用程序中制作Cozmo的表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个想法,我想给机器人眼睛动画,类似于Anki的Cozmo,但我不想在LED屏幕上显示,而是想在python应用程序中运行它,相似的感觉/效果(轻微闪烁,看起来像素化)。这里的问题是我不知道从哪里开始。我必须拥有所有表达式(悲伤,快乐,愤怒...)和过渡的所有动画精灵,然后使用tkinter或pygame。或者我是否可以直接从一组配置(例如数组)中绘制?

I have this idea, in which I'd like to animate a pair of robot eyes, similar to Anki's Cozmo, but instead of displaying on a LED screen, I would like to run this in a python application, while still have a similar feel/effect (slight flickering, look pixelated). The problem here is that I have not a clue where to start. Must I have all the animation sprites for all the expressions (sad, happy, angry...) and transitions, then using tkinter or pygame. Or can I just draw directly from a set of configuration (e.g. array)? It would be great if you can suggest some approaches.

推荐答案

一种简单的方法是将数据存储在一个简单的字符串中,然后然后根据这些字符串创建曲面。它不需要太多内存,而且易于阅读和修改,并且可以存在于您的代码中。

An easy way is to store the data in a simple string, and then create your surfaces from those strings. It does not need much memory while being easy to read and modify and it can live inside your code.

当然,有很多不同的方法可以执行所需的操作,但我想像这样的简单操作将是一个不错的开始(您会明白的):

Of course there are a lot of different ways to do what you want, but I guess something simple like this would be a good start (you'll get the idea):

import pygame
from itertools import cycle

TILE_SIZE = 32
SCREEN_SIZE = pygame.Rect((0, 0, 21*TILE_SIZE, 8*TILE_SIZE))


class Expression(pygame.sprite.Sprite):
    def __init__(self, data):
        super().__init__()
        self.image = pygame.Surface((len(data[0]), len(data)))
        x = y = 0
        for row in data:
            for col in row:
                if col == "O":
                    self.image.set_at((x, y), pygame.Color('dodgerblue'))
                x += 1
            y += 1
            x = 0
        self.image = pygame.transform.scale(self.image, (TILE_SIZE*len(data[0]), TILE_SIZE*len(data)))
        self.rect = self.image.get_rect()

REGULAR = Expression([
"                     ",
"                     ",
"    OOOO     OOOO    ",
"   OOOOOO   OOOOOO   ",
"   OOOOOO   OOOOOO   ",
"    OOOO     OOOO    ",
"                     ",
"                     ",
])

QUESTION = Expression([
"                     ",
"                     ",
"    OOOO             ",
"   OOOOOO    OOOO    ",
"   OOOOOO   OOOOOO   ",
"    OOOO     OOOO    ",
"                     ",
"                     ",
])

SAD = Expression([
"                     ",
"                     ",
"                     ",
"                     ",
"                     ",
"   OOOOOO   OOOOOO   ",
"                     ",
"                     ",
])

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE.size)
    timer = pygame.time.Clock()
    expressions = cycle([REGULAR, QUESTION, REGULAR, QUESTION, SAD])
    current = next(expressions)
    pygame.time.set_timer(pygame.USEREVENT, 1000)

    while True:

        for e in pygame.event.get():
            if e.type == pygame.QUIT: 
                return
            if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
                return
            if e.type == pygame.USEREVENT:
                current = next(expressions)

        screen.fill((30, 30, 30))
        screen.blit(current.image, current.rect)
        timer.tick(60)
        pygame.display.update()

if __name__ == "__main__":
    main()

去哪里此处:


  • 使用较小的切片大小和较大的字符串以获得更好的分辨率。

  • Use a smaller tile size and bigger strings to get a better resolution.

或在程序外部(在绘图工具中)为动画创建图像并使用所需的图像。

Or create the images for your animation outside of your program (in a drawing tool) and use these of you want.

创建定义不同动画的表达式的不同列表,并为每个心情更改当前动画。

Create different lists of expressions that define different animations, and change the current animation for each "mood".

这篇关于哪种方法可以在图形化python应用程序中制作Cozmo的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆