繁体   English   中英

蟒蛇基维。 负值

[英]Python Kivy. Negative values

你好,我有一个问题,我正在用 Python 在 Kivy 库中制作一个游戏。 我想在函数“ on_touch_down ”中反转 self.hero_pos.points 我知道如何通过从 update_hero 复制粘贴坐标并使它们为负数来实现。 但我很好奇我可以用更简单的方式来保持代码干燥吗? :)

def on_touch_down(self, touch):
    #what should i do? 
    

 def on_touch_up(self,touch):
   pass

def init_hero(self):
    with self.canvas:
        Color(0,1,0)
        self.hero_pos = Triangle()

def update_hero(self):
    center_x = int(self.width/2) 
    spacing = self.V_spacing * self.width 
    ymax = self.height * self.hero_height
    ymin = self.height * self.hero_height
    x1, y1 = (center_x - spacing*.25, ymin)
    x2, y2 = (center_x - spacing *.5, ymax + ymin)
    x3, y3 = ( center_x - spacing*.75, ymin)
    
    self.hero_pos.points = [ x1,y1,x2,y2,x3,y3]

再会。 我不确定“反向”是什么意思。

选项1

如果 reverse 表示 '[y3, x3, y2, x2, y1, x1]',那将是一个简单的函数。
def reverse(self):
    n_points = len(self.hero_pos.points)//2
    ans = []
    for r in range(1, n_points, 2):
        ans.append(self.hero_pos.points[r-1])
        ans.append(self.hero_pos.points[r])
    return ans

选项 2

如果反过来你的意思是'[x3,y3, x2,y2, x1,y1]':
 def reverse(self): n_points = len(self.hero_pos.points)//2 ans = [] for r in range(1, n_points, 2): ans.append(self.hero_pos.points[r-1]) ans.append(self.hero_pos.points[r]) return ans

推荐

我强烈建议您将每个 x,y 值作为一对存储在列表(对列表)中。 这将使迭代和重新排序更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM