How can i change a variable in a definition? I've searched in the Internet but couldnt find something that helps me. I found out that global variables could help but i dont know how to use them and i've got another problem my code doesnt even react to my "Space-Button". Here's my full code i know they write dont post all of ur code but maybe this will help u because i dont even understand whats important and what not.(I know i shouldnt use tkinter and pygame together but im to lazy to code and im a noob that just knows 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()
Edit:
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()