无关代码修复后的代码错误使用Guizero构建的Python

如何解决无关代码修复后的代码错误使用Guizero构建的Python

因此,我正在用Python构建这个简单的游戏,所以我决定引入一个功能,该功能可以调节地图大小(吉泽华夫饼)。在第16行(waffle.height = waffle.width = Levelsizenumber.value =int(e)中,我添加了根据滑块(也是guizero部件)更改华夫饼尺寸的代码。这以某种方式破坏了第56行(pixel.color = '#ff0000')中的代码。我尝试使用十六进制颜色和认情况下接受的单词。散布函数中的所有其他行都起作用,除了实际绘制由散布算法确定的像素的那一行。有什么建议? (下面的代码

from guizero import Waffle,App,PushButton,Window,Text,Slider

"""This is supposed to be a simple minigame where you try to protect against fires"""


def startgame():
    startmenu.hide()
    app.show()
    print(Speednumber.value)
    app.repeat(int(Speednumber.value),propagate)


# Level size
def level(e):
    waffle.height = waffle.width = Levelsizenumber.value =int(e)


# Fire speed choose func
def firespeedchoose(a):
    Speednumber.value = a


# change "n",the ignition number


def ignitions(slider_value):
    n = int(slider_value)
    return n


# picks a Box's adjacent Box (includes diagonals): the x and y differences are less than 2,at least one of them is 1
def adjacent(pixel):  # and if it is neutral
    first_x = pixel.x
    first_y = pixel.y
    print("Yup,adjacent works")
    for second_pixel in pixels:
        second_x = second_pixel.x
        second_y = second_pixel.y
        x_dif = abs(first_x - second_x)
        y_dif = abs(first_y - second_y)
        if x_dif < 2 and y_dif < 2 and (x_dif == 1 or y_dif == 1) and second_pixel.color == "green":
            print("Adjacent is sending a value")
            return second_pixel
    return pixel  # else it returns the original pixel


# dictates the fire spread: picks a random cell adjacent to a fire cell and makes it a fire cell,for all fire cells
def propagate():
    print("propagate do be running")
    spreadables = []
    for source in pixels:
        print("The first loop in spreadable works")
        if source.color == "#ff0000":
            spreadables.append(adjacent(source))
            print("It reached that if stuff in propagate")
    for pixel in spreadables:
        print("Its coloring stuff")
        pixel.color = '#ff0000'


"""This ignites the fire"""


def lighter(slider_value):
    Ignitioncount.value = int(slider_value)
    print("Lighter is running")
    a = int(Ignitioncount.value) + 1
    for i in range(0,a):
        x1 = randint(0,int(waffle.height)-1)
        y1 = randint(0,int(waffle.height)-1)
        waffle.set_pixel(x1,y1,"#ff0000")


def menubuttonthing():
    waffle.set_all("green")
    app.hide()
    scoremenu.hide()
    startmenu.show()


"""this resets the whole dam thing"""


def resetthething():
    waffle.set_all("green")
    lighter(Ignitioncount.value)
    Speednumber.value = Speedslider.value
    print(Speednumber.value)
    app.show()
    scoremenu.hide()
    """i hid the score menu so that i Could use the function
     for both buttons(the score menu and the game itself)"""


"""this kinda protects against the fire"""


def protec(a,b):
    thepixelcolor = waffle.get_pixel(a,b)
    if thepixelcolor != "#ff0000":
        waffle.set_pixel(a,b,"blue")


"""score system"""


def scoresystem():
    score = 0
    for height in range(waffle.height):
        for lenght in range(waffle.width):
            if waffle.get_pixel(height,lenght) == "blue":
                score -= 1
            if waffle.get_pixel(height,lenght) == "green":
                score += 1
    app.hide()
    scoremenu.show()
    Actualscore.value = score


"""basic GUI setup"""
app = App(layout="grid",height=625,width=500)
# place waffle her if level size doesnt work

restbutton = PushButton(app,text="Reset",command=resetthething,grid=[1,2])
propagatebutton = PushButton(app,text="Propagate",command=propagate,3])
scorebutton = PushButton(app,text="score",command=scoresystem,4])
"""Start menu stuff"""
startmenu = Window(app,title="Welcome",height=500,width=550)
introtext = Text(startmenu,text="Welcome to this tiny random game")
Instructions = Text(startmenu,size=9,text="Instructions: The objective is to protect as much of the forest(green) ")
Instructions2 = Text(startmenu,text=" while using the least water (left mouse click that turns healthy forest into protected blue areas)")
Instructions3 = Text(startmenu,text="from the fire(red)")
Numberofignitions = Text(startmenu,size=10,text="Choose the difficulty")
Ignitioncount = Text(startmenu,text="0")
Ignitionslider = Slider(startmenu,start=0,end=5,command=lighter)
# Fire speed
Speed = Text(startmenu,text="Choose the speed")
Speednumber = Text(startmenu,text="750")
Speedslider = Slider(startmenu,start=250,end=2000,command=firespeedchoose)
# Waffle size
Levelsize = Text(startmenu,text="Choose the level size")
Levelsizenumber = Text(startmenu,text="20") #Works as a variable
Levelsizeslide = Slider(startmenu,end=40,command=level)
# Start
startbutton = PushButton(startmenu,text="Start",command=startgame)
# credits
credits = Text(startmenu,text="Made by The Cool Guy 468 and Kidplayer_666",size=8)
"""score stuff"""

scoremenu = Window(app)
scoretext = Text(scoremenu,text="your score is")
Actualscore = Text(scoremenu,text="0")
restbutton2 = PushButton(scoremenu,command=resetthething)
menubutton = PushButton(scoremenu,text="Menu",command=menubuttonthing)
# Waffle setup here for experimental purposes,aka,level size testing
waffle = Waffle(app,pad=0,1],height=int(Levelsizenumber.value),width=int(Levelsizenumber.value),color="green",command=protec,dim=25)
"""Pixel shuffler"""
pixels = []
for x in range(waffle.width):
    for y in range(waffle.height):
        pixels.append(waffle.pixel(x,y))
shuffle(pixels)
"""just starting the app"""
print(Speednumber.value)
startmenu.show()
scoremenu.hide()
app.hide()
app.display()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?