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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| import tkinter as tk
import threading, time, random, math
keys = {}
def key_down(event):
key = event.keysym
keys[key] = True
def key_up(event):
key = event.keysym
if key == 'Escape':
root.destroy()
keys[key] = False
a = 5
def move():
while go:
KEYS = keys.copy()
for key in KEYS:
#First Player GREEN
if key == 'z' and KEYS[key]: #Up
if collision_bar(pts1, -a):
canvas.move(p1, 0, -a)
if key == 's' and KEYS[key]: #Down
if collision_bar(pts1, +a):
canvas.move(p1, 0, +a)
#Second Player RED
if key == 'Up'and KEYS[key]:
if collision_bar(pts2, -a):
canvas.move(p2, 0, -a)
if key == 'Down' and KEYS[key]:
if collision_bar(pts2, +a):
canvas.move(p2, 0, +a)
time.sleep(0.001)
def collision_bar(pts, add):
new_1 = max(0,pts[1]+add)
new_2 = min(700,pts[5]+add)
if new_1 != pts[1] and new_2 != pts[5] :
pts[1] = pts[3] = new_1
pts[5] = pts[7] = new_2
return True
else:
return False
v=4.0
v_orig=4.0
def collision_balle():
global go, v, add_x, add_y
list_angles = [x for x in range(5,360,50) if x not in (90, 180, 270)]
angle = math.radians(random.choice(list_angles))
#angle = 2.4
add_x = math.cos(angle)*v
add_y = -(math.sin(angle)*v)
pi = math.pi
while True:
x0, y0, x1, y1 = canvas.coords(ball)
r = (y1-y0)/2
center_y = r+y0
center_x = r+x0
#VICTOIRE
if x0 <= 0:
print('PLAYER 2 GAGNE')
go = False
return
elif x1 >= 1000:
print('PLAYER 1 GAGNE')
go = False
return
#TOP/BOTTOM
elif y0 <= 0 or y1 >= 700:
add_y = -add_y
#REBOND CONTRE JOUEUR 1
elif x0 <= pts1[2]:
if pts1[1] < center_y < pts1[5]: #REBOND NORMAL
add_x = max(add_x,-add_x)
threading.Thread(target=blink, args=(p1,"GREEN")).start()
elif center_y < pts1[1] < y1 : #REBOND BORD HAUT => REPART VERS LE HAUT
v += 0.5
add_x = math.cos(angle)*v
add_y = -(math.sin(angle)*v)
add_y = min(add_y,-add_y)
if center_x > pts1[2] : #REPART VERS LA DROITE AVANT QU'IL NE SOIT TROP TARD
add_x = max(add_x,-add_x)
threading.Thread(target=blink, args=(p1,"GREEN")).start()
elif y0 < pts1[5] < center_y: #REBOND BORD BAS => REPART VERS LE BAS
v += 0.5
add_x = math.cos(angle)*v
add_y = -(math.sin(angle)*v)
add_y = max(add_y,-add_y)
if center_x > pts1[2] : #REPART VERS LA DROITE AVANT QU'IL NE SOIT TROP TARD
add_x = max(add_x,-add_x)
threading.Thread(target=blink, args=(p1,"GREEN")).start()
#REBOND CONTRE JOUEUR 2
elif x1 >= pts2[0]:
if pts2[1] < center_y < pts2[5]: #REBOND NORMAL
add_x = min(add_x,-add_x)
threading.Thread(target=blink, args=(p2,"RED")).start()
elif center_y < pts2[1] < y1: #REBOND BORD HAUT => REPART VERS LE HAUT
v += 0.5
add_x = math.cos(angle)*v
add_y = -(math.sin(angle)*v)
add_y = min(add_y,-add_y)
if center_x < pts2[0] : #REPART VERS LA GAUCHE AVANT QU'IL NE SOIT TROP TARD
add_x = min(add_x,-add_x)
threading.Thread(target=blink, args=(p2,"RED")).start()
elif y0 < pts2[5] < center_y: #REBOND BORD BAS => REPART VERS LE BAS
v += 0.5
add_x = math.cos(angle)*v
add_y = -(math.sin(angle)*v)
add_y = max(add_y,-add_y)
if center_x < pts2[0]: #REPART VERS LA GAUCHE AVANT QU'IL NE SOIT TROP TARD
add_x = min(add_x,-add_x)
threading.Thread(target=blink, args=(p2,"RED")).start()
canvas.move(ball, add_x, add_y)
time.sleep(0.01)
def get_color():
for i, x in enumerate(range(int(v_orig*10),int(v_orig*10+90),5)): #4.0 -> 12.5 soit 18 vitesses
if v == round(x/10,1):
return rgbtohex(BALL_COLORS[i][0], BALL_COLORS[i][1], BALL_COLORS[i][2])
return rgbtohex(BALL_COLORS[-1][0], BALL_COLORS[-1][1], BALL_COLORS[-1][2])
def blink(player,original_color=None):
if player == ball:
original_color = get_color()
canvas.itemconfig(player, fill= "WHITE")
time.sleep(0.1)
canvas.itemconfig(player, fill=original_color)
R,G,B = (75,255,75)
def rgbtohex(r,g,b):
return f'#{r:02x}{g:02x}{b:02x}'
#18 nuances de couleurs du vert au rouge
BALL_COLORS = [ (R-x,G,B) for x in range(0,R+1,15) ] + [(255,G-x,B) for x in range(15,G+1-R,15)]
def create_circle(x, y, r): #center coordinates, radius
x0 = x - r
y0 = y - r
x1 = x + r
y1 = y + r
return canvas.create_oval(x0, y0, x1, y1, fill= rgbtohex(R,G,B), width=0)
root = tk.Tk()
root.geometry(f"1000x700+{int(1920/2-1000/2)}+{int(1080/2-700/2)}")
canvas = tk.Canvas(root, width=1000, height=700, bg='GREY60')
canvas.pack()
pts1 = [0,0, 50,0, 50,150, 0,150]
p1 = canvas.create_polygon(pts1, fill='GREEN')
pts2 = [950,0, 1000,0, 1000,150, 950,150]
p2 = canvas.create_polygon(pts2, fill='RED')
ball = create_circle(500,350,20)
root.bind('<KeyPress>', key_down)
root.bind('<KeyRelease>', key_up)
go = True
threading.Thread(target=move).start()
threading.Thread(target=collision_balle).start()
root.mainloop() |
Partager