bouncing.py

Created by schraf

Created on February 24, 2024

877 Bytes

Découvrez ma chaine YouTube (Jeux pour la NUMWORKS, Python…)


from kandinsky import *
from time import *
from random import randint

def init():
 global x,y,t,dx,dy,i   
 x, y = randint(20,300), randint(20,200)
 t = 4
 dx = -2 * randint(0,1) + 1
 dy = -2 * randint(0,1) + 1
 i = 0

red, green, blue = 255, 0, 0
colors = []
for j in range(0, 255 * 6 + 1, 10):
 if 255 >= j > 0: blue += 10
 elif 255 * 2 >= j > 255: red -= 10
 elif 255 * 3 >= j > 255 * 2: green += 10
 elif 255 * 4 >= j > 255 * 3: blue -= 10
 elif 255 * 5 >= j > 255 * 4: red += 10
 elif 255 * 6 >= j > 255 * 5: green -= 10
 colors.append((red, green, blue))

init()
while True:
 fill_rect(x,y,2*t,2*t,colors[i%154])
 sleep(.001)
 x += dx
 y += dy
 if x < -t or x + t > 320: 
    dx = -dx
    x += dx * t
 if y < -t or y + t > 222:
    dy = -dy
    y += dy * t
 i += 1
 if i % 100 == 0: t += 1
 if t > 120:
    fill_rect(0,0,320,222,colors[i%154])
    init()