Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard bind
#1
Hey everyone :D
So i need to make a little game for school and i have a problem but first i need to explain how this game will work :
So basically you'll have an ennemy (boxe type game btw) in front of you that will throw some punches at you and you have a window of (for now it's 1s) to respond by pressing a key my problem
right now is that the key isn't detected and i'm not too sure of what going wriong, could anyone help please ? (Sorry for the lack of comment, please ask if you have any question)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from tkinter import*
from random import*
import time
D,H,B,G=0,0,0,0
 
 
 
def AttackSelect(C):
    C=randint(1,4)
    C=1
    if C==1:
        txt = canvas.create_text(700, 80, text="Jab", font="Arial 16 italic", fill="black")
    elif C==2:
        txt = canvas.create_text(700, 450, text="Direct", font="Arial 16 italic", fill="red")
    elif C==3:
        S=randint(1,2)
        if S==1:
            txt = canvas.create_text(70, 450, text="Right Hook", font="Arial 16 italic", fill="blue")
        else:
            txt = canvas.create_text(1330, 450, text="Left Hook", font="Arial 16 italic", fill="blue")
    elif C==4:
        txt = canvas.create_text(700, 800, text="Uppercut", font="Arial 16 italic", fill="Orange")
    fenetre.update()
    return C
 
def Clear():
    canvas.create_rectangle(670, 70, 730, 90, fill="white", outline="white")
    canvas.create_rectangle(640, 440, 760, 460, fill="white", outline="white")
    canvas.create_rectangle(15, 440, 150, 460, fill="white", outline="white")
    canvas.create_rectangle(1260, 440, 1400, 460, fill="white", outline="white")
    canvas.create_rectangle(560, 790, 840, 810, fill="white", outline="white")
    fenetre.update()
 
 
def Gauche(event):
    global G
    print("gauche")
    G=1
     
 
def Droite(event):
    global D
    print("Droite")
    D=1
 
def Haut(event):
    print("haut")
    global H
    H=1
 
def Bas(event):
    return (True)
 
def AttackDefense(V):
    global G,D,H,B
    if C==1:
        max = 1
        start = time.time()
        while True:
            for i in range (500000):
                a=1
            remaining = max + start - time.time()
            print(D,G,H)
            if D==1 or G==1 or H==1:
                V=2
                G,D,H=0,0,0
                return V
            elif remaining <= 0:
                V=0
                return V
 
def Timer():
    max = 1
    start = time.time()
    while True:
        for i in range (500000):
            a=1
        remaining = max + start - time.time()
        if remaining <= 0:
            break
 
def jeu():
    global V,E,J,C
    while J>0 and E>0:
        C=AttackSelect(C)
        V=AttackDefense(V)
        Clear()
        print(V)
        if V==2:
            E=E-100
            canvas.create_rectangle(E+10,30,1400,40,fill="grey")
        elif V==0:
            J=J-100
            canvas.create_rectangle(J+10,820,1400,830,fill="grey")
        Timer()
 
         
fenetre = Tk()
canvas = Canvas(fenetre, width=1440, height=900, background='white')
PVJ = canvas.create_rectangle(10,820,1400,830,fill="red")
PVE = canvas.create_rectangle(10, 30, 1400, 40, fill="yellow")
canvas.pack()
fenetre.update()
canvas.bind('<Left>',Gauche)
canvas.bind('<Right>',Droite)
canvas.bind('<Up>',Haut)
canvas.bind('<Down>',Bas)
 
 
D,H,B,G=0,0,0,0
C=0
V=0
J=1390
E=1390
 
 
jeu()
fenetre.mainloop()  
Reply
#2
Hey, Gaiiden!

Just, instead of, canvas.bind use fenetre.bind. That's all :)

So, you should use bind method on Tk class, not on Canvas class.
Reply
#3
Thanks A LOT man i thought about this but for some reasons didn't try it...
Reply


Forum Jump:

User Panel Messages

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