A
A
Andrey Pankevich2021-12-18 19:12:35
Python
Andrey Pankevich, 2021-12-18 19:12:35

How to remove keypress for bind_all function in tkinter?

Newbie. I deal with the tkinter module, I make a simple toy where the tank model moves and shoots, but I ran into a problem: When you hold down the shoot button, in my case "BackSpace", a huge number of shots occur and the program starts to lag. Is there any switch for only 1 click or how to set the limit of processed clicks per second?

from tkinter import *
import tkinter as tk
import random
import time


win = Tk()
win.title('Мой первый шутер на Python')
win.resizable(False, False)
win.wm_attributes('-topmost', 1)

win_width = 800
win_height = 800
item_size = 20

virtual_x = win_width / item_size
virtual_y = win_height / item_size

player_color1 = 'cyan'

player_x = virtual_x // 2
player_y = virtual_y // 2

player_list = []

player_move_x = 0
player_move_y = 0

bullet_list = []

bullet_move_x = 0
bullet_move_y = 0


def paint_player(canvas, x, y):
    global player_list
    id1 = canvas.create_oval(x*item_size,y*item_size, x*item_size+item_size, y*item_size+item_size, fill=player_color1)
    id2 = canvas.create_rectangle(((x)*item_size)+5, ((y-0.9)*item_size)+5, (x*item_size + item_size)-5, ((y-0.7)*item_size + item_size)-5, fill='gray')
    player_list.append([x, y, id1])
    player_list.append([x, y, id2])

def delete_last_player():
    global player_list
    temp_bl_1 = player_list.pop(1)
    temp_bl_2 = player_list.pop(0)
    canvas.delete(temp_bl_1[2])
    canvas.delete(temp_bl_2[2])

def delete_last_bullet():
    global bullet_list
    temp_pl = bullet_list.pop(0)
    canvas.delete(temp_pl[2])

def player_move(event):
    global player_x
    global player_y
    global player_move_x
    global player_move_y
    if event.keysym == 'Up':
        player_move_x = 0
        player_move_y = -1
        delete_last_player()
    elif event.keysym == 'Down':
        player_move_x = 0
        player_move_y = 1
        delete_last_player()
    elif event.keysym == 'Left':
        player_move_x = -1
        player_move_y = 0
        delete_last_player()
    elif event.keysym == 'Right':
        player_move_x = 1
        player_move_y = 0
        delete_last_player()
    
    player_x = player_x + player_move_x
    player_y = player_y + player_move_y

    paint_player(canvas, player_x, player_y)

def paint_bullet(x, y):
    global player_x
    global bullet_list
    id = canvas.create_oval(((x)*item_size)+5, ((y-1)*item_size)+5, (x*item_size + item_size)-5, ((y-1)*item_size + item_size)-5, fill='Black')
    bullet_list.append([x, y, id])
    
def player_shoot(event):
    global player_y
    global player_x
    x = player_x
    y = player_y
    if event.keysym == 'BackSpace':
        for i in range(int(y), 0, -1):
            paint_bullet(x,i)
            win.update()
            time.sleep(0.05)
            delete_last_bullet()


canvas = Canvas(win, width= win_width, height= win_height, bd=0, highlightthickness=0)
canvas.pack()
win.update()

paint_player(canvas, player_x, player_y)

canvas.bind_all('<KeyPress-Left>', player_move)
canvas.bind_all('<KeyPress-Right>', player_move)
canvas.bind_all('<KeyPress-Up>', player_move)
canvas.bind_all('<KeyPress-Down>', player_move)
canvas.bind_all('<KeyPress-BackSpace>', player_shoot)

win = mainloop()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question