diff --git a/Jeux/Flappy bird/color_pixel.py b/Jeux/Flappy bird/color_pixel.py new file mode 100644 index 0000000000000000000000000000000000000000..d366ab94bf6a1c32e2f13b56c4e75e3ae895e527 --- /dev/null +++ b/Jeux/Flappy bird/color_pixel.py @@ -0,0 +1,58 @@ +import turtle +import time +from PIL import Image +import io + +img = LTurtle = HTurtle = LImg = HImg = None + +def get_background(): + global img, LTurtle, HTurtle , LImg , HImg + turtle.hideturtle() + screen = turtle.Screen() + LTurtle = screen.window_width() + HTurtle = screen.window_height() + screen_image = screen.getcanvas().postscript(colormode="color") + img = Image.open(io.BytesIO(screen_image.encode('utf-8'))) + LImg = img.size[0] + HImg = img.size[1] + turtle.showturtle() + + +def get_pixel_color(x, y): + X = (x+LTurtle//2)*LImg/LTurtle + Y = HImg-(y+HTurtle//2)*HImg/HTurtle + if 0<X<LImg and 0<Y<HImg: + color = img.getpixel((int(X), int(Y))) + return color + else: + return (0,0,0) + +if __name__ == "__main__": + # Créer une tortue + t = turtle.Turtle() + def rectangle(h, l): + t.begin_fill() + for _ in range(2): + t.fd(l) + t.left(90) + t.fd(h) + t.left(90) + t.end_fill() + + rectangle(200,200) + t.fd(100) + t.left(90) + t.fd(100) + + # Capturer l'écran à la position de la tortue + get_background() + + t0 = time.time() + print(get_pixel(-10,-10)) + print(get_pixel(10,10)) + print(get_pixel(210,210)) + + print(time.time()-t0) + + + diff --git a/Jeux/Flappy bird/flappy_bird.py b/Jeux/Flappy bird/flappy_bird.py new file mode 100644 index 0000000000000000000000000000000000000000..a4f5874177c268a1c54a97a34e24314040fc54b7 --- /dev/null +++ b/Jeux/Flappy bird/flappy_bird.py @@ -0,0 +1,74 @@ +from turtle import * +from random import randint + +from color_pixel import * + +### Constantes +LWorld = 1000 +HWorld = 300 +lObstacle = 10 +ratioObstacleEspacement = 15 +nObstacles = LWorld // (lObstacle*ratioObstacleEspacement) +hFenetre = lObstacle*5 + +### Background +def rectangle(h, l): + begin_fill() + for _ in range(2): + fd(l) + left(90) + fd(h) + left(90) + end_fill() + + + +def dessiner_obstacles(): + up();goto(-LWorld//2 + lObstacle*10, -HWorld//2);down() # Aller en bas à gauche + speed(0) + for _ in range(nObstacles): + hObstacle = randint(HWorld//5,(4*HWorld)//5) + + rectangle(hObstacle, lObstacle) + + up() + left(90) + fd(hObstacle+hFenetre) + right(90) + down() + + rectangle(HWorld- (hObstacle+hFenetre), lObstacle) + + up() + right(90) + fd(hObstacle+hFenetre) + left(90) + fd(lObstacle*ratioObstacleEspacement) + down() + + +#### Dessin + +# Background +Screen().setup(width=LWorld, height=HWorld) + +dessiner_obstacles() + +up();goto(-LWorld//2 , 0); left(90) # Aller au début + +# Init +shape('circle') +get_background() +onkey(lambda: fd(20), 'space') +listen() + + +# Boucle principale du jeu +while True: + update() + setx(xcor() + 1) + backward(1) + if get_pixel_color(xcor(),ycor()) != (255,255,255): + break; +print("Perdu") + \ No newline at end of file