課程視頻:
https://edu.csdn.net/course/detail/28882
遊戲效果圖:
代碼:
from tkinter import *
import time
import tkinter.messagebox
from ClspositionUpOrDown import ClspositionUpOrDown
from Obliquedirection import Obliquedirections
# 畫布
# 棋盤
# 鼠標左鍵綁定事件
# 落下棋子後位置記錄下來。保證每個棋子能夠落在準確的位置上。
# 交替變換顏色,記錄位置
# 水平向右方向的輸贏判斷
# 水平方向向左完成判斷
# 向下方向完成判斷
# 斜向方向完成判斷
# 遊戲結束,一方獲勝的判斷
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):
if self.clsposition.close==1:
tkinter.messagebox.showinfo("提示","遊戲已結束!")
return
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=self.clsposition.colorx)
self.ball = Ball()
# self.clsposition.positions[i][x] = 1
# downy = x
# downx = i
# self.ball.down(id1, self.canvas, self.tk,i,x)
if (self.clsposition.colorx == '#389aff'):
self.clsposition.colorx = 'red'
self.clsposition.positions[i][x] = 1
else:
#self.clsposition.colorx = 'blue'
self.clsposition.colorx = '#389aff'
self.clsposition.positions[i][x] = 2
self.ball.down(id1, self.canvas, self.tk, i, x, self.clsposition)
if i + 1 <= 4:
a = self.clsposition.positions[i + 1][x]
# print(a)
break
# self.ball.down(id1, self.canvas, self.tk)
class Ball:
def __init__(self):
self.y = 0
self.y0 = 10
# self.clsposition=Clsposition()
self.clspositionupordown = ClspositionUpOrDown() #上下
self.oblique=Obliquedirections() #斜向的判斷
def down(self, id1, canvas, tk, ii, x, clsposition): # 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
# 水平方向輸贏的判斷
a0=clsposition.winornotrightleft(ii, x)
# 垂直方向輸贏的判斷
a1= self.clspositionupordown.winornotupdown(ii, x,clsposition.positions)
#斜向的判斷
a2=self.oblique.Obliquedirection(ii,x,clsposition.positions)
a3=self.oblique.Obliquedirection2(ii,x,clsposition.positions)
if a0==444 or a1==555 or a2==666 or a3==777:
clsposition.close=1
return tkinter.messagebox.showinfo("提示",clsposition.colorx+"方輸了!對手勝了")
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]]
self.colorx = "#389aff" #淡藍色
self.close = 0
def winornotrightleft(self, i, b):
self.count = 1
# 水平向右
for x in range(3):
# print(self.count)
if i + x + 1 < 5:
print(x)
print(self.positions[i + x + 1][b])
# print(self.positions[i + x ][b])
# print(i+x+1)
if i + x + 1 > 4 or self.positions[i + x + 1][b] == 0:
break
if (self.positions[i + x][b] == self.positions[i + x + 1][b]):
self.count += 1
# print(x)
else:
break
# 水平向左
for x in range(3):
# print(self.count)
# print(self.positions[i + x ][b])
# print(i+x+1)
if i- x-1 <0 or self.positions[i - x - 1][b] == 0:
break
if (self.positions[i - x][b] == self.positions[i - x - 1][b]):
self.count += 1
# print(x)
else:
break
if (self.count >= 4):
# tkinter.messagebox.showinfo('提示', "成功!")
#tkinter.messagebox.showinfo('提示', "水平方向成功!")
self.close=1 #遊戲結束
return 444
if __name__ == "__main__":
ball = Ball()
game = Game()
game.canvas.bind('<Button-1>', game.action)
dlines = Dlines()
dlines.drawline(game.canvas)
game.tk.mainloop()
ClspositionUpOrDown.py
#向下方向的判斷。考慮到球向下落,所以沒有向上判斷的必要
import tkinter.messagebox
class ClspositionUpOrDown:
def winornotupdown(self, i, b,positions):
self.count = 1
# 向下
for x in range(3):
# print(self.count)
# print(positions[i ][b+x+1])
# print(self.positions[i + x ][b])
# print(i+x+1)
if b + x + 1 > 4 or positions[i][b+x+1] == 0:
break
if (positions[i][b+x] == positions[i][b+x+1]):
self.count += 1
# print(x)
else:
break
print(self.count)
if (self.count >= 4):
# tkinter.messagebox.showinfo('提示', "成功!")
#tkinter.messagebox.showinfo('提示', "垂直方向成功!")
return 555
Obliquedirection
import tkinter.messagebox
class Obliquedirections:
def Obliquedirection(self, a, b,positions):
self.count = 1
# 右斜下方
for x in range(3):
# print(x)
# 處理邊界
if (b + x + 1 > 4):
break
if (a + x + 1 > 4):
break
# print(a+x+1,b+x+1)
if (positions[a + x][b + x] == positions[a + x + 1][b + x + 1]):
self.count += 1
else:
break
# 左斜上方
for x in range(3):
# print(x)
# 處理邊界
if (b - x - 1 < 0):
break
if (a - x - 1 < 0):
break
if (positions[a - x][b - x] == positions[a - x - 1][b - x - 1]):
self.count += 1
# print(a,b)
else:
break
print(self.count)
if (self.count >= 4):
isover = 1
#tkinter.messagebox.showinfo('提示', "斜向一成功!")
return 666
def Obliquedirection2(self, a, b,positions):
self.count = 1
# 左斜下方
for x in range(3):
# print(x)
# 處理邊界
if (b + x + 1 > 4):
break
if (a - x - 1 < 0):
break
if (positions[a - x][b + x] == positions[a - x - 1][b + x + 1]):
self.count += 1
# print(x)
else:
break
# 右斜上方
for x in range(3):
if (a + x + 1 > 4):
break
if (b - x - 1 < 0):
break
if (positions[a + x][b - x] == positions[a + x + 1][b - x - 1]):
self.count += 1
# print(x)
else:
break
print(self.count)
if (self.count >= 4):
#tkinter.messagebox.showinfo('提示', "斜向二成功!")
return 777