0

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()
HelpMe
  • 1
  • 4
  • 1
    You should take a look at [ask] and [MCVE] – pvg Aug 30 '17 at 15:57
  • Ah and how can i make a code block WITHOUT adding 4 spaces in every line? – HelpMe Aug 30 '17 at 15:57
  • 1
    For the code block you can paste your code in then highlight the code and then press the button that looks like `{}` or pres `ctrl+k` this will move everything into a code block. – Mike - SMT Aug 30 '17 at 15:59
  • @HelpMe Take a look at my updated answer. I do not have pygame installed on my PC but for your 2 issues you talked about in your question my answer should work for you. – Mike - SMT Aug 30 '17 at 16:11

1 Answers1

0

To provide some info in regards to your statement:

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

Here is a simple example of how you would work with variables in the global name space from inside of a function.

We need to use the global identifier inside of each function that needs to work with a global variable.

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)

For you next issue:

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

The issue is related to the fact you have a function named start and a variable. this is not a good idea. Also you need to tell your function where to look for the variable start if its not strictly a local variable to the function. You should instead change the function:

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

To:

# 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:

One issue with binds and canvas is bind will not work on drawn objects unless the bind is for a mouse click. It has to do with focus not being possible on drawn objects. So you will need to bind the space bar to the root window instead. Or the frame the canvas is in will work if you use frames.

Change:

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

To:

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

This should fix your issue with space bar.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Still doesnt work i tried it and there are many people saying that u shouldnt use global variables (i dont know why) could u explain this to me? And thx – HelpMe Aug 31 '17 at 15:02
  • @HelpMe The use of global is sometimes needed when writing a program without using classes and certain kinds of globals are fine. Here is a link to a good answer about global variables and why they are considered bad. [Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – Mike - SMT Aug 31 '17 at 15:07
  • @HelpMe and what exactly did you try. Did you change everything I mentioned or only the globals? – Mike - SMT Aug 31 '17 at 15:15
  • Everything what u writed in the second block of code. – HelpMe Aug 31 '17 at 15:57
  • @HelpMe when you press space are you at least getting the print statement to the console? – Mike - SMT Aug 31 '17 at 16:04
  • Nope it doesnt do anything i think something is wrong. – HelpMe Aug 31 '17 at 17:28
  • @HelpMe did you change `canvas.bind('', start)` to `canvas.bind('', change_start)`? You need to update the binding as well as the function. – Mike - SMT Aug 31 '17 at 18:02
  • Yes i did do this. – HelpMe Sep 03 '17 at 12:47
  • @HelpMe then add the changes you made to your question as an update at the bottom of the question. – Mike - SMT Sep 03 '17 at 15:37
  • I think i have to make a comment so u can see i wrote back sooo here. – HelpMe Sep 05 '17 at 14:28
  • @HelpMe I was testing your code a bit. I just added an update to my answer. Let me know if it helps. – Mike - SMT Sep 06 '17 at 15:32