那曲檬骨新材料有限公司

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Python版警察抓小偷游戲源代碼

汽車電子技術(shù) ? 來源:Python代碼大全 ? 作者: Python代碼狂人 ? 2023-02-24 09:56 ? 次閱讀

Python版警察抓小偷游戲源代碼,有多個難度級別,直接運(yùn)行g(shù)ame.py,輸入難度級別(1-13)。不同的難度等級對應(yīng)不同的圖形。

圖片

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)

完整程序代碼下載地址:

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

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 圖形
    +關(guān)注

    關(guān)注

    0

    文章

    71

    瀏覽量

    19346
  • 源代碼
    +關(guān)注

    關(guān)注

    96

    文章

    2946

    瀏覽量

    66950
  • python
    +關(guān)注

    關(guān)注

    56

    文章

    4807

    瀏覽量

    85035
收藏 人收藏

    評論

    相關(guān)推薦

    java編寫的掃雷游戲源代碼

    求一個java編寫的掃雷游戲源代碼,謝謝!!!
    發(fā)表于 07-15 15:20

    HFC5209A“不好了,有人偷東西,快來抓小偷”語言集成電

    HFC5209A“不好了,有人偷東西,快來抓小偷”語言集成電路圖
    發(fā)表于 03-31 16:30 ?1264次閱讀
    HFC5209A“不好了,有人偷東西,快來<b class='flag-5'>抓小偷</b>”語言集成電

    貪吃蛇游戲java源代碼

    貪吃蛇游戲java源代碼
    發(fā)表于 12-27 17:56 ?9次下載

    Python微服務(wù)開發(fā)的源代碼合集免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是Python微服務(wù)開發(fā)的源代碼合集免費(fèi)下載。
    發(fā)表于 09-20 08:00 ?3次下載

    python的html基本結(jié)構(gòu)及常見文本標(biāo)簽源代碼免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是python的html基本結(jié)構(gòu)及常見文本標(biāo)簽源代碼免費(fèi)下載。
    發(fā)表于 12-04 08:00 ?0次下載
    <b class='flag-5'>python</b>的html基本結(jié)構(gòu)及常見文本標(biāo)簽<b class='flag-5'>源代碼</b>免費(fèi)下載

    Python深度學(xué)習(xí)2018的源代碼合集免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是Python深度學(xué)習(xí)2018的源代碼合集免費(fèi)下載。
    發(fā)表于 01-16 10:25 ?70次下載

    python實(shí)現(xiàn)目標(biāo)檢測的源代碼免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是python實(shí)現(xiàn)目標(biāo)檢測的源代碼免費(fèi)下載
    發(fā)表于 04-09 08:00 ?6次下載
    <b class='flag-5'>python</b>實(shí)現(xiàn)目標(biāo)檢測的<b class='flag-5'>源代碼</b>免費(fèi)下載

    python文件讀取的源代碼免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是python文件讀取的源代碼免費(fèi)下載。
    發(fā)表于 08-07 17:14 ?20次下載
    <b class='flag-5'>python</b>文件讀取的<b class='flag-5'>源代碼</b>免費(fèi)下載

    使用文件保存游戲python代碼和資料說明

    本文檔的主要內(nèi)容詳細(xì)介紹的是使用文件保存游戲python代碼和資料說明免費(fèi)下載。
    發(fā)表于 09-24 17:08 ?11次下載
    使用文件保存<b class='flag-5'>游戲</b>的<b class='flag-5'>python</b><b class='flag-5'>代碼</b>和資料說明

    使用Python按行讀文件的源代碼免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是使用Python按行讀文件的源代碼免費(fèi)下載。
    發(fā)表于 10-22 17:57 ?12次下載
    使用<b class='flag-5'>Python</b>按行讀文件的<b class='flag-5'>源代碼</b>免費(fèi)下載

    基于LabVIEW的貪吃蛇游戲源代碼

    基于LabVIEW的貪吃蛇游戲源代碼
    發(fā)表于 04-22 09:27 ?74次下載

    Python版超市管理系統(tǒng)源代碼

    Python版超市管理系統(tǒng)源代碼,基于django+mysql安裝步驟
    的頭像 發(fā)表于 02-24 09:59 ?1807次閱讀
    <b class='flag-5'>Python</b>版超市管理系統(tǒng)<b class='flag-5'>源代碼</b>

    Python版蚊子大作戰(zhàn)源代碼

    Python滅蚊小游戲源代碼,超解壓的滅蚊小游戲,通過消滅蚊子賺錢,屏幕里的蚊子不被消滅就會被蚊子吸血,通過商店購買血包、血瓶、血桶、回血針來使自己回血,也可以在商店購買不同的滅蚊工具
    的頭像 發(fā)表于 02-24 10:29 ?1209次閱讀
    <b class='flag-5'>Python</b>版蚊子大作戰(zhàn)<b class='flag-5'>源代碼</b>

    Python編程實(shí)戰(zhàn)(源代碼)

    [源代碼]Python編程實(shí)戰(zhàn) 妙趣橫生的項(xiàng)目之旅
    發(fā)表于 06-06 17:49 ?3次下載

    [源代碼]Python算法詳解

    [源代碼]Python算法詳解[源代碼]Python算法詳解
    發(fā)表于 06-06 17:50 ?0次下載
    百家乐游戏平台有哪些哪家的口碑最好 | 金鼎百家乐官网局部算牌法| 大发888com| 百家乐官网玄机| 威尼斯人娱乐场积分| 线上百家乐官网开户| 爱拼百家乐的玩法技巧和规则| 百家乐官网平台要多少钱| 太阳城酒店| 上海百家乐官网的玩法技巧和规则| 大发888娱乐场下载最高| 金榜百家乐官网的玩法技巧和规则 | 大发888免费软件下载| 网上百家乐官网乐代理| 明升娱乐城开户| 百家乐桌现货| 永利博百家乐官网游戏| 二八杠怎么玩| 大西洋百家乐官网的玩法技巧和规则 | 广元市| 百家乐陷阱| 新葡京百家乐官网娱乐城 | 太阳城百家乐下载网址| 百家乐官网的桌布| 新大发888娱乐城| 玩百家乐官网五湖四海娱乐城| 乐透乐博彩论坛| 百家乐庄闲机率分析| 网络百家乐官网真假| 奇博网上娱乐| 波浪百家乐游戏中| 赌场百家乐官网网站| 优博网| 大发888娱乐场下| 澳门百家乐职业赌客| 互联网百家乐官网的玩法技巧和规则 | 百家乐官网玩法开户彩公司| 澳门百家乐长赢打| 百家乐注码技巧| 博彩论坛交流中心| 免费百家乐倍投工具|