Python版警察抓小偷游戏源代码,有多个难度级别
Python版警察抓小偷游戏源代码,有多个难度级别,直接运行game.py,输入难度级别(1-13)。不同的难度等级对应不同的图形。
game.py
""" Header files to initialize the game """
import pygame
import gameGraph
import os
from tkinter import messagebox
from tkinter import *
import platform
import BFS
import random
import cop_robber_algorithmcurrentOS = platform.system()""" GAME DRIVER CODE """
print(f"Welcome to Cops and Robbers!")
level = input("Please enter the level (1 - 13): ")while int(level) > 13 or int(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()def checkLink(nodeA, nodeB):if algoMatrix[nodeA][nodeB] == 1:return TrueTk().wm_withdraw() # to hide the main windowmessagebox.showinfo('Node', 'Node: ' + str(nodeA) + ' is not connected to the current Robber Node')return Falsepygame.init() # Initialize pygame module""" Optimizing screen resolution factor on the basis of operating system """
if currentOS == "Windows":factor = 0.8
elif currentOS == "Linux":factor = 1
elif currentOS == "Darwin":factor = 0.8def nodeClicked(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() for i in range(totalVertices)]
counter = 0# ACCEPT GRAPH FROM FILE #
locationVector = []
file = open("data/nodePos" + level + ".txt", "r")
lines = file.readlines()
for line in lines:x, y = map(int, line.split())x = int(x * factor)y = int(y * factor)locationVector.append((x, y))for node in nodeVector: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 = 0
cop = 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 = 1
robber = pygame.sprite.Sprite()
robber.image = pygame.transform.scale(pygame.image.load("sprites/robber.png").convert_alpha(),(int(45 * factor), int(45 * factor)))# DRAW EDGES #
for i in range(totalVertices):for j in range(totalVertices):if gameMatrix[i][j] == 1 and i != j:pygame.draw.line(screen, (0, 0, 0), nodeVector[i].rect.center, nodeVector[j].rect.center, int(5 * factor))valCorrect = int(22 * factor)
turn = 0def gameplay(gameRunning):""" Function that controls the essential initial components of the game """global robberNode, copNode, turnwhile gameRunning:""" 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 """for userAction in pygame.event.get():""" QUIT IF THE EXIT CROSS IS CLICKED """if userAction.type == pygame.QUIT:gameRunning = False""" HANDLING MOUSE BUTTON CLICKS """if pygame.mouse.get_pressed()[0]:for i in range(totalVertices):if nodeVector[i].rect.collidepoint(pygame.mouse.get_pos()):nodeClicked(i)if checkLink(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 = iscreen.blit(robber.image, (locationVector[robberNode][0] - valCorrect, locationVector[robberNode][1] - valCorrect))pygame.display.flip()""" CHECK IF THE TWO SPRITES HAVE HIT THE SAME NODE """if robberNode == 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 """if robberNode == copNode:Tk().wm_withdraw() # to hide the main windowmessagebox.showinfo('Uh-Oh!', 'Looks like you were caught')return "Lost"elif turn > totalEdges + 1:Tk().wm_withdraw() # to hide the main windowmessagebox.showinfo('Woooohooooo!', 'Looks like you evaded the cops for long enough!')return "Won"runStatus = True
robberNode = 1
gameResult = gameplay(runStatus)
cop_robber_algorithm.cop_robber_preliminary(algoMatrix, totalVertices)
if cop_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)
完整程序代码下载地址:Python版警察抓小偷游戏源代码
Python版警察抓小偷游戏源代码,有多个难度级别相关推荐
- Python版打字练习软件源代码,键盘练习软件源代码,含娱乐模式和训练模式
Python版打字练习软件源代码,键盘练习软件源代码,含娱乐模式和训练模式 按ESC切换左手练习,右手练习.双手练习 完整代码下载地址:Python版打字练习软件源代码 核心代码: import ra ...
- Python版超市管理系统源代码,基于django+mysql
Python版超市管理系统源代码,基于django+mysql 安装步骤 1.在mysql中创建名为demo_django_supermarket的数据库,修改config/setting.py中数据 ...
- Python飞行棋游戏源代码,基于socket网络通信的小游戏,可设置多个游戏房间及参与飞行棋游戏的玩家
直接运行ludoServer.py即可,然后用浏览器打开http://127.0.0.1:4399/,完成房间创建.房间设置及玩家设备即可开始游戏 完整程序代码下载地址:Python飞行棋游戏源代码 ...
- Python五子棋小游戏源代码,支持人机对战和局域网对战两模式
Python五子棋小游戏源代码,支持人机对战和局域网对战两模式,程序运行截图: 核心程序代码 WuZi.py ''' Function:五子棋小游戏-支持人机和局域网对战 Author:Charles ...
- Python跳跳兔小游戏源代码,兔年必玩小游戏,兔年大吉
Python跳跳兔小游戏源代码,兔年必玩小游戏,兔年大吉,小兔子跳跳,按空格键向上跳跃,按键盘方向键进行左右移动,以避开飞弹,以防被炸,还可以捡到火箭道具哦. 完整程序下载地址:Python跳跳兔小游 ...
- Python地雷战小游戏源代码
Python版地道战小游戏源代码,游戏中寻找所需要的五种合成地雷的原材料,并躲避敌人的抓捕,雷可以炸死敌方.程序运行截图: 主要程序代码: tunnel_war_game.py import pyga ...
- Python小鸟管道游戏源代码及素材
Python小鸟管道游戏源程序包含一个Vipgame2.py及一个图片素材包,小鸟管道游戏的素材包请在百度网盘下载,https://pan.baidu.com/s/1agk5zgre7CZHJ0-pd ...
- Python版名片管理系统源代码
Python版名片管理系统,功能: 新增名片(记录用户的姓名,电话,QQ,邮箱): 显示所有名片: 查询名片: 查询成功后可以修改,删除名片 运行截图: cards_main.py :程序的入口,完整 ...
- Python项目实战:Python版超市管理系统源代码
Python版超市管理系统可实现下单商品,修改商品数量,删除商品,结算商品. 程序使用元组代表商品,元组的多个元素分别代表商品条码,商品名称,商品单价: 使用dict来表示系统当前仓库中的所有商品,d ...
最新文章
- 配置MUX VLAN示例(汇聚层设备)
- xml、 Dao service 三层参数以及对应关系
- ios12彻底关闭siri_Siri正在iOS 12中获取自定义语音操作
- [C++11]独占的智能指针unique_ptr的删除器
- 京东:妥善处理个别显卡售后的问题 不存在“金融化”情况
- Jupyter插件的使用
- php ajax mysql视频教学视频_PHP入门教程之AJAX 与 MySQL
- Solaris系统环境变量声明方法
- CSS-div垂直居中方法总结
- MD5 加密算法详细介绍
- Python功能实现:为pdf电子书籍生成书签目录
- java中dao_java中的Dao类是什么意思?
- JS实现将数字金额转换为大写人民币汉字的方法
- 服务器系统盘是否需要阵列,服务器硬盘必须设置阵列吗
- 来自2018年最后的瞎扯——从“空间”到“强人工智能”
- 实数 有理数 无理数
- 【Python爬虫】:模拟登录QQ空间
- 运维工程师的工作内容有哪些?能详细列举一下吗?
- 科技交流英语(2022秋)
- IntelliJ IDEA快速入门 | 第三十篇:如何来自定义模板呢?