2017-08-30 77 views
0

如何更改定義中的變量?我在互聯網上搜索,但無法找到幫助我的東西。我發現全局變量可以幫助,但我不知道如何使用它們,我有另一個問題,我的代碼甚至不反應我的「空間按鈕」。這裏是我的完整代碼,我知道他們寫不發佈所有的代碼,但也許這將幫助你,因爲我什至不明白什麼重要,什麼不是。(我知道我不應該使用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() 
+1

你應該看看[問]和[MCVE] – pvg

+0

啊,我怎麼能做一個代碼塊沒有在每行添加4個空格? – HelpMe

+1

對於代碼塊,您可以粘貼您的代碼,然後突出顯示代碼,然後按下看起來像'{}'或壓力'ctrl + k'的按鈕,這會將所有內容都移動到代碼塊中。 –

回答

0

爲了在問候你的說法的一些信息:

我發現全局變量可以幫助,但我不知道如何使用它們

下面是一個簡單的例子,說明如何使用全局變量n一個函數內部的ame空間。

我們需要在需要使用全局變量的每個函數內部使用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) 

爲你下一個問題:

代碼甚至不反應,我的「空間按鈕」

的問題與你有一個啓動功能命名的事實和一個變量。這不是一個好主意。此外,如果函數不是局部變量,則需要告訴函數在何處查找變量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") 

UPDATE:

一個用結合和帆布問題結合將無法在繪製對象工作,除非綁定是鼠標單擊。它與focus不適用於繪製對象。因此,您需要將空格鍵綁定到根窗口。或者如果使用框架,畫布所在的框架將會起作用。

變化:

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

要:

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

這應該與空格鍵解決您的問題。

+0

仍然沒有工作,我試過了,有很多人說,你應該使用全局變量(我不知道爲什麼)你能解釋這一點給我嗎?而thx – HelpMe

+0

@HelpMe編寫一個程序時,有時需要使用全局,而不使用類,某些種類的全局變量很好。這是一個關於全局變量的良好答案的鏈接,以及爲什麼它們被認爲是不好的。 [爲什麼全局變量是邪惡的?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) –

+0

@HelpMe,你究竟做了什麼。你是否改變了我提到的所有內容或僅改變了全局變量? –