如何更改定义中的变量

2024-03-29 14:47:45 发布

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

如何更改定义中的变量?我在网上搜索过,但找不到对我有帮助的东西。我发现全局变量可能有帮助,但我不知道如何使用它们,我还有一个问题,我的代码甚至对我的“空格键”没有反应。这是我的完整代码,我知道他们不会写你所有的代码,但也许这会帮助你,因为我甚至不明白什么是重要的,什么不是。(我知道我不应该一起使用tkinter和pygame,但我懒得代码,我只是一个noob,只知道tkinter)。你知道吗

from pygame import *
import pygame as pygame
from tkinter import *
import tkinter as tk
from time import sleep

#---------------------------------------

HEIGHT = 500

WIDTH = 500

window = Tk()

window.title("Test")

canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="grey")

canvas.pack(expand=False)

#---------------------------------------

a = (5, 5)
b = (85, 85)
c = (415, 415)
d = (495, 495)
e = (5, 415)
f = (85, 495)

player = canvas.create_rectangle([a, b], tags="obj1Tag", fill = "red4")
finish = canvas.create_rectangle([c, d], tags="obj2Tag", fill = "green4")
arrow = canvas.create_rectangle([e, f], tags="obj3Tag", fill = "blue4")

#---------------------------------------

mainloopon = True
start = False
start2 = False
startwindow = True
pms = 0.015625
arrow_direction = ("None")

#---------------------------------------

def start(event):
    start = True
    print ("start")

canvas.bind('<space>', start)

#---------------------------------------

colors = {"red":"blue4",
          "blue4":"yellow",
          "yellow":"green",
          "green":"red"}


def onObjectClick(event):
    current_color = canvas.itemcget(arrow, "fill")
    print (canvas.itemconfig(arrow, "fill"))
    canvas.itemconfig(arrow, fill=colors[current_color])

canvas.tag_bind(arrow, '<ButtonPress-1>', onObjectClick)

#---------------------------------------

while mainloopon == True:

    if startwindow == True:
        window.update()

        if start2 == True:
            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'blue4'):
                arrow_direction = ("right")

            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'red'):
                arrow_direction = ("left")


            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'yellow'):
                arrow_direction = ("up")

            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'green'):
                arrow_direction = ("down")

    if start == True:
        canvas.move(player, 0, [pms])
        window.update()
        #sleep(0.0001)
        playerc = canvas.coords(player)
        arrowc = canvas.coords(arrow)

        if playerc == arrowc:
            start = False
            start2 = True

    if arrow_direction == ("right"):
        canvas.move(player, [pms], 0)
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("left"):
        canvas.move(player, [-pms], 0)
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("up"):
        canvas.move(player, 0, [-pms])
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("down"):
        canvas.move(player, 0, [pms])
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

window.mainloop()

编辑:

WIDTH = 500

window = Tk()

window.title("Test")

canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="grey")

canvas.pack(expand=False)

#---------------------------------------

a = (5, 5)
b = (85, 85)
c = (415, 415)
d = (495, 495)
e = (5, 415)
f = (85, 495)

player = canvas.create_rectangle([a, b], tags="obj1Tag", fill = "red4")
finish = canvas.create_rectangle([c, d], tags="obj2Tag", fill = "green4")
arrow = canvas.create_rectangle([e, f], tags="obj3Tag", fill = "blue4")

#---------------------------------------

mainloopon = True
start = False
start2 = False
startwindow = True
pms = 0.015625
arrow_direction = ("None")

#---------------------------------------

def change_start(event):
    global start
    start = True
    print (start)

canvas.bind('<space>', change_start)

#---------------------------------------

colors = {"red":"blue4",
          "blue4":"yellow",
          "yellow":"green",
          "green":"red"}


def onObjectClick(event):
    current_color = canvas.itemcget(arrow, "fill")
    print (canvas.itemconfig(arrow, "fill"))
    canvas.itemconfig(arrow, fill=colors[current_color])

canvas.tag_bind(arrow, '<ButtonPress-1>', onObjectClick)

#---------------------------------------

while mainloopon == True:

    if startwindow == True:
        window.update()

    if start2 == True:
        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'blue4'):
            arrow_direction = ("right")

        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'red'):
            arrow_direction = ("left")


        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'yellow'):
            arrow_direction = ("up")

        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'green'):
            arrow_direction = ("down")

    if start == True:
        canvas.move(player, 0, [pms])
        #window.update()
        #sleep(0.0001)
        playerc = canvas.coords(player)
        arrowc = canvas.coords(arrow)

        if playerc == arrowc:
            start = False
            start2 = True

    if arrow_direction == ("right"):
        canvas.move(player, [pms], 0)
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("left"):
        canvas.move(player, [-pms], 0)
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("up"):
        canvas.move(player, 0, [-pms])
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("down"):
        canvas.move(player, 0, [pms])
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

window.mainloop()

Tags: trueifcoordswindowfillstartcanvasplayer
1条回答
网友
1楼 · 发布于 2024-03-29 14:47:45

为您的陈述提供一些信息:

I found out that global variables could help but i dont know how to use them

下面是一个简单的示例,说明如何从函数内部处理全局名称空间中的变量。你知道吗

我们需要在每个需要处理全局变量的函数中使用global标识符。你知道吗

some_var = "test"
some_var2 = "test2"

def change_global_var():
    global some_var, some_var2 # any variable names go here separated by a comma
    some_var = "Test Complete"
    some_var2 = "Test Complete"

change_global_var()

print(some_var, some_var2)

下一期:

code doesn't even react to my "Space-Button"

这个问题与您有一个名为start的函数和一个变量有关。这不是个好主意。如果变量start不是函数的局部变量,那么还需要告诉函数在哪里查找它。您应该改为更改函数:

def start(event):
    start = True
    print ("start")

收件人:

# can be any function name just don't use names you have already used.
def change_start(event):

    global start # tells the function that start is in the global namespace
    start = True
    print ("start")

更新:

binds和canvas的一个问题是bind不能在绘制的对象上工作,除非绑定用于鼠标单击。它与focus在绘制对象上不可能有关。因此需要将空格键绑定到根窗口。或者画布所在的框架将在使用框架时起作用。你知道吗

更改:

canvas.bind('<space>', change_start)

收件人:

window.bind('<space>', change_start)

这将解决你的问题与空格键。你知道吗

相关问题 更多 >