Python 大作业——五子棋游戏
姓名:      学号:
姓名:      学号:
游戏介绍:
    我们设计的是五子棋游戏,支持两人一个鼠标对下,黑方用左键单击,白方用右键单击,谁先下均可,落子无悔,下过的棋子对方点击后不会变,程序可自行判断输赢并在五子连珠时弹出结果对话框,游戏双方需遵守不在空地点击和一次下一子的规则。
游戏代码设计:
    代码均为原创,没有借鉴和抄袭,首先是用户GUI界面设计,点击start进入游戏界面,点击quit则退出程序,为了方便判断和记录,我们按从左到右,从上到下的顺序给15x15=225颗棋子编号225,左键绑定函数callback1,点击后可算出它位于哪颗棋子上再画出来黑子,并把对应编号计入record这个列表,之后进入判断函数。右键绑定函数 callback2,点击后画出白子,
对应编号计入recor这个列表,之后进入判断函数,其中总列表rec的作用是使棋子不被下第二遍。
作业感想
    这个游戏虽然很小但是可以供室友们晚上娱乐之用,我们倾注了很多心血,之前采用模块化编程失败了很多次,有事件响应问题,参数传递问题,到第七个程序才成功,感谢张同珍老师指点了很多,我们学会了使用类,受益匪浅,对Python产生了浓厚的兴趣。
过程截图
五、实验代码
from Tkinter import *
from tkMessageBox import *
class Game:
    def __init__(self):
        self.A=[]
        self.B=[]
        d=set()
        =set()
        =
        =Tk()
        ry("180x250")
        itle("Wu Zi Qi Game")
        self.r=,width=180,height=210,bg="purple")
        pic=PhotoImage(file="beijing.gif")
        ate_image(90,100,image=pic)
        place(x=0,y=15)
        ,text="***Wu Zi Qi Game***",fg="red").place(x=20,y=0)
        ,text="start",command=self.start).place(x=30,y=230)
        ,text="quit ",destroy).place(x=100,y=230)
        ainloop()
    def start(self):
        destroy()
        p=Tk()
        p.title("Game Start")
        self.c=p,width=480,height=480,bg="white")
        pack()
        ate_rectangle(25,25,455,455,fill="gray")
        for i in range(30,451,30):
            for j in range(30,451,30):
                ate_oval(i-2,j-2,i+2,j+2,fill="blue")
        for i in range(1,16):
            ate_line(30,30*i,450,30*i)
            ate_line(30*i,30,30*i,450)
        ate_oval(234,234,246,246,fill="black")
        ate_oval(115,115,125,125,fill="black")
        ate_oval(355,115,365,125,fill="black")
        ate_oval(115,355,125,365,fill="black")
        ate_oval(355,355,365,365,fill="black")
        bind("<Button-1>",self.callback1)
        bind("<Button-3>",self.callback2)
        ainloop()   
   
    def callback1(self,event):
免费游戏代码大全        u,v=event.x,event.y
       
        s=u/15
        if s%2==1:
            self.x=(s+1)/2
        else:
            self.x=s/2
        l=v/15
        if l%2==1:
            self.y=(l+1)/2
        else:
            self.y=l/2
        g=(self.y-1)*15+self.x
        while g not :
            ate_oval(self.x*30-12,self.y*30-12,self.x*30+12,self.y*30+12,fill="black")
            self.A.append(g)
            d=set(self.A)
            =
            judge=panduan(d)
            if judge==1:
                answer=showinfo("Game over","Black wins!")
                p.destroy()
               
    def callback2(self,event):
        u,v=event.x,event.y
       
        s=u/15
        if s%2==1:
            self.m=(s+1)/2
        else:
            self.m=s/2
        l=v/15
        if l%2==1:
            self.n=(l+1)/2
        else:
            self.n=l/2
       
        k=(self.n-1)*15+self.m
        while k not :
            ate_oval(self.m*30-12,self.n*30-12,self.m*30+12,self.n*30+12,fill="white")
            self.B.append(k)
            =set(self.B)
            =
            judge=panduan()
            if judge==1:
                answer=showinfo("Game over","White wins!")
                p.destroy()
               
         
def panduan(g,record):
    #判断横排是否出现赢的情况
    if {g-4,g-3,g-2,g-1}<=record:
        return 1
    elif {g-3,g-2,g-1,g+1}<=record:
        return 1
    elif {g-2,g-1,g+1,g+2}<=record:
        return 1       
    elif {g-1,g+1,g+2,g+3}<=record:
        return 1       
    elif {g+1,g+2,g+3,g+4}<=record:
        return 1     
    #判断竖列是否出现赢的情况
    elif {g-60,g-45,g-30,g-15}<=record:
        return 1       
    elif {g-45,g-30,g-15,g+15}<=record:
        return 1       
    elif {g-30,g-15,g+15,g+30}<=record:
        return 1