[SOLVED] Need Help With Game Menu (Python)

Hello guys, I know there are a lot of topics about “Game Menu”, but I cant fix my problem
Heres the code :

The sketch:

import Classex #import Player
import random

width = 1000
height = width/2

def setup ():
    smooth()
    global player,collectable,enemy,gameState
    gameState = 0
    player = Classex.Player(width/2,height/2,50)
    collectable = Classex.Collectable()
    enemy = Classex.Enemy()
    size(width,height)
    frameRate(60)

def draw():
    if gameState == 0:
        background(0)
        menu()
    else:
        print("EXIT")
        background(0,255,0)
        game()
   

def game():
    print("PlayerX:" + str(player.x) + "PlayerY:" + str(player.y))
    print("CoinY:" + str(collectable.x) + "CoinY:" + str(collectable.y))
    background(0,255,0)
    textSize(20)
    fill(255)
    text("Score:" + str(player.score),920,480)
    player.logic()
    player.show()
    enemy.show()
    enemy.AI(player.x,player.y)
    collectable.show()
    allLogic()
    if keyPressed :
        player.keyPressed()
    
def allLogic():
    distance = dist(player.x ,player.y , collectable.x ,collectable.y )
    distance2 = dist(player.x ,player.y , enemy.x ,enemy.y )
    print(distance)
    if  distance < player.l / 2 + collectable.r/2 :
        collectable.x = random.randint(0,(width-collectable.r))
        collectable.y = random.randint(0,(height-collectable.r))
        player.score = player.score + 1
    if distance2 < (player.l / 2 + enemy.r/2) - 10 :
        end()
def menu():
    if mouseX > 420 and mouseY > 150 and mouseX < 420+123 and mouseY < 150+60:
        fill(255,255,0)            
    else:
        fill(0)
    if mousePressed :
        if mouseX > 420 and mouseY > 150 and mouseX < 420+123 and mouseY < 150+60:
              gameState = 1
              print(gameState)
    rect(420,150,123,60)
    fill(255)
    textSize(50)
    text("PLAY",420,200)
    noStroke()
    if mouseX > 430 and mouseY > 225 and mouseX < 430+90 and mouseY < 225+40:
        fill(0,255,0)            
    else:
        fill(0)
    if mousePressed :
        if mouseX > 430 and mouseY > 225 and mouseX < 430+90 and mouseY < 225+40:
            exit() 
    rect(430,225,90,40)
    fill(255)
    textSize(35)
    text("EXIT",440,260)

Classex:

import random

class Player:
    def __init__(self,x,y,l):
        self.x = x
        self.y = y
        self.l = l
        self.score = 0
    
    def show(self):
        rectMode(CENTER)
        stroke(0)
        strokeWeight(1)
        fill(0,0,255)
        rect(self.x,self.y,self.l,self.l,20)
        
    def keyPressed(self):
     if key == 'a':
         self.x = self.x - 1.5
     elif key == 'd':
         self.x = self.x + 1.5
     elif key == 's':
         self.y = self.y + 1.5
     elif key == 'w':
         self.y = self.y - 1.5
    def logic(self):
        self.x = constrain(self.x ,0 + self.l/2,width - self.l/2) 
        self.y = constrain(self.y ,0 + self.l/2,height - self.l/2) 
        
    
class Collectable:
    def __init__(self):
        self.x = random.randint(0,(width-20))
        self.y = random.randint(0,(height-50))
        self.r = 20
    
    def show(self):
        fill(255,255,0)
        stroke(0)
        strokeWeight(1)
        ellipse(self.x,self.y,self.r,self.r)

class Enemy:
    def __init__ (self):
        self.x = 50 
        self.y = 100
        self.speed = 1
        self.r = 50

        
     
    def AI(self,plx,ply):
        angle = atan2(ply - self.y,plx - self.x)
        self.x = cos(angle) * self.speed + self.x
        self.y = sin(angle) * self.speed + self.y
        

        
        
    def show(self):
        stroke(0)
        strokeWeight(1)
        fill(255,0,0)
        ellipse(self.x,self.y,self.r,self.r)

The problem is that my if doesnt work, when i press play, my console prints ‘1’(this means that my gameState changed), but I dont go in my else where theres the game starts
:(.
I think the problem may be in between lines 15-21, or my “PLAY” button. Only exit button works and it is the least important :disappointed:
(What I hate about Pyhton is the indentation, but I need to learn :snake: for school)
Hf :slight_smile:

1 Like

Every time we need to use any of the Python’s assignment operators =: :leftwards_arrow_with_hook:
Py.Processing.org/reference/assign.html

over a global variable within a function, we’ve gotta declare it w/ the keyword global 1st: :globe_with_meridians:
Py.Processing.org/reference/globals.html

Otherwise that variable is considered local to that function! :grimacing:

And that’s exactly what you’re doing to global variable gameState within function menu(). :cold_sweat:

It’s not declared w/ global! Therefore it’s treated as a local variable instead! :ghost:

4 Likes