Skip to content
Snippets Groups Projects
Commit b8c77e4c authored by Vincent N's avatar Vincent N
Browse files

Tentative de pong en turtle

Pas adapté aux élèves
parent db41acc0
Branches
No related tags found
No related merge requests found
from turtle import *
from math import sqrt
# Constantes
XPalette = 10
YPalette = 100
xTerrain = 300
yTerrain = 200
# Formes
paletteShape = ((0, 0), (0, YPalette), (XPalette, YPalette), (XPalette, 0))
register_shape('palette', paletteShape)
carreShape = ((0, 0), (0, XPalette), (XPalette, XPalette), (XPalette, 0))
register_shape('carre', carreShape)
wn = Screen()
wn.bgcolor('lightblue')
ball = Turtle(shape='circle')
ball.penup()
ball.shapesize()
ball.speed(0)
ball.goto(xTerrain//2, yTerrain//2)
ball.pendown()
ball.width(5)
for _ in range(2):
ball.right(90)
ball.forward(yTerrain)
ball.right(90)
ball.forward(xTerrain)
ball.penup()
ball.goto(0,0)
ball.setheading(45)
# turtle object
p1 = Turtle(shape='palette')
p1.penup()
p1.setheading(90)
p1.speed(0)
# p1.goto(-100, -50)
p2 = Turtle(shape='palette')
p2.penup()
p2.setheading(90)
p2.speed(0)
p2.goto(100, -50)
def check_collision(t1, t2):
x1, y1 = t1.position()
x2, y2 = t2.position()
print(abs(y1 - y2), abs(x1 - x2))
return abs(x1 + XPalette/2 - x2) <= (XPalette/2 + XPalette/2 ) and abs(y1 - y2) <= (YPalette/2 + XPalette/2 )
def deplacement():
ball.forward(10)
if check_collision(ball, p2):
print("coucou")
ball.backward(10)
if ball.heading<90:
ball.left(90)
else:
ball.left(-90)
elif check_collision(ball, p1):
print("coucou")
ball.backward(10)
if ball.heading<90:
ball.left(-90)
else:
ball.left(90)
wn.ontimer(deplacement, 500)
wn.onkey(lambda: p1.forward(10), 'w')
wn.onkey(lambda: p1.backward(10), 's')
wn.onkey(lambda: p2.forward(10), 'Up')
wn.onkey(lambda: p2.backward(10), 'Down')
ball.dot()
#
# wn.listen()
# deplacement()
#
# wn.mainloop()
import turtle
# Configuration de la fenêtre
win = turtle.Screen()
win.title("Pong en Python Turtle")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)
# Score
score_a = 0
score_b = 0
# Raquette de gauche
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=6, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)
# Raquette de droite
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=6, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)
# Balle
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.2
ball.dy = -0.2
# Affichage du score
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Joueur A: 0 Joueur B: 0", align="center", font=("Courier", 24, "normal"))
# Fonctions pour les mouvements
def paddle_a_up():
y = paddle_a.ycor()
if y < 250:
paddle_a.sety(y + 20)
def paddle_a_down():
y = paddle_a.ycor()
if y > -240:
paddle_a.sety(y - 20)
def paddle_b_up():
y = paddle_b.ycor()
if y < 250:
paddle_b.sety(y + 20)
def paddle_b_down():
y = paddle_b.ycor()
if y > -240:
paddle_b.sety(y - 20)
# Liaison clavier
win.listen()
win.onkeypress(paddle_a_up, "w")
win.onkeypress(paddle_a_down, "s")
win.onkeypress(paddle_b_up, "Up")
win.onkeypress(paddle_b_down, "Down")
# Boucle principale du jeu
while True:
win.update()
# Déplacement de la balle
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Collision avec les bords
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_a += 1
pen.clear()
pen.write(f"Joueur A: {score_a} Joueur B: {score_b}", align="center", font=("Courier", 24, "normal"))
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_b += 1
pen.clear()
pen.write(f"Joueur A: {score_a} Joueur B: {score_b}", align="center", font=("Courier", 24, "normal"))
# Collision avec les raquettes
if (340 < ball.xcor() < 350) and (paddle_b.ycor() - 50 < ball.ycor() < paddle_b.ycor() + 50):
ball.setx(340)
ball.dx *= -1
if (-350 < ball.xcor() < -340) and (paddle_a.ycor() - 50 < ball.ycor() < paddle_a.ycor() + 50):
ball.setx(-340)
ball.dx *= -1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment