在“找词”游戏中输入的代码输出不正确(使单词随机)

时间:2019-07-05 08:07:24

标签: python kivy

我正在借助“ python”和“ kivy”进行寻字游戏。 我做了PlotName(LabelObj,word,direction = 0,plot = -1)函数。 此功能可以在随机的10x10字网格中生成字。

Ex.PlotName(LabelObg,'ORANGE',1)-LabelObg是10x10网格图的kivy标签对象。 方向为0 =水平,1 =垂直,2 = crossup,3 = crossdown单词制作。

当类似单词的ABC和AXY试图放在相同的图中时,此功能首先对其进行检查和更正。

但是有时候我遇到问题,如下图所示。

enter image description here

新单词覆盖上一个单词。

我不知道为什么会遇到这个问题。(我认为这是嵌套循环的问题。)

这是我的完整游戏。 https://drive.google.com/open?id=1m0W0KDsvmKcTg68LHfVsFS4P_m2d-jn_

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
import random

def PlotName(LabelObj,word,direction=0,plot=-1):
    word = word
    direction = direction  # 0=horizontal , 1=vertical , 2=crossup , 3=crossdown
    if(plot!=-1):Rand=plot
    else:Rand = random.randint(0 ,99)
    newRand=Rand

#Checking of previous occupied places

    while (True and WordBox.Occupied_plots!=[]):
        bool=True
        if (direction == 0):

            while True:
                if ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand += 1
                    break

                elif ((10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand -= 1
                    break

                Rand = random.randint(0 ,99)

        if (direction == 1):

            while True:
                if ((10 - int(Rand / 10)) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand += 10
                    break

                elif ((10 - int((99 - Rand) / 10)) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand -= 10
                    break

                Rand = random.randint(0, 99)

        if (direction == 2):

            while True:
                if ((10 - int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand += 10
                        Rand += 1
                    break

                elif ((10 - int((99 - Rand) / 10)) >= len(word)) and (
                        (10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand -= 10
                        Rand -= 1
                    break

                Rand = random.randint(0, 99)

        if (direction == 3):

            while True:
                if ((int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand -= 10
                        Rand += 1
                    break

                elif ((int((99 - Rand) / 10)) >= len(word)) and (
                        (10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                    for i in word:
                        if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
                            bool = False
                        Rand += 10
                        Rand -= 1
                    break

                Rand = random.randint(0, 99)

        if(bool):
            break
        else:
            Rand = random.randint(0 ,99)
            newRand=Rand

#After Checking -- Put Word --

    Rand=newRand
    if (direction == 0):

        while True:
            if ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand += 1
                break

            elif ((10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand -= 1
                break

            Rand = random.randint(0 ,99)

    if (direction == 1):


        while True:
            if ((10 - int(Rand / 10)) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand += 10
                break

            elif ((10 - int((99 - Rand) / 10)) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand -= 10
                break

            Rand = random.randint(0 ,99)


    if (direction == 2):

        while True:
            if ((10 - int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand += 10
                    Rand += 1
                break

            elif ((10 - int((99 - Rand) / 10)) >= len(word)) and (
                    (10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand -= 10
                    Rand -= 1
                break

            Rand = random.randint(0 ,99)

    if (direction == 3):

        while True:
            if ((int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand -= 10
                    Rand += 1
                break

            elif ((int((99 - Rand) / 10)) >= len(word)) and (
                    (10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
                for i in word:
                    LabelObj[Rand].text = i
                    LabelObj[Rand].color = (1, 0, 0, 1)
                    WordBox.Occupied_plots.append(Rand)
                    Rand += 10
                    Rand -= 1
                break

            Rand = random.randint(0 ,99)

class MainScreen(Screen):
    def switch(self,direction,screen):
        self.manager.current=screen

class GameScreen(Screen):
    pass

class Manager(ScreenManager):
    pass

class RootWid(FloatLayout):
    player='player'
    def inpChange1(self,x):
        if(x.text=='player'):
            x.text=''
        elif(x.text==''):
            x.text='player'

    def ok_click(self):
        self.parent.switch('left','game_scr')
        RootWid.player=self.ids['inp1']

class RootWid2(FloatLayout):
    pass

class WordBox(FloatLayout):
    Myself=object
    Occupied_plots=[]

    def myself(self):
        WordBox.Myself=self
        original_Dictionary = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                               'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
        LabelObj=[]
        y=0
        x=0
        for i in range(100):
            if(x>0.99):
                x=0
                y+=0.1

            Latter=random.randint(0,25)
            labelobj=Label(text='%',pos_hint={'x':x,'y':y},size_hint=(0.1,0.1))
            x+=0.1

            self.add_widget(labelobj)
            LabelObj.append(labelobj)

        PlotName(LabelObj,'BANANA',0,13)
        PlotName(LabelObj,'ORANGE',3,17)

        return ''

class NameBox(FloatLayout):
    names=['','','','','','','','','','']
    Myself=object

    def myself(self):
        NameBox.Myself=self
        return ''

    @staticmethod
    def get_names():
        for i in range(10):
            NameBox.names[i]=NameBox.Myself.ids['lbl_'+str(i+1)].text
        return NameBox.names
    @staticmethod
    def set_names(name,index=1):
        NameBox.Myself.ids['lbl_'+str(index)].text=name

class MyCanvas(FloatLayout):
    pass

class Credit(FloatLayout):
    pass

class Mygame(App):

    def build(self):
        return Manager()

Mygame().run()

1 个答案:

答案 0 :(得分:0)

我尝试了几个小时。 我进入每个if-else-loop代码。 终于找到了我的答案。

每次Rand从random.randint(0,99)获取新值时,我都必须将Rand的值分配给newRand。

例如

            Rand = random.randint(0 ,99)
            newRand=Rand

感谢您抽出时间阅读我的QnA。 :)