如何在猕猴桃中创建相对于彼此的多个形状? [英] How do I create multiple shapes relative to each other in kivy?

查看:64
本文介绍了如何在猕猴桃中创建相对于彼此的多个形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在画布上创建多个形状并使它们均匀地间隔开.我还想让形状在到达屏幕末尾时开始新的一行.

I want to be able to create multiple shapes on my canvas and have them evenly spaced out from each other. I would also like to have the shapes start a new line once they reach the end of the screen.

这里有我现在的代码:

def draw_streak(self, obj):
        name = obj.text
        can = self.root.get_screen("three")

        with open("streak.json", "r") as file:
            read = json.load(file)

        for key in read.keys():
            if key == name:
                with open("streak.json", "r+") as f:
                    data = json.load(f)

                get_score = data.get(key, {}).get('score')

                for x in range(get_score):
                    with can.ids.my_box.canvas:
                        Color(0, 1, 0, .75, mode='rgba')
                        Rectangle(pos=can.pos, size=(30,30))

如果get_score是10,那么我想绘制10个矩形.现在,当我运行程序时,只绘制了一个矩形.我相信正在绘制多个,但它们彼此重叠.

if get_score was 10, then I would like 10 rectangles to be drawn. Right now when I run the program only one rectangle is drawn. I believe multiple are being drawn but they just overlap eachother.

编辑

这是kv代码:

<ScreenThree>
    id: screen_three
    name: "three"
    on_leave: my_box.canvas.clear()
    on_leave: selected_streak.canvas.clear()
    ...
        BoxLayout:
            id: my_box
            orientation: "vertical"

my_box只是BoxLayout的ID

my_box is just the id for the BoxLayout

推荐答案

pos[0]pos[1]size[0]size[1]

pos[0], pos[1], size[0], size[1]

rect.size和new_pos中的0和1做什么?

what does the 0 and 1 in rect.size and new_pos do?

Kivy小部件»pos

pos

小部件的位置.

pos 是(<<的 ReferenceListProperty a href ="https://kivy.org/doc/stable/api-kivy.uix.widget.html#kivy.uix.widget.Widget.x" rel ="nofollow noreferrer"> x , y ) 属性.

pos is a ReferenceListProperty of (x, y) properties.

pos包含小部件位置的x和y坐标. pos[0]表示x坐标,pos[1]表示y坐标.

The pos contains the x and y coordinates of a widget's position. pos[0] refers to the x coordinate, and pos[1] refers to the y coordinate.

size

小部件的大小.

尺寸是(<<的 ReferenceListProperty a href ="https://kivy.org/doc/stable/api-kivy.uix.widget.html#kivy.uix.widget.Widget.width" rel ="nofollow noreferrer">宽度,高度) 属性.

size is a ReferenceListProperty of (width, height) properties.

size包含小部件的宽度和高度. size[0]表示宽度,size[1]表示高度.

The size contains the width and height of a widget. size[0] refers to the width and size[1] refers to the height.

为防止矩形重叠,请保存起始位置,并在绘制每个矩形后对其进行递增.

To prevent the rectangles from overlapping, save the starting position and increment it after drawing each rectangle.

以下代码片段在对角线处添加矩形.

The following snippets add rectangles diagonally.

    new_pos = can.pos
    for x in range(get_score):
        with can.ids.my_box.canvas:
            Color(0, 1, 0, .75, mode='rgba')
            rect = Rectangle(pos=new_pos, size=(30, 30))
        new_pos[0] += rect.size[0]
        new_pos[1] += rect.size[1]

输出

这篇关于如何在猕猴桃中创建相对于彼此的多个形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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