Python五子棋的小程序
2015-06-02 16:28
579 查看
代码是在开源中国上看到的,源代码网址:http://www.oschina.net/code/snippet_2365439_48010
需要安装graphics模块,下载地址:http://mcsp.wartburg.edu/zelle/python/graphics.py 保存到C:\Python27\Lib\site-packages 路径中的版本号改成你用的
from graphics import * from math import * def gobangwin(): win=GraphWin("this is a gobang game",400,400) #ÖÆ×÷21x21µÄÆåÅÌ win.setBackground("yellow") i1=0 while i1<401: l=Line(Point(i1,0),Point(i1,400)) l.draw(win) i1=i1+20 i2=0 while i2<401: l=Line(Point(0,i2),Point(400,i2)) l.draw(win) i2=i2+20 return win def main(): win = gobangwin() list1 = [] list2 = [] list3 = [] change = 0 g = 0 m=0 n=0 while g == 0: if change%2 == 1: p1 = win.getMouse() if not ((round((p1.getX()+10)/20),round((p1.getY()+10)/20)) in list3): a1 = round((p1.getX()+10)/20) b1 = round((p1.getY()+10)/20) list1.append((a1,b1)) list3.append((a1,b1)) piece = Circle(Point(20*a1,20*b1),8) #´´½¨Æå×Ó piece.setFill('white') piece.draw(win) for m in range(21): #ÅжÏÊäÓ® for n in range(21): if n<17 and (m,n) in list1 and (m,n+1) in list1 and (m,n+2) in list1 and (m,n+3) in list1 and (m,n+4) in list1 : message = Text(Point(100,100),"white win.") message.draw(win) g = 1 #Åжϰ×ÆåÊúÐÐ elif m<17 and (m,n) in list1 and (m+1,n) in list1 and (m+2,n) in list1 and (m+3,n) in list1 and (m+4,n) in list1 : message = Text(Point(100,100),"white win.") message.draw(win) g = 1 #Åжϰ×ÆåºáÐÐ elif m<17 and n<17 and (m,n) in list1 and (m+1,n+1) in list1 and (m+2,n+2) in list1 and (m+3,n+3) in list1 and (m+4,n+4) in list1 : message = Text(Point(100,100),"white win.") message.draw(win) g = 1 #Åжϰ×ÆåбÐÐ elif m<17 and n>3 and (m,n) in list1 and (m+1,n-1) in list1 and (m+2,n-2) in list1 and (m+3,n-3) in list1 and (m+4,n-4) in list1 : message = Text(Point(100,100),"white win.") message.draw(win) g = 1 #Åжϰ×ÆåбÐÐ else: change = change+1 #»»ºÚÆå×ß else: p2 = win.getMouse() if not ((round((p2.getX()+10)/20),round((p2.getY()+10)/20)) in list3): a2 = round((p2.getX()+10)/20) b2 = round((p2.getY()+10)/20) list2.append((a2,b2)) list3.append((a2,b2)) piece = Circle(Point(20*a2,20*b2),8) piece.setFill('black') piece.draw(win) for m in range(21): for n in range(21): if n<17 and (m,n) in list2 and (m,n+1) in list2 and (m,n+2) in list2 and (m,n+3) in list2 and (m,n+4) in list2 : message = Text(Point(100,100),"black win.") message.draw(win) g = 1 #ÅжϺÚÆåÊúÐÐ elif m<17 and (m,n) in list2 and (m+1,n) in list2 and (m+2,n) in list2 and (m+3,n) in list2 and (m+4,n) in list2 : message = Text(Point(100,100),"black win.") message.draw(win) g = 1 #ÅжϺÚÆåºáÐÐ elif m<17 and n<17 and (m,n) in list2 and (m+1,n+1) in list2 and (m+2,n+2) in list2 and (m+3,n+3) in list2 and (m+4,n+4) in list2 : message = Text(Point(100,100),"black win.") message.draw(win) g = 1 #ÅжϺÚÆåбÐÐ elif m<17 and n>3 and (m,n) in list2 and (m+1,n-1) in list2 and (m+2,n-2) in list2 and (m+3,n-3) in list2 and (m+4,n-4) in list2 : message = Text(Point(100,100),"black win.") message.draw(win) g = 1 #ÅжϺÚÆåбÐÐ else: change = change+1 #»»°×Æå×ß message = Text(Point(100,120),"Click anywhere to quit.") message.draw(win) win.getMouse() win.close() main()
相关文章推荐
- python使用xmlrpclib模块实现对百度google的ping功能
- Python基础:12函数细节
- python基于xmlrpc实现二进制文件传输的方法
- 科研项目之经验之谈
- Python中的自省(反射)详解
- Python基础:11变量作用域和闭包
- 简明python教程:第3章 最初的步骤
- Python Tkinter Grid简明彻底理解
- python-根据字符串动态生成对象eval
- Python的dict.fromkeys应用
- Python中pip安装非PyPI官网第三方库的方法
- python 基础学习 正则表达式1(规则)
- python __name__的含义
- Python中字典创建、遍历、添加等实用操作技巧合集
- python greenlet
- 简明python教程:第2章 安装Python
- 关于python的threading.Thread.daemon 与 multiprocessing.Process.daemon的疑问
- Python:itertools模块
- python itertools的使用
- 详细记录python的range()函数用法