代码:
from tkinter import*
import time
#画布
#棋盘
#鼠标左键绑定事件
#落下棋子后位置记录下来。保证每个棋子能够落在准确的位置上。
class Game:
def __init__(self):
#self.ball=ball
self.clsposition = Clsposition()
self.tk = Tk()
self.tk.title("四二学堂Python四子连珠游戏")
self.tk.resizable(0, 0) # 窗口在水平、垂直方向的大小都不能改变
# tk.wm_attributes("-topmost",1) #画布窗口放在其他所有窗口之前
self.canvas = Canvas(self.tk, width=700, height=700)
# anvas = Canvas(tk,width=700,height=700,bd=0,highlightthickness=0)
# bd=0 highlightthickness=0 确保在画布之外没有边框,看上去更美观些
self.canvas.pack()
def action(self,event):
for i in range(0, 5):
if (event.x >= 100 + i * 100 and event.x < 100 + i * 100 + 100):
for x in range(4, -1, -1):
print(self.clsposition.positions[i][x])
if (self.clsposition.positions[i][x] == 0):
id1 = self.canvas.create_oval(100 + i * 100, 40, 100 + i * 100 + 100, 140, fill="red")
self.ball = Ball()
self.clsposition.positions[i][x] = 1
#downy = x
#downx = i
self.ball.down(id1, self.canvas, self.tk,i,x)
break
#self.ball.down(id1, self.canvas, self.tk)
class Ball:
def __init__(self):
self.y=0
self.y0=10
def down(self,id1,canvas,tk,i,x):
self.canvas=canvas
self.tk=tk
for i in range(0, 200):
self.canvas.move(id1, 0, 10) # 10像素纵向下降
self.y=self.y+10
if(self.y>x*100+50):
self.y0=0
break
self.tk.update()
time.sleep(0.1)
class Dlines:
def drawline(self, canvas):
self.canvas = canvas
a = [100, 200, 300, 400, 500, 600]
for ii in a:
self.canvas.create_line(100, ii, 600, ii)
self.canvas.create_line(ii, 100, ii, 600)
class Clsposition:
def __init__(self):
self.positions = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
'''
class ball:
def down(self):
'''
if __name__=="__main__":
ball=Ball()
game=Game()
game.canvas.bind('<Button-1>', game.action)
dlines = Dlines()
dlines.drawline(game.canvas)
game.tk.mainloop()