【摸鱼神器】— 五子棋
这个点快到下班或者休息吃饭的时间了吧,不知道现在有没有人,我来分享一个摸鱼神器 — 五子棋 。
用python写出来的!明天上班的时候就能拿出来摸摸鱼 ~
但是还是要注意时间哈,被老板发现了可不能怪我哦
实现效果
就这样,轻轻松松的赢了,是我太厉害了
代码展示
python学习Q裙:770699889##3
import pygame
import time
import computer
from button import Buttonimg_chess_white = pygame.image.load('imgs/white.gif')
img_chess_black = pygame.image.load('imgs/black.gif')
img_btn_restart = ''SCREEN_WIDTH=900
SCREEN_HEIGHT=800
BG_COLOR=pygame.Color(200, 200, 200)
Line_COLOR=pygame.Color(255, 255, 200)
TEXT_COLOR=pygame.Color(255, 0, 0)
# 定义颜色
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)class MainGame():window = NoneStart_X = 50Start_Y = 50Line_Span = 40Max_X = Start_X + 18 * Line_SpanMax_Y = Start_Y + 18 * Line_Spanplayer1Color = 'B'player2Color = 'W'overColor = 'S'# 1代表玩家1 , 2代表到玩家2 0代表结束Putdownflag = player1ColorChessmanList = []button_go =Nonedef __init__(self):'''初始化'''def startGame(self):MainGame.window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])pygame.display.set_caption("五子棋")MainGame.button_go = Button(MainGame.window, "重新开始", SCREEN_WIDTH - 100, 300) # 创建开始按钮#初始化while True:time.sleep(0.1)#获取事件MainGame.window.fill(BG_COLOR)self.drawchChessboard()self.bitechessman()MainGame.button_go.draw_button()self.VictoryOrDefeat()self.Computerplay()self.getEvent()pygame.display.update()pygame.display.flip()def drawchChessboard(self):for i in range(0,19):x = MainGame.Start_X + i * MainGame.Line_Spany = MainGame.Start_Y + i * MainGame.Line_Spanpygame.draw.line(MainGame.window, BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], 1)pygame.draw.line(MainGame.window, BLACK, [MainGame.Start_X, y], [MainGame.Max_X, y], 1)def getEvent(self):# 获取所有的事件eventList = pygame.event.get()for event in eventList:if event.type == pygame.QUIT:self.endGame()elif event.type == pygame.MOUSEBUTTONDOWN:pos = pygame.mouse.get_pos()mouse_x = pos[0]mouse_y = pos[1]if (mouse_x > MainGame.Start_X- MainGame.Line_Span/2 and mouse_x < MainGame.Max_X + MainGame.Line_Span / 2) and (mouse_y > MainGame.Start_Y- MainGame.Line_Span/2 and mouse_y < MainGame.Max_Y + MainGame.Line_Span / 2):#print( str(mouse_x) + "" + str(mouse_y))#print(str(MainGame.Putdownflag))if MainGame.Putdownflag != MainGame.player1Color:returnclick_x = round((mouse_x - MainGame.Start_X) / MainGame.Line_Span)click_y = round((mouse_y - MainGame.Start_Y) / MainGame.Line_Span)click_mod_x = (mouse_x - MainGame.Start_X) % MainGame.Line_Spanclick_mod_y = (mouse_y - MainGame.Start_Y) % MainGame.Line_Spanif abs(click_mod_x-MainGame.Line_Span/2) >=5 and abs(click_mod_y-MainGame.Line_Span/2) >=5:#print("有效点:x="+str(click_x)+" y="+str(click_y))#有效点击点self.putdownchess(MainGame.player1Color, click_x, click_y)else:print("out")if MainGame.button_go.is_click():self.restart()print("button_go click")else:print("button_go click out")def putdownchess(self, t, x, y):flag = Falsefor item in MainGame.ChessmanList:if item.x == x and item.y == y:flag = Trueif not flag:cm = Chessman(t, x, y)MainGame.ChessmanList.append(cm)MainGame.Putdownflag = MainGame.player2Color#print("ChessmanListlen:" + str(len(MainGame.ChessmanList)))def bitechessman(self):for item in MainGame.ChessmanList:item.displaychessman()def bureautime(self):''''''def Computerplay(self):if MainGame.Putdownflag == MainGame.player2Color:print("轮到电脑了")computer = self.getComputerplaychess()if computer==None:return#print("computer x="+str(computer.x) + " y="+str(computer.y))MainGame.ChessmanList.append(computer)MainGame.Putdownflag = MainGame.player1Color#判断游戏胜利def VictoryOrDefeat(self):for item in MainGame.ChessmanList:if self.calHorizontalCount(item) == 5 or self.calVerticalityCount(item) == 5 or self.calBevelsUpCount(item) == 5 or self.calBevelsDownCount(item)==5:txt =""if item.troops == MainGame.player1Color :txt = "玩家"else:txt = "电脑"MainGame.window.blit(self.getTextSuface("%s获胜" % txt), (SCREEN_WIDTH-100, 200))MainGame.Putdownflag = MainGame.overColorreturndef restart(self):MainGame.ChessmanList = []MainGame.Putdownflag = MainGame.player1Colordef calHorizontalCount(self,chessman):count = 1for i in range(1, 5):fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x and x.y == chessman.y + i,MainGame.ChessmanList)if len(list(fi)) == 1:count += 1else:breakreturn countdef calVerticalityCount(self, chessman):count = 1for i in range(1, 5):fi = filter(lambda x:x.troops==chessman.troops and x.x == chessman.x+ i and x.y == chessman.y , MainGame.ChessmanList)if len(list(fi)) == 1:count += 1else:breakreturn countdef calBevelsUpCount(self, chessman):count = 1for i in range(1, 5):fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x + i and x.y == chessman.y - i,MainGame.ChessmanList)if len(list(fi)) == 1:count += 1else:breakreturn countdef calBevelsDownCount(self, chessman):count = 1for i in range(1, 5):fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x + i and x.y == chessman.y + i,MainGame.ChessmanList)if len(list(fi)) == 1:count += 1else:breakreturn countdef getTextSuface(self, text):pygame.font.init()# print(pygame.font.get_fonts())font = pygame.font.SysFont('kaiti', 18)txt = font.render(text, True, TEXT_COLOR)return txtdef endGame(self):print("exit")exit()def getComputerplaychess(self):if len(MainGame.ChessmanList) == 0:return Chessman(MainGame.player2Color, 9, 9)for i in range(0, 19):for j in range(0, 19):if len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.troops==MainGame.player1Color , MainGame.ChessmanList))):arr[i][j] = 1elif len(list(filter(lambda x: x.x == i and x.y == j and x.troops==MainGame.player2Color, MainGame.ChessmanList))):arr[i][j] = 2'''newarr = computer.getPoint(arr)if newarr.x != -1 and newarr.y != -1:print("结果:"+ str(newarr.x) +" "+ str(newarr.y))return Chessman(MainGame.player2Color, newarr.x, newarr.y)'''newarr = computer.getPoint(arr)if newarr[0] != -1 and newarr[1] != -1:print("结果:" + str(newarr[0]) + " " + str(newarr[1]))return Chessman(MainGame.player2Color, newarr[0], newarr[1])# 进攻# 防守for item in MainGame.ChessmanList:if item.troops == MainGame.player1Color:prev_x = item.x - 1prev_y = item.y - 1next_x = item.x + 1next_y = item.y + 1if next_x < 19 and len(list(filter(lambda x: x.x == next_x and x.y == item.y, MainGame.ChessmanList)))==0:return Chessman(MainGame.player2Color, next_x, item.y)if next_y < 19 and len(list(filter(lambda x: x.x == item.x and x.y == next_y, MainGame.ChessmanList)))==0:return Chessman(MainGame.player2Color, item.x, next_y)for i in range(0,18):for j in range(0, 18):fi= filter(lambda x: x.x == i and x.y == j, MainGame.ChessmanList)if len(list(fi)) == 0 :return Chessman(MainGame.player2Color, i, j)return Noneclass Chessman():def __init__(self,t,x,y):self.images = {'B' : img_chess_black,'W' : img_chess_white,}self.troops = tself.image = self.images[self.troops]self.x = xself.y = yself.rect = self.image.get_rect()self.rect.left = MainGame.Start_X + x * MainGame.Line_Span - MainGame.Line_Span/2self.rect.top = MainGame.Start_Y + y * MainGame.Line_Span - MainGame.Line_Span/2def displaychessman(self):if self.troops != 'N':self.image = self.images[self.troops]MainGame.window.blit(self.image,self.rect)if __name__ == '__main__':MainGame().startGame()
这样直接放代码的方式,大家是不是很喜欢!
现在代码到手了。感兴趣的朋友,赶快去试试吧!
觉得不错的话记得给我点赞哦!
【摸鱼神器】— 五子棋相关推荐
- 一键摸鱼神器火了!专为Windows系统打造,老板在身后也可以很淡定
点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 丰色 发自 凹非寺 量子位 报道 | 公众号 QbitAI 哪个打工 ...
- 达摩院清华博士研发了一个AI摸鱼神器!有了它,老板都不好意思打扰你
点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 来自:开源最前线 最近,清华大学又火了!开设了一门课程--<摸 ...
- 炸裂!VSCode 摸鱼神器!!!
来源:玩转Github本文约1600字,建议阅读5分钟 除了开发插件外,有哪些可以上班摸鱼的插件呢?这里给大家写一个渔夫指南,汇总一些在VSCode中可以"摸鱼"的好玩插件! 现在 ...
- 偷走不经意流逝的时光,摸鱼神器:神偷
大家好,我是TJ 关注TJ君,回复"武功秘籍"免费获取计算机宝典书籍 前几日TJ君和大家分享了一个摸鱼利器:一款插件,让摸鱼变得如此简单,聊QQ聊微信怎么看都像是在工作 发现还有不 ...
- Win10上线摸鱼神器,已经被玩疯了!
开源最前线(ID:OpenSourceTop) 猿妹 整编 整理自:https://www.cnblogs.com/dino623/p/developing_an_app_with_winui3.ht ...
- 程序员公开上班摸鱼神器!有了它,老板都不好意思打扰你!
文末赠书 来自/开源最前线(ID:OpenSourceTop) 链接/https://github.com/svenstaro/genact 最近,清华大学又火了!开设了一门课程--<摸鱼学导论 ...
- 这5个摸鱼神器太火了!程序员:知道了快删!
这届打工人最喜欢的两个词 除了下班,就是摸鱼 鲁迅曾说过:要想成为一名合格的打工人 摸鱼是必备技能之一 △ 图源网络,如侵删 适(放)当(空)摸(自)鱼(己) 是对工作的调剂 摸鱼摸得好,工作没烦恼 ...
- 华为“杀疯了”:发布“摸鱼”神器10余款新品
或许,你已经快被华为智慧办公产品包围了. 在笔记本电脑上"刷手机"是一种什么感觉? 比如摸鱼时间,坐在工位上的你可以在电脑上一键打开"开心消消乐",而其他人只 ...
- GitHub开源了一款程序员摸鱼神器!上班摸鱼还不会被老板发现。。。
点击上方"Github爱好者社区",选择星标 回复"资料",获取小编整理的一份资料 作者:GG哥 来源:GitHub爱好者社区(github_shequ) 这是 ...
最新文章
- MOSS的SPBuiltInFieldId成员的字段类型对照关系表
- Python正则表达式之编译正则表达式(2)
- 【Matlab】一个超简单的生成顺序数组的方法
- 交通运输线(LCA)
- mysql-5.7 持久化统计信息详解
- shields 徽标_到处都有平面徽标
- mysql --force -f_Mysql_mysql force Index 强制索引
- css菜单下拉菜单_在CSS中创建下拉菜单
- 笔记本卡顿不流畅是什么原因_为什么越来越多的笔记本电池不可拆卸
- es6 Symbol.for(),Symbol.keyFor()
- updatebyprimarykeyselective的where条件是全部字段_多组连续数据对比,不满足单因素方差分析的条件怎么办?...
- Leetcode 304.二维区域和检索-矩阵不可变
- python3 3种方式分别用for循环、while循环计算1到100的和
- 2019年 AI 顶会速递
- isis仿真软件怎么导入C语言,Proteus导入程序的操作方法
- m选n组合的两种算法(C语言实现)
- 麦考利久期公式(c语言实现)
- RAM与ROM的特点和区别
- 软件测试用例覆盖率怎么算,如何计算增量测试覆盖率
- 汉字编码问题(附编码察看器)