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_algorithm

currentOS = 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 True
    Tk().wm_withdraw()  # to hide the main window
    messagebox.showinfo('Node', 'Node: ' + str(nodeA) + ' is not connected to the current Robber Node')
    return False


pygame.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.8


def nodeClicked(node):
    Tk().wm_withdraw()  # to hide the main window
    messagebox.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 = 0


def gameplay(gameRunning):
    """ Function that controls the essential initial components of the game """
    global robberNode, copNode, turn
    while 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 = 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 """
                            if robberNode == copNode:
                                Tk().wm_withdraw()  # to hide the main window
                                messagebox.showinfo('Uh-Oh!', 'Looks like you were caught')
                                gameRunning = False
                                break

                            """ 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 window
                                messagebox.showinfo('Uh-Oh!', 'Looks like you were caught')
                                return "Lost"
                            elif turn > totalEdges + 1:
                                Tk().wm_withdraw()  # to hide the main window
                                messagebox.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版警察抓小偷游戏源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python代码大全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值