Python Forum
how to work with variables changed in definitions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to work with variables changed in definitions
#1
Hi there,

I've used Python in school for like two years, so I really don't know what I'm doing. Excuse that please and don't worry about my messy code.

I'm currently coding to check a wether a click was performed on the right tile.
I've gotten so far, that I can spawn in images to give feedback to the user, but I also want to count the times, the user is clicking the wrong tile.

My problem is, that I defined "wrong" as following:

    def wrong (wx,wy):
        window.blit(wrongpng, (wx,wy))
        lcchange = -1
this is called upon in this:
(Yes, I'm using actual pixel numbers, but I will change that after testing(also ignore my suboptimal code))

    def a(av):
        mposx, mposy = pygame.mouse.get_pos()
        if av == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+16 and mposy <= ty+26:
                rectangle (tx+16, ty+16, 10, 10, GREY)
        if av == 2:
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 5:
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+16 and mposy <= ty+26:
                rectangle (tx+60, ty+16, 10, 10, GREY)
which is called in this:

    for av in range (6):
        a(av)
        av = av+1
because I wanted to change the Lifecounter in this:

     Lifecounter = Lifecounter + lcchange
     lcchange = 0
I've tested with print in every imaginable place and I've realized, that in the instant I return to the a for-loop that the lcchange variable is back to 0 although it changed it to -1 in the "wrong def"

So my question is:
How do I solve this properly?

Thanks in advance for any help!

Here is the whole Code for you to orientate in case of irritation:

import pygame, sys
from pygame.locals import *
import time
import random
import pygame
pygame.init()

# CLOCK ######################################

FPS = 30
clock = pygame.time.Clock()

# FENSTER ####################################

width = 1200
height = 600

Fenster = pygame.display.set_mode((width,height))
pygame.display.set_caption('Sudoku in Cool')

# VARIABLES ##################################

BLACK = (0,0,0)
GREY = (40,40,40)
WHITE = (255,255,255)

tx = 500
ty = 200

wx = 500
wy = 300

testpng = pygame.image.load('Test.png')
wrongpng = pygame.image.load('wrong.png')

def test (tx,ty):
    Fenster.blit(testpng, (tx,ty))

def rectangle(rex, rey, rew, reh, color):
    pygame.draw.rect(Fenster, color, [rex, rey, rew, reh])

# GAMELOOP ###################################

def game_loop():
    End = False

    def wrong (wx,wy):
        Fenster.blit(wrongpng, (wx,wy))
        lcchange = -1

    def a(av):
        mposx, mposy = pygame.mouse.get_pos()
        if av == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+16 and mposy <= ty+26:
                rectangle (tx+16, ty+16, 10, 10, GREY)
        if av == 2:
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+16 and mposy <= ty+26:
                wrong(wx,wy)
        if av == 5:
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+16 and mposy <= ty+26:
                rectangle (tx+60, ty+16, 10, 10, GREY)
    def b(bv):
        mposx, mposy = pygame.mouse.get_pos()
        if bv == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+27 and mposy <= ty+37:
                rectangle (tx+16, ty+27, 10, 10, GREY)
        if bv == 2:
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+27 and mposy <= ty+37:
                rectangle (tx+27, ty+27, 10, 10, GREY)
        if bv == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+27 and mposy <= ty+37:
                rectangle (tx+38, ty+27, 10, 10, GREY)
        if bv == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+27 and mposy <= ty+37:
                wrong(wx,wy)
        if bv == 5:
            mposx, mposy = pygame.mouse.get_pos()
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+27 and mposy <= ty+37:
                rectangle (tx+60, ty+27, 10, 10, GREY)
    def c(cv):
        mposx, mposy = pygame.mouse.get_pos()
        if cv == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+38 and mposy <= ty+48:
                wrong(wx,wy)
        if cv == 2:
            mposx, mposy = pygame.mouse.get_pos()
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+38 and mposy <= ty+48:
                wrong(wx,wy)
        if cv == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+38 and mposy <= ty+48:
                rectangle (tx+38, ty+38, 10, 10, GREY)
        if cv == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+38 and mposy <= ty+48:
                wrong(wx,wy)
        if cv == 5:
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+38 and mposy <= ty+48:
                rectangle (tx+60, ty+38, 10, 10, GREY)
    def d(dv):
        mposx, mposy = pygame.mouse.get_pos()
        if dv == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+49 and mposy <= ty+59:
                wrong(wx,wy)
        if dv == 2:
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+49 and mposy <= ty+59:
                rectangle (tx+27, ty+49, 10, 10, GREY)
        if dv == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+49 and mposy <= ty+59:
                rectangle (tx+38, ty+49, 10, 10, GREY)
        if dv == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+49 and mposy <= ty+59:
                rectangle (tx+49, ty+49, 10, 10, GREY)
        if dv == 5:
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+49 and mposy <= ty+59:
                rectangle (tx+60, ty+49, 10, 10, GREY)
    def e(ev):
        mposx, mposy = pygame.mouse.get_pos()
        if ev == 1:
            if mposx >= tx+16 and mposx <= tx+26 and mposy >= ty+60 and mposy <= ty+70:
                wrong(wx,wy)
        if ev == 2:
            if mposx >= tx+27 and mposx <= tx+37 and mposy >= ty+60 and mposy <= ty+70:
                rectangle (tx+27, ty+60, 10, 10, GREY)
        if ev == 3:
            if mposx >= tx+38 and mposx <= tx+48 and mposy >= ty+60 and mposy <= ty+70:
                wrong(wx,wy)
        if ev == 4:
            if mposx >= tx+49 and mposx <= tx+59 and mposy >= ty+60 and mposy <= ty+70:
                wrong(wx,wy)
        if ev == 5:
            if mposx >= tx+60 and mposx <= tx+70 and mposy >= ty+60 and mposy <= ty+70:
                rectangle (tx+60, ty+60, 10, 10, GREY)

    Fenster.fill (BLACK)
    
    tx = 500
    ty = 200
    test(tx,ty)
    mposx = 0
    mposy = 0
    Lifecounter = 3
    
# EVENTHANDLING ##############################

    while not End:

        for event in pygame.event.get():

            if event.type == QUIT:
                End = True

            if event.type == pygame.MOUSEBUTTONUP:
                mposx, mposy = pygame.mouse.get_pos()

                rectangle (wx, wy, 75, 75, BLACK)
                lcchange = 0
                
                for av in range (6):
                    a(av)
                    av = av+1
                for bv in range (6):
                    b(bv)
                    bv = bv+1
                for cv in range (6):
                    c(cv)
                    cv = cv+1
                for dv in range (6):
                    d(dv)
                    dv = dv+1
                for ev in range (6):
                    e(ev)
                    ev = ev+1
                Lifecounter = Lifecounter + lcchange
                lcchange = 0

        if Lifecounter == 0:
            End = True
        
        pygame.display.update()
        clock.tick(FPS)

game_loop()
pygame.quit ()
Reply
#2
You return it.

def wrong (wx,wy):
    window.blit(wrongpng, (wx,wy))
    lcchange = -1
    return lcchange
And then assign it where you call it: lcchange = wrong(wx, wy).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks so much!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 375 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  Passing parameters with arrays and array definitions michael_lwt 1 912 Jul-07-2022, 09:45 PM
Last Post: Larz60+
  How do the variables work? Oshadha 4 1,282 Apr-08-2022, 12:58 PM
Last Post: deanhystad
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,030 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  The behavior of tune model has changed Led_Zeppelin 5 4,360 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  how can a variable change if I haven't changed it? niminim 5 2,993 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 3,681 Jun-01-2019, 01:22 PM
Last Post: buran
  my list is being changed ivinjjunior 15 5,585 May-29-2019, 02:54 PM
Last Post: ivinjjunior
  RuntimeError: dictionary changed size during iteration anna 4 3,441 Feb-20-2019, 11:04 AM
Last Post: anna
  RuntimeError: dictionary changed size during iteration Skaperen 1 9,106 Dec-10-2018, 10:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020