使用tkinter python的蛇和梯子

时间:2015-12-05 17:22:31

标签: user-interface python-3.x button tkinter tkinter-canvas

import random
from tkinter import *

class Spinner(object):

@staticmethod
def getSpin():
    newSpin = random.randint(1,6)
    return newSpin

class Player(object):

def __init__(self,name):
    self.position = 1
    self.name = name

def setName(self,name):
    self.name = name

def changePosition(self,number):
    self.position = self.position + number

def setPosition(self,pos):
    self.position = pos
    return self.position

def getPosition(self):
    return self.position

def getName(self):
    return self.name

def spin(self):
    newSpin = Spinner.getSpin()
    self.position = self.position + newSpin
    print(str(self.name) + "'s spin was: " + str(newSpin))

class Path(object):
      @staticmethod    
      def buildLadders():
    ladders = [[0 for x in range(2)] for x in range(9)]
    ladders[0][0] = 2
    ladders[0][1] = 9
    ladders[1][0] = 8
    ladders[1][1] = 11
    return ladders

@staticmethod
def buildChutes():
    chutes = [[0 for x in range(2)] for x in range(10)]
    chutes[0][0] = 9
    chutes[0][1] = 3
    chutes[1][0] = 12
    chutes[1][1] = 6
    return chutes

class Check(Player):
    def __init__(self):
    super(Check,self).__init__()

def checkLadders(self):
    ladders = Path.buildLadders()
    for i in range(0,len(ladders),1):
        if self.getPosition() == ladders[i][0]:
            self.position = self.setPosition(ladders[i][1])
            print(str(self.name) + " Landed on a Ladder! from " + \
                  str(ladders[i][0]) +" to " + str(ladders[i][1]))

def newPosition(self):
    return self.position

def checkChutes(self):
    chutes = Path.buildChutes()
    for i in range(0,len(chutes),1):
        if self.getPosition() == chutes[i][0]:
            self.position = self.setPosition(chutes[i][1])
            print(str(self.name) + " Landed on a Chutes!")

class Match_Position(Player):
def __init__(self,name):
    super(Match_Position,self).__init__(name)
    self.match = [[70,235],
                [180,235],
                [290,235],
                [400, 235],
                [400, 140],
                [290, 140],
                [180, 140],
                [70, 140],
                [70, 45],
                [180, 45],
                [290, 45],
                [400, 45]]
    self.name = name
    self.players = Player(self.name)
    self.pos = self.players.getPosition()
    self.position_actual = []
    self.__str__()
    self.actualpos()

def __str__(self):
    for j in range(len(self.match)):
        if self.pos == (j+1):
            self.position_actual.append(self.match[j][0])
            self.position_actual.append(self.match[j][1])

def actualpos(self):
    return self.position_actual

class Display(object):
def __init__(self,master,img,name):
    canvas_width = 650
    canvas_height = 300
    self.name = name
    print(self.name)
    self.pos = Match_Position(self.name).actualpos()
    print(self.pos)
    self.canvas = Canvas(master, width = canvas_width, height = canvas_height, bg = "yellow")
    self.canvas.grid(padx=0, pady=0)
    self.canvas.create_image(300,150,anchor=CENTER, image = img)
    self.animate(master)

def animate(self,master):
    Button(master, text= "ROLL", command=self.say_hello(self.name[0])).grid( row=3, column=0, sticky=E)
    Button(master, text= "ROLL", command=self.say_hello1(self.name[1])).grid( row=3, column=1, sticky=E)

def say_hello(self,name):
    self.name = name
    self.name = Player(self.name)
    self.name.spin()
    Check.checkLadders(self.name)
    Check.checkChutes(self.name)   
    x = self.pos[0]
    y = self.pos[1]
    self.canvas.create_oval(x,y,x+20,y+20, fill='blue')

def say_hello1(self,name):
    self.name = name
    self.name = Player(self.name)
    self.name.spin()
    Check.checkLadders(self.name)
    Check.checkChutes(self.name)   
    x = self.pos[0]
    y = self.pos[1]
    self.canvas.create_oval(x,y,x+20,y+20, fill='red')



class BounceController(object):
def __init__(self):
    master = Tk()
    master.title("Snake and Ladder")
    master.geometry("700x350")
    img = PhotoImage( file = "puzzlor-chutes-and-ladders.gif" )
    name = ['n','s']
    Display(master,img,name).animate(master)
    master.mainloop()

def main():
BounceController()

main()

enter image description here

打印出来了,但是错误: “'播放器'对象不支持索引”弹出。 什么是对象不支持索引错误?

当我点击按钮时,椭圆实际上并没有移动。 使用tkinter Button,我可以调用该方法,对吗?

但是通过这样做,如果我想做例如,点击按钮,结果椭圆移动到不同的位置,我该如何实现?因为上面的代码,只要点击按钮,椭圆就不会移动。

感谢。由于我对python和编程很陌生,所以任何帮助都会如此感激。

1 个答案:

答案 0 :(得分:1)

索引是指对[n]后缀的对象,其中n为int。以下是重现该消息的方法。

>>> class C: pass

>>> C()[1]
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    C()[1]
TypeError: 'C' object does not support indexing

错误消息上方的文件条目会告诉您给出错误的行号和表达式。代码中的某个位置,如您未向我们展示的回溯所示,您有x [n],其中x是您定义的Player类的实例。

对象是可订阅的,当且仅当它具有__getitem__方法时Player没有且不应该有。