Python版警察抓小偷游戏源代码

描述

Python版警察抓小偷游戏源代码,有多个难度级别,直接运行game.py,输入难度级别(1-13)。不同的难度等级对应不同的图形。

python

game.py

""" Header files to initialize the game """importpygameimportgameGraphimportosfromtkinterimportmessageboxfromtkinterimport*importplatformimportBFSimportrandomimportcop_robber_algorithm currentOS = platform.system()""" GAME DRIVER CODE """print(f"Welcome to Cops and Robbers!") level =input("Please enter the level (1 - 13): ")whileint(level) >13orint(level) <1:print(f"Invalid Input!") level =input("Please enter the level (1 - 13): ") graphFile =open("data/level"+ level +".txt","r") fileData = graphFile.readlines() totalVertices, totalEdges =map(int, fileData[0].split()) graph = gameGraph.Graph(totalVertices, totalEdges) graph.acceptGraph(fileData) gameMatrix = graph.returnDirectedAdjacencyMatrix() algoMatrix = graph.returnUndirectedAdjacencyMatrix()defcheckLink(nodeA, nodeB):ifalgoMatrix[nodeA][nodeB] ==1:returnTrueTk().wm_withdraw()# to hide the main windowmessagebox.showinfo('Node','Node: '+str(nodeA) +' is not connected to the current Robber Node')returnFalsepygame.init()# Initialize pygame module""" Optimizing screen resolution factor on the basis of operating system """ifcurrentOS =="Windows": factor =0.8elifcurrentOS =="Linux": factor =1elifcurrentOS =="Darwin": factor =0.8defnodeClicked(node):Tk().wm_withdraw()# to hide the main windowmessagebox.showinfo('Next Node','You have selected Node: '+str(node))""" Game Window Attributes """screenSize = (int(1500* factor),int(1000* factor)) screen = pygame.display.set_mode(screenSize) pygame.display.set_caption("Cops and Robbers") screen.fill([255,255,255])""" Sprite Attributes """# GRAPH ATTRIBUTES #nodeVector = [pygame.sprite.Sprite()foriinrange(totalVertices)] counter =0# ACCEPT GRAPH FROM FILE #locationVector = [] file =open("data/nodePos"+ level +".txt","r") lines = file.readlines()forlineinlines: x, y =map(int, line.split()) x =int(x * factor) y =int(y * factor) locationVector.append((x, y))fornodeinnodeVector: node.image = pygame.transform.scale(pygame.image.load("sprites/node.png").convert_alpha(), (int(75* factor),int(75* factor))) node.rect = node.image.get_rect(center=locationVector[counter]) screen.blit(node.image, node.rect) counter = counter +1# COP ATTRIBUTES #copNode =0cop = pygame.sprite.Sprite() cop.image = pygame.transform.scale(pygame.image.load("sprites/cop.png").convert_alpha(), (int(45* factor),int(45* factor)))################game_folder = os.path.dirname(__file__) img_folder = os.path.join(game_folder,"img")# ROBBER ATTRIBUTES #robberNode =1robber = pygame.sprite.Sprite() robber.image = pygame.transform.scale(pygame.image.load("sprites/robber.png").convert_alpha(), (int(45* factor),int(45* factor)))# DRAW EDGES #foriinrange(totalVertices):forjinrange(totalVertices):ifgameMatrix[i][j] ==1andi != j: pygame.draw.line(screen, (0,0,0), nodeVector[i].rect.center, nodeVector[j].rect.center,int(5* factor)) valCorrect =int(22* factor) turn =0defgameplay(gameRunning):""" Function that controls the essential initial components of the game """globalrobberNode, copNode, turnwhilegameRunning:""" UPDATE POSITIONS OF COP AND ROBBER SPRITE AT EVERY STEP """screen.blit(robber.image, (locationVector[robberNode][0] - valCorrect, locationVector[robberNode][1] - valCorrect)) screen.blit(cop.image, (locationVector[copNode][0] - valCorrect, locationVector[copNode][1] - valCorrect)) pygame.display.flip()""" HANDLE USER ACTION """foruserActioninpygame.event.get():""" QUIT IF THE EXIT CROSS IS CLICKED """ifuserAction.type== pygame.QUIT: gameRunning =False""" HANDLING MOUSE BUTTON CLICKS """ifpygame.mouse.get_pressed()[0]:foriinrange(totalVertices):ifnodeVector[i].rect.collidepoint(pygame.mouse.get_pos()): nodeClicked(i)ifcheckLink(i, robberNode):""" MOVING THE ROBBER TO A NEW NODE """pygame.draw.rect(screen, (255,0,0), ( ( locationVector[robberNode][0] - valCorrect, locationVector[robberNode][1] - valCorrect), (int(45* factor),int(45* factor)))) robberNode = i screen.blit(robber.image, ( locationVector[robberNode][0] - valCorrect, locationVector[robberNode][1] - valCorrect)) pygame.display.flip()""" CHECK IF THE TWO SPRITES HAVE HIT THE SAME NODE """ifrobberNode == copNode: Tk().wm_withdraw()# to hide the main windowmessagebox.showinfo('Uh-Oh!','Looks like you were caught') gameRunning =Falsebreak""" MOVING THE COP TO A NEW NODE """pygame.draw.rect(screen, (0,255,0), ( (locationVector[copNode][0] - valCorrect, locationVector[copNode][1] - valCorrect), (int(45* factor),int(45* factor)))) copNode = BFS.BFS(graph, copNode, robberNode) screen.blit(cop.image, ( locationVector[copNode][0] - valCorrect, locationVector[copNode][1] - valCorrect)) pygame.display.flip() turn = turn +1""" CHECK IF THE TWO SPRITES HAVE HIT THE SAME NODE """ifrobberNode == copNode: Tk().wm_withdraw()# to hide the main windowmessagebox.showinfo('Uh-Oh!','Looks like you were caught')return"Lost"elifturn > totalEdges +1: Tk().wm_withdraw()# to hide the main windowmessagebox.showinfo('Woooohooooo!','Looks like you evaded the cops for long enough!')return"Won"runStatus =TruerobberNode =1gameResult = gameplay(runStatus) cop_robber_algorithm.cop_robber_preliminary(algoMatrix, totalVertices)ifcop_robber_algorithm.cop_robber_final(algoMatrix, totalVertices): graphType ="Robber Win"else: graphType ="Cop Win"Tk().wm_withdraw() messagebox.showinfo(gameResult,"Level: "+ level +"\\n"+"Turns Survived: "+str(turn) +"\\n"+"Graph Type:"+ graphType)

完整程序代码下载地址:

https://download.csdn.net/download/weixin_42756970/86813741

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分