本文主要是介绍【四二学堂】python四子连珠游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
课程视频:
https://edu.csdn.net/course/detail/28882
游戏效果图:
代码:
from tkinter import *
import time
import tkinter.messageboxfrom ClspositionUpOrDown import ClspositionUpOrDown
from Obliquedirection import Obliquedirections# 画布
# 棋盘
# 鼠标左键绑定事件
# 落下棋子后位置记录下来。保证每个棋子能够落在准确的位置上。
# 交替变换颜色,记录位置
# 水平向右方向的输赢判断
# 水平方向向左完成判断
# 向下方向完成判断
# 斜向方向完成判断
# 游戏结束,一方获胜的判断class Game:def __init__(self):# self.ball=ballself.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("提示","游戏已结束!")returnfor 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] = 1else:#self.clsposition.colorx = 'blue'self.clsposition.colorx = '#389aff'self.clsposition.positions[i][x] = 2self.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 = 0self.y0 = 10# self.clsposition=Clsposition()self.clspositionupordown = ClspositionUpOrDown() #上下self.oblique=Obliquedirections() #斜向的判断def down(self, id1, canvas, tk, ii, x, clsposition): # i,x代表位置self.canvas = canvasself.tk = tkfor i in range(0, 200):self.canvas.move(id1, 0, 10) # 10像素纵向下降self.y = self.y + 10if (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=1return tkinter.messagebox.showinfo("提示",clsposition.colorx+"方输了!对手胜了")breakself.tk.update()time.sleep(0.1)class Dlines:def drawline(self, canvas):self.canvas = canvasa = [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 = 0def 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:breakif (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:breakif (self.positions[i - x][b] == self.positions[i - x - 1][b]):self.count += 1# print(x)else:breakif (self.count >= 4):# tkinter.messagebox.showinfo('提示', "成功!")#tkinter.messagebox.showinfo('提示', "水平方向成功!")self.close=1 #游戏结束return 444if __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.messageboxclass 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:breakif (positions[i][b+x] == positions[i][b+x+1]):self.count += 1# print(x)else:breakprint(self.count)if (self.count >= 4):# tkinter.messagebox.showinfo('提示', "成功!")#tkinter.messagebox.showinfo('提示', "垂直方向成功!")return 555
Obliquedirection
import tkinter.messageboxclass Obliquedirections:def Obliquedirection(self, a, b,positions):self.count = 1# 右斜下方for x in range(3):# print(x)# 处理边界if (b + x + 1 > 4):breakif (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 += 1else:break# 左斜上方for x in range(3):# print(x)# 处理边界if (b - x - 1 < 0):breakif (a - x - 1 < 0):breakif (positions[a - x][b - x] == positions[a - x - 1][b - x - 1]):self.count += 1# print(a,b)else:breakprint(self.count)if (self.count >= 4):isover = 1#tkinter.messagebox.showinfo('提示', "斜向一成功!")return 666def Obliquedirection2(self, a, b,positions):self.count = 1# 左斜下方for x in range(3):# print(x)# 处理边界if (b + x + 1 > 4):breakif (a - x - 1 < 0):breakif (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):breakif (b - x - 1 < 0):breakif (positions[a + x][b - x] == positions[a + x + 1][b - x - 1]):self.count += 1# print(x)else:breakprint(self.count)if (self.count >= 4):#tkinter.messagebox.showinfo('提示', "斜向二成功!")return 777
这篇关于【四二学堂】python四子连珠游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!