2017-06-12 145 views
2

私はボタンウィジェットを使用してKivyでMinesweeperのゲームを作っています。私は、マウスのクリックがマウスの左クリックか右のマウスクリックかによって、異なるアクションを起こしたいと思っています。誰か助けてくれますか?Kivy Python右クリック

以下は、私のCellクラスとインポートされたモジュールです。

Cell.onPressed()は、ボタンが押されたときに呼び出される関数です。

import kivy 
from kivy.config import Config 

Config.set('input', 'mouse', 'mouse,disable_multitouch') 

from random import randint 
from kivy.app import App 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.relativelayout import RelativeLayout 
from kivy.uix.anchorlayout import AnchorLayout 

width = 10 
height = 10 
bombs = 5 
size = 60 

class Cell(): 

    def __init__(self): 
    self.isBomb = False 
    self.isVisible = False 
    self.neighbors = None 
    self.location = [] 
    self.button = Button(size = (size, size), size_hint = (None, None)) 
    self.button.bind(on_press = self.onPressed) 

    def build(self, x, y): 
    self.location = [x, y] 
    self.count_neighbors() 

    def onPressed(self, instance): 
    #if left_click == True:  
    self.isVisible = True 
    self.button.text = str(self.neighbors) 
    if self.neighbors == 0: 
     for i in range(-1, 2): 
     for j in range(-1, 2): 
      if (0 <= self.location[0] + i < width) and (0 <= self.location[1] + j < height): 
      if grid[self.location[0] + i][self.location[1] + j].isVisible == False: 
       grid[self.location[0] + i][self.location[1] + j].onPressed(instance)  
    #if right_click == True: 
     #Toggle state 


    def count_neighbors(self): 
    if self.isBomb == False: 
     count = 0 
     for i in range(-1, 2): 
     for j in range(-1, 2): 
      if (0 <= self.location[0] + i < width) and (0 <= self.location[1] + j < height): 
      if grid[self.location[0] + i][self.location[1] + j].isBomb == True: 
       count += 1 
     self.neighbors = count 

class TestApp(App): 

    def build(self): 
    root = AnchorLayout(anchor_x = 'center', anchor_y = 'center') 
    grid_root = RelativeLayout(size = (width * size, height * size), size_hint = (None, None)) 
    layout = [] 
    for i in range(height): 
     layout.append(BoxLayout(orientation='horizontal', size_hint = (.8, .8), pos = (0, (height - 1) * size - i * size))) 
     for j in range(width):  
     layout[i].add_widget(grid[j][i].button)  
     grid_root.add_widget(layout[i]) 
    root.add_widget(grid_root) 
    return root 


def init_grid(): 
    global grid 
    grid = [[Cell() for x in range(width)] for y in range(height)] 
    for _ in range(bombs): 
    while True: 
     x = randint(0, height - 1) 
     y = randint(0, width - 1) 
     if grid[x][y].isBomb == False: 
     grid[x][y].isBomb = True 
     break 
    for i in range(width): 
    for j in range(height): 
     grid[j][i].build(j, i) 


if __name__ == '__main__': 
    init_grid() 
    TestApp().run() 

答えて

3

代わりon_presson_touch_downはそうあなたが利用可能touchパラメータを持つことができますバインドする必要があります:私は何かの作業を得た

... 
self.button.bind(on_touch_down = self.onPressed) 
... 

def onPressed(self, instance, touch): 
    if touch.button == 'right': 
     print("right mouse clicked") 
    ... 
+0

私はプログラム全体を含むように編集しました。あなたの答えは私にとってはうまくいかなかった、私は「あまりにも多くの引数」エラーまたは「タッチが定義されていません」を取得します。 どのモジュールをインポートする必要があり、どのように実装するのがベストですか? –

+0

申し訳ありませんが、編集エラーです。すぐに更新するon_press'の代わりにbindの 'on_touch_down'にする必要があります。 2番目の例をもう一度試してください。 – hurturk

+0

ボタンが単に左クリックか右クリックかを検出できない理由はわかりません。私が読んでいるところでは、kivyのウィジェットモジュールを使用する必要があり、それが記述されていました。 "Widget.on_touch_down()、Widget.on_touch_move()、Widget.on_touch_up()は衝突を一切しません。あなたのウィジェット内にタッチがあるかどうかを知りたい場合は、Widget.collide_point()を使用してください。 " 私は基本的に私のプログラム全体を再考する必要があります。 –

0

、それはしかし少しハックです。 私はウィジェットから継承する新しいクラスを作成し、単に「マウス」

class TouchInput(Widget): 

    def on_touch_down(self, touch): 
    global mouse 
    mouse = touch.button 

私はTOUCHINPUTのインスタンスを作成した()と呼ばれるグローバル変数を変更し、

class TestApp(App): 

    def build(self): 
    root = AnchorLayout(anchor_x = 'center', anchor_y = 'center') 
    root_input = TouchInput() 
    grid_root = RelativeLayout(size = (width * size, height * size), size_hint = (None, None)) 
    layout = [] 
    for i in range(height): 
     layout.append(BoxLayout(orientation='horizontal', size_hint = (.8, .8), pos = (0, (height - 1) * size - i * size))) 
     for j in range(width):  
     layout[i].add_widget(grid[j][i].button)  
     grid_root.add_widget(layout[i]) 
    root.add_widget(grid_root) 
    root.add_widget(root_input) 
    return(root) 

今私のルートレイアウトにこれを追加しましたボタンが押されるたびに、それが右クリックか左クリックかをチェックすることができます。

def onPressed(self, instance): 
    if mouse == 'left': 
    print('Left!') 
    if mouse == 'right': 
    print('Right!')