我试图制作记忆游戏,将图像应用到12个不同的按钮,并在显示2个按钮时检查图像是否相同。
-------------------完成表格?---------------------- ---
这是对Duncan C的建议的尝试;
#!/usr/bin/python
import os
import sys
import fnmatch
import glob
import re
import shutil
##########################################################################################################
#Remove the directory
def remove(path):
try:
shutil.rmtree(path)
print "Deleted : %s" % path
except OSError:
print OSError
print "Unable to remove folder: %s" % path
##########################################################################################################
#This function will look for the .sh files in a given path and returns them as a list.
def searchTreeForSh(path):
full_path = path+'*.sh'
listOfFolders = glob.glob(full_path)
return listOfFolders
##########################################################################################################
#Gets the full path to files containig .sh and returns a list of folder names (prefix) to be acted upon.
#listOfScripts is a list of full paths to .sh file
#dirname is the value that holds the root directory where listOfScripts is operating in
def getFolderNames(listOfScripts):
listOfFolders = []
folderNames = []
for foldername in listOfScripts:
listOfFolders.append(os.path.splitext(foldername)[0])
for folders in listOfFolders:
folder = folders.split('/')
foldersLen=len(folder)
folderNames.append(folder[foldersLen-1])
folderNames.sort()
return folderNames
##########################################################################################################
def minmax(items):
return max(items)
##########################################################################################################
#This function will check the latest entry in the tuple provided, and will then send "everything" to the remove function except that last entry
def sortBeforeDelete(statDir, t):
count = 0
tuple(statDir)
timeNotToDelete = minmax(statDir)
for ff in t:
if t[count][1] == timeNotToDelete:
count += 1
continue
else:
remove(t[count][0])
count += 1
##########################################################################################################
#A loop to run over the fullpath which is broken into items (see os.listdir above), elemenates the .sh and the .txt files, leaves only folder names, then matches it to one of the
#name in the "folders" variable
def coolFunction(folderNames, path):
localPath = os.listdir(path)
for folder in folderNames:
t = () # a tuple to act as sort of a dict, it will hold the folder name and it's equivalent st_mtime
statDir = [] # a list that will hold the st_mtime for all the folder names in subDirList
for item in localPath:
if os.path.isdir(path + item) == True:
if re.search(folder, item):
mtime = os.stat(path + '/' + item)
statDir.append(mtime.st_mtime)
t = t + ((path + item,mtime.st_mtime),)# the "," outside the perenthasis is how to make t be a list of lists and not set the elements one after theother.
if t == ():continue
sortBeforeDelete(statDir, t)
##########################################################################################################
def main(path):
dirs = os.listdir(path)
for component in dirs:
if os.path.isdir(component) == True:
newPath = path + '/' + component + '/'
listOfFolders= searchTreeForSh(newPath)
folderNames = getFolderNames(listOfFolders)
coolFunction(folderNames, newPath)
##########################################################################################################
if __name__ == "__main__":
main(sys.argv[1])
答案 0 :(得分:2)
@IBAction func
即可。所以你现在可以有一个@IBAction func buttonPressed(sender: UIButton)
而不是12个(我想,因为你的函数被称为@IBAction func button1PRESSED(sender: AnyObject)
)。因此,当您第一次单击按钮时,您可以存储按钮,如果单击了第一个或第二个按钮,也会存储。当您单击第二个按钮时,检查它是否与单击的第一个按钮具有相同的UIImage
,否则就是您应该做的事情。
@IBAction func buttonPressed(sender: UIButton) {
if self.firstButtonStored == true {
if self.firstButton.image == sender.image {
// They are the same
} else {
// they are not the same
}
} else {
self.firstButtonStored = true
self.firstButton = sender
}
}
我还建议将所有按钮存储在一个OutletCollection
(像数组一样工作)而不是自己的12个按钮。而且我还会使用UIImage
以外的其他内容来检查它们是否相同,不确定这是否真的有效,因为您需要图像名称而不是图像本身来进行比较。如果您需要帮助,请告诉我。
答案 1 :(得分:2)
不要,不要, DON' 创建12个单独的变量,card1-card12
和12 IBActions
button1PRESSED
- {{1} }。这种事情,你有很多完全相同的事情,唯一改变的是价值是" 代码气味"。它告诉你你做错了。
相反,你应该找到一种方法来做到这一点,你可以对所有按钮使用相同的IBAction,并索引到数组而不是有12个单独的变量。
我建议在按钮上使用标签。按钮1使用标签1,按钮2使用标签2等
然后在您的IBAction中,从按钮中获取标签并使用它来确定按下了哪个按钮以及该怎么做。
您还需要一个标志,告诉您这是第一个按下的按钮还是第二个按钮,这样您就可以判断是否需要测试匹配(如果它是第二个按钮)或只记得哪一个被按下(如果这是按下的第一个按钮。)
button12PRESSED
您可以为每个值使用一组图像,然后根据var firstButtonValue: Int
var firstButtonAlreadyPresssed: Bool
@IBAction func buttonPressed(sender: UIButton)
{
if !firstButtonAlreadyPresssed
{
firstButtonValue = sender.tag //remember the button for later
firstButtonAlreadyPresssed = true
}
else
{
//We already have a first button pressed.
if sender.tag == firstButtonValue
{
//right answer. Handle it.
}
else
{
//wrong answer. Handle it.
}
firstButtonAlreadyPresssed = false //reset the "isFirstButton" flag for the next time.
}
}
或firstButtonValue
的值获取该图像。
答案 2 :(得分:2)
方法,而不是完整的解决方案
创建一个包含Card
和value
属性的结构index
,并按值Equatable
生成。
您可以添加更多属性,例如done
,status
,image
或其他。
struct Card : Equatable, CustomStringConvertible {
let value : Int
let index : Int
var description : String { return "Card \(index)"}
}
func ==(lhs: Card, rhs: Card) -> Bool
{
return lhs.value == rhs.value
}
随机播放值并创建一个包含12张卡片的数组
var values = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
values.shuffleInPlace()
var cards = [Card]()
for (index, value) in values.enumerate() {
cards.append(Card(value: value, index: index))
}
实施查找相应卡片的方法
func twinCard(card : Card) -> Card {
return cards.filter({$0 == card && $0.index != card.index}).first!
}
在Interface Builder中,将标签从0到11分配给12个按钮,并将所有按钮连接到同一个@IBAction
@IBAction func buttonPressed(sender: UIButton) {
let tag = sender.tag
let chosenCard = cards[tag]
let otherCard = twinCard(chosenCard)
// The following depends on the design of the UI
// for example get the corresponding button by viewWithTag(otherCard.index)
// or use an array which holds the button references.
...
}
不要随意移动视图,随机播放模型; - )