首先,我意識到下面的代碼可能不是很好,所以對任何讓你感到畏懼的道歉,我只是想盡可能多地編碼,希望得到更好。一個新手的hang子手項目:處理重複的字母
這是一個小hang子手遊戲項目的一部分,我試圖找出處理字符串中重複字母的最佳方法。
這就是我現在:
def checkDupes(word):
global dupeList
global repeatTimesDupes
if repeatTimesDupes != 0:
dupeCount = 0
for i in range(len(word)):
temp = word[i]
print("temp letter is: ", temp)
for j in range(i+1,len(word)):
if word[j] == temp:
if temp not in dupeList:
dupeList.append(word[j])
print("the dupeList contains: ", dupeList)#debug
repeatTimesDupes -= 1
def getLetter(position,buttons,word):
i = 96
index = 0
letter = chr(i)
for button in buttons:
if button != None:
i+=1
if button.collidepoint(position):
print("the position is: ", position)
print(i)
for j in range(len(word)):
print(word[j] , chr(i))
if word[j] == chr(i):
index = j
return chr(i), index
else:
return '?', -1
def checkForLetter(word,letter):
inWord = " "
for i in range(len(word)):
if word[i] == letter:
inWord = True
break
else:
print(len(word))
print (word[i])
inWord = False
return inWord
#========================== Start Loop ===========================================
while done == False:
events = pygame.event.get()
screen.fill(BGCOLOR)
timedelta = clock.tick_busy_loop(60)
timedelta /= 1000 # Convert milliseconds to seconds
for event in events:
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
if event.type == pygame.MOUSEBUTTONUP:
if event.button == MOUSEBUTTONLEFT:
pos = pygame.mouse.get_pos()
for button in buttonsList:
if button.collidepoint(pos):
if button != None:
checkDupes(gameWord)
letter, atIndex = getLetter(pos,buttonsList,gameWord)
letterSelected = True
moveCounter+=1
screen.blit(blackBG,(0,0))
showButtons(letterList)
showLetterSlots(gameWord,screenRect)
setCounters(moveMade,mistakeMade)
if letterSelected:
inGameWord = checkForLetter(gameWord, letter)
if inGameWord:
print(atIndex)
print(letter)
letterRen = wordFonts.render(letter,1,(0,255,0))
renderList[atIndex] = letterRen
print("The render list is: ", renderList)
renCount = 0
for r in lineRectList:
if renderList[renCount] != '?' :
screen.blit(renderList[renCount],((r.centerx-10),430))
if renCount <= len(gameWord):
renCount+=1
#update game screen
clock.tick(60)
pygame.display.update()
#========================== End Loop =============================================
pygame.quit()
我正在尋找快速的方法來處理重複,使他們與他們的比賽以及位圖混合。我已經放慢了所有循環的字母組合,所以我不確定我目前的getDupes
是否可行。
如果有人願意看這個,並提供一些意見,我非常感激。
謝謝你的時間。
請將您的代碼發佈範圍縮小到與您所問的問題相關的內容。 –
完成並完成,雖然所有downvotes雖然?這個地方在精英主義中是如此根深蒂固,以至於人們看不起那些試圖改進的新手? 對不起,我不想成爲一個屁股,但我對我發佈的每個問題的否定回答有點令人沮喪。我的意思是,我應該尋求其他方面的幫助嗎? – Wretch11
@ Wretch11歡迎來到堆棧溢出!降伏投票並不是對你的個人反思。你在這裏非常受歡迎。 Downvotes是表示該帖子需要改進的信號,而不是完全拒絕您提交的內容。現在,讓我們看看我們是否能夠弄清楚這裏發生了什麼。這裏有什麼期望的行爲?如果有重複的字符串,應該發生什麼? – Ares