在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

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

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

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)

完整程序代碼下載地址:

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

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 圖形
    +關注

    關注

    0

    文章

    71

    瀏覽量

    19279
  • 源代碼
    +關注

    關注

    96

    文章

    2945

    瀏覽量

    66747
  • python
    +關注

    關注

    56

    文章

    4797

    瀏覽量

    84683
收藏 人收藏

    評論

    相關推薦

    java編寫的掃雷游戲源代碼

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

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

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

    貪吃蛇游戲java源代碼

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

    Python微服務開發的源代碼合集免費下載

    本文檔的主要內容詳細介紹的是Python微服務開發的源代碼合集免費下載。
    發表于 09-20 08:00 ?3次下載

    python的html基本結構及常見文本標簽源代碼免費下載

    本文檔的主要內容詳細介紹的是python的html基本結構及常見文本標簽源代碼免費下載。
    發表于 12-04 08:00 ?0次下載
    <b class='flag-5'>python</b>的html基本結構及常見文本標簽<b class='flag-5'>源代碼</b>免費下載

    Python深度學習2018的源代碼合集免費下載

    本文檔的主要內容詳細介紹的是Python深度學習2018的源代碼合集免費下載。
    發表于 01-16 10:25 ?70次下載

    python實現目標檢測的源代碼免費下載

    本文檔的主要內容詳細介紹的是python實現目標檢測的源代碼免費下載
    發表于 04-09 08:00 ?6次下載
    <b class='flag-5'>python</b>實現目標檢測的<b class='flag-5'>源代碼</b>免費下載

    python文件讀取的源代碼免費下載

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

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

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

    使用Python按行讀文件的源代碼免費下載

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

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

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

    Python版超市管理系統源代碼

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

    Python版蚊子大作戰源代碼

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

    Python編程實戰(源代碼)

    [源代碼]Python編程實戰 妙趣橫生的項目之旅
    發表于 06-06 17:49 ?3次下載

    [源代碼]Python算法詳解

    [源代碼]Python算法詳解[源代碼]Python算法詳解
    發表于 06-06 17:50 ?0次下載
    主站蜘蛛池模板: 天天干夜夜欢| 天堂69亚洲精品中文字幕| 亚洲网在线| 六月婷婷网视频在线观看| 亚洲精品美女久久久久网站| yy8090韩国日本三理论免费| 在线播放色| 狠狠狠狼鲁欧美综合网免费| 久久久精品免费观看| www.色涩| 五月婷婷深爱五月| 国产专区青青草原亚洲| 狠狠干网址| 亚洲人毛茸茸bbxx| 国产精品嫩草影院一二三区| 大香线蕉97久久| 久久精品re| 久久久噜噜噜久久网| 狠狠久| 亚洲成a人片在线观看导航| 国产免费一区二区三区最新| 粉嫩尤物在线456| 特级毛片aaaa级毛片免费| 狼狼色丁香久久女婷婷综合| 欧美黑粗硬| 亚洲天堂ww| 99成人在线| 曰本a| 久久夜色撩人精品国产| 国产午夜视频在线观看| 天堂综合网| 午夜视频吧| 六月丁香啪啪| 日本小视频免费| 在线女同免费观看网站| 国产精品美女久久久| 天天插天天摸| 天天做天天爱夜夜爽| 亚洲一区二区免费看| 美女被啪到哭网站在线观看| 国产大片免费观看中文字幕|