how should i be binding spacebar to serve my purpose?

Viewed 51

I've been trying to figure how to set up pause and unpause function on my game that allows me to use space to pause and then unpause as well. I've been struggling mostly because I'm so new to coding. I feel like I'm not understanding the true and false or rather the binds. The code is as follows:

import random
from tkinter import *

def paused(window):

    PAUSED = False

    while True:
        pause(window)
        canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2, font=('consolas', 70), text="PAUSED", fill="white", tag='paused')
        window.keys.bind("<space>", paused)
        if not paused:
            paused = False
            window.update()
1 Answers

I am assuming you are using pygame. In pygame, you can listen to pygame.event and check if the space bar has been clicked.

PAUSED = False

while True: 
    for event in pygame.event.get(): 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE: 
                 PAUSED = not PAUSED # inverts PAUSED
    if not PAUSED: 
        draw() # update
    clock.tick(60)

Otherwise, you can use the keyboard module.

Related