Restart Menu Issues

Here’s the link to my current version of the code: https://paste.pythondiscord.com/bikebowije.py
What I’m trying to fix

I have two problems:

  1. You can still play the game even with the game over menu
  2. I can’t figure out how to make a restart button that will reload everything

If anyone can help me with any of these problems it would be a great help to me, thanks!

You do not need to create GUI objects in the logic process.

def __init__(self):
    self.gameOverScreen = DirectDialog(frameSize=(-0.7, 0.7, -0.7, 0.7), adeScreen=0.4, relief=DGG.FLAT)
    self.gameOverScreen.hide()
    self.label = DirectLabel(text="Game Over!", parent=self.gameOverScreen, scale=0.1, pos=(0, 0, 0.2))
    self.label.hide()



def life(self, task):
    if (self.bar['value'] >= 0):
        self.bar['value'] -= 1
    else:
        self.gameOverScreen.show()
        self.label.show()
        
        # Here you need to delete all dynamic object transformations and 
        # create a default one with parameters, as during initialization.
        # Also delete or stop tasks.
        
        return task.done

    return task.again

This approach is very wrong, you create a loadSfx object each time you call “def Move(self):”

    def Move(self):
        self.WalkSound = self.loader.loadSfx("/Users/38167/PycharmProjects/viren/venv/walk.mp3")
        self.pandaActor.setY(self.pandaActor, -200)
        self.pandaActor.setPlayRate(2, "walk")
        walk = self.pandaActor.getAnimControl("walk")
        self.WalkSound.play()
        if not walk.isPlaying():
            self.pandaActor.loop("walk")

Creating instances must be placed in “def __init __ (self):”
You need to learn Python to get started.

I am well aware that every time I call move it defines it every time since there was no apparent problem I didn’t fix it(my bad that one was on me). As for your solution, that still doesn’t answer my 2nd question which was “I can’t figure out how to make a restart button that will reload everything.” I will try your first solution though.

Thanks!

I think I replied that rebooting is a reset of geometry, actors, animation, tasks to the starting position. Since your logic and as a programmer, you must understand how to return the world to its original state.

Imagine that the game consists of one model box, which is moved using the keys W, S, A, D, from the initial position at the start was 0, 0, 0, and health was 100. But some time passed, and you are in position 34, 456, 4 for example with health 83. How to reboot? it’s very simple to return 0, 0, 0 and set health to 100. That’s all.

Ok I understand, also I tried changing it like in the example. Now how do I actually solve my problems:

  1. You can still play the game even with the game over menu
  2. I can’t figure out how to make a restart button that will reload everything

because they’re still happening.

I think this will help you.

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.gui.DirectGui import DirectWaitBar, DirectButton, DirectLabel

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        
        self.disableMouse()
        self.camera.setPos(0, -20.0, 5)
        
        self.bar = DirectWaitBar(pos=(0, 0, -0.7), range=(100), value=(100))
        self.button = DirectButton(text = ("Start over?"), scale=.1, command=self.reset)
        self.button.hide()

        self.taskMgr.doMethodLater(0.09, self.life, "life-")
        
        self.label = DirectLabel(text="Game Over!", scale=0.1, pos=(0, 0, 0.2))
        self.label.hide()
        
        self.environ = self.loader.loadModel("environment")
        self.environ.reparentTo(self.render)
        
        self.box = self.loader.loadModel("box")
        self.box.reparentTo(self.render)
        
        camera.lookAt(self.box)
        camera.reparentTo(self.box)
        
        self.speed = 10.0
        
        self.move_forward = None
        self.move_back = None
        self.move_left = None
        self.move_right = None
        
        self.accept("w", self.setMove, ["forward", True])
        self.accept("w-up", self.setMove, ["forward", None])
        self.accept("s", self.setMove, ["back", True])
        self.accept("s-up", self.setMove, ["back", None])
        self.accept("a", self.setMove, ["left", True])
        self.accept("a-up", self.setMove, ["left",None])
        self.accept("d", self.setMove, ["right", True])
        self.accept("d-up", self.setMove, ["right",None])
        
        self.taskMgr.add(self.Move, "move")

    def setMove(self, name, vector):
        if name == "forward": self.move_forward = vector
        if name == "back": self.move_back = vector
        if name == "left": self.move_left = vector
        if name == "right": self.move_right = vector
            
            
    def Move(self, task):
        if self.move_forward != None:
            self.box.setY(self.box.getY()+self.speed*globalClock.getDt())
        if self.move_back != None:
            self.box.setY(self.box.getY()-self.speed*globalClock.getDt())
        if self.move_left != None:
            self.box.setX(self.box.getX()-self.speed*globalClock.getDt())
        if self.move_right != None:
            self.box.setX(self.box.getX()+self.speed*globalClock.getDt())
        return task.again

    def life(self, task):
        if (self.bar['value'] >= 0):
            self.bar['value'] -= 1
        else:
        
            taskMgr.remove("move")
            taskMgr.remove("life-")
            
            self.label.show()
            self.button.show()
            
            return task.done

        return task.again
        
    def reset(self):
    
        self.move_forward = None
        self.move_back = None
        self.move_left = None
        self.move_right = None
        self.box.setPos(0, 0, 0)
        self.bar['value'] = 100
        self.taskMgr.add(self.Move, "move")
        self.taskMgr.doMethodLater(0.09, self.life, "life-")
        self.button.hide()
        self.label.hide()



app = MyApp()
app.run()

globalClock.getDt() is underlined

And? It works for me.

but it doesn’t work for me? Also, why should I rewrite my entire code?

The name of the error?

Wait, sorry can you help me do jumping with my current code? I don’t want to have to copy and paste your code.

Well then, I don’t understand why I need to waste time, how to implement the jump, I told you again, I’m not going to write, because you are too lazy to copy :slight_smile:

I didn’t want you too, that’s why I said " I don’t want to have to copy and paste your code." literally one message above.
Also, I’m facing another problem:

  def life(self, task):
        if (self.bar['value'] >= 0):
            self.bar['value'] -= 10
        else:

            self.gameOverScreen.show()
            self.label.show()
            self.loader.unloadModel('models/environment')
            return task.done

        return task.again

why didn’t this delete the environment?

Did I tell you to learn python above? look for information about links.

Ok sorry I have another question, why is my base underlined? Whenever I try to do base. something it will always say unresolved reference base. Can you please help me?

Does my code throw an error? I do not know what is underlined, I have no idea.