尝试为回合制游戏创建重播功能(非常新手)

2024-06-08 21:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在玩一个名为Hearthstone的交易卡游戏,它是由暴雪制作的。这个游戏相当不错,但缺乏任何一款自称“竞争性”的游戏应该具备的基本功能,比如统计跟踪和回放。 所以,正如我在标题中所说,我正在尝试创建一个(非常粗糙和糟糕的)脚本,让我记录下我打的每一场比赛。由于我缺乏编程技能,80%的脚本只是一堆代码,我从各种地方借来的,并进行了调整,使之能满足我的需要。 我们的想法是让它像这样工作

  1. 我把我玩的每一圈都拍下来。这可能会让人恼火,但我不敢考虑实现OCR来让脚本在每一个回合开始时都拍照。太棒了,但我做不到。。。在

    游戏将所有图片发送到桌面(无需编码)。

  2. 游戏结束后,我运行脚本

2.1每个匹配项都将有一个带编号的文件夹,因此脚本会创建该文件夹。这些文件夹将被称为“Match1”、“Match2”等等。你可以看到我自己写的有多糟糕:P

import sys
import os
import shutil

def checkFolder():
os.path.join('D:\Hearthstone\Replays\Match1')
matchNumber=1
while os.path.exists("D:\\Hearthstone\\Replays\\Match"+ str(matchNumber)) is True:
    matchNumber=matchNumber + 1
else:
    os.makedirs("D:\Hearthstone\Replays\Match"+str(matchNumber))

2.2脚本将照片从桌面发送到最近创建的文件夹。问题是我不知道如何让脚本将目标文件夹更改为创建的最新文件夹。我没有写这部分代码,我只是修改了它。来源:http://tinyurl.com/srcbh

^{pr2}$

就这样!我需要第2.2步的帮助。我将非常感谢您的帮助! 现在,我之所以写这么大的帖子,是因为我想把我的想法公之于众,希望能激励别人认真对待类似的项目。Hearthstone有成千上万的玩家可以从中受益,更不用说对于经验丰富的人来说,这似乎是一个相当简单的任务。在


Tags: path代码import脚本文件夹游戏osmatch
3条回答

首先,你发现了一个问题并提出了一个解决方案,这表明你并不缺乏编程技能。给自己点荣誉吧。你只需要多练习。:)

这应该更好,我还没有运行它,所以可能有一些错误:p

def checkFunction(base_dir='D:\\Hearthstone\\Replays\\'):  #set this as a parameter with a default
    match_number = 1
    if os.path.exists(base_dir):  #if the base directory doesn't exist you'll have a bad time
        while os.path.exists(os.path.join(base_dir, 'Match{0}'.format(match_number)))
            match_number += 1
        new_dir = os.path.join(base_dir, 'Match{0}'.format(match_number))
        os.makedirs(new_dir)
        return new_dir

对于checkFolder函数,我建议它返回新的目录名(如上所述)。您还需要缩进它下面的所有行,以便python知道这些行是该函数的一部分(这可能只是格式问题)。在

然后,一旦checkFolder函数正常工作,您在2.2中所做的更改就是:

^{pr2}$

这将查看文件夹中的匹配项,并获取文件夹的最小编号。在

folder = os.path.join('C:\\Users\\Felipe\\', 'Desktop') # Folder in which the images are in. 
recorded_matches_location = 'D:\\Hearthstone\\Replays\\'
match_number = 1
match_name = 'match1'
while match_name in os.listdir(recorded_matches_location):
    match_number = 1 + match_number
    match_name = 'match' + str(match_number) # corrected it! there must be a string and not a variable
destination = os.path.join(recorded_matches_location, match_name) #**Destination needs to be the newest folder and I dont know how to implement that...
extmove = 'png' # The extension you wish to organize. 
num = 0 # Variable simply to use after to count images. 

for filename in os.listdir(folder): #Run through folder.
        extension = filename.split(".")[-1] # This strips the extensions ready to check and places into the extension
        if extension == extmove: # If statement. If the extension of the file matches the one set previously then..
            shutil.move(folder + "\\" + filename, destination) # Move the file from the folder to the destination folder. Also previously set. 
        num = num + 1
        print(num)

print (filename, extension)

好吧,我终于成功了!在

import sys
import os
import shutil

def sendPhotos():
    matchNumber=1
    photos_dest = "D:\\Hearthstone\\Replays\\Match"
    while os.path.exists(photos_dest+ str(matchNumber)): #creates the name of the folder "Match1", "Match2", etc.
        matchNumber=matchNumber + 1
    else:
        photos_destination = photos_dest+str(matchNumber)
        os.makedirs(photos_destination)
        for files in os.listdir('C:\\Users\\Felipe\\Desktop'):#only png files are moved
            if files.endswith(".png"):
                shutil.move(files, photos_destination)

sendPhotos()

感谢那些给我答案的人!我真的很感激!在

相关问题 更多 >