#I am facing this problem i want to change the direction of the ball based on player's xcor and ycor collision
#If player hits the ball from a side example - north. The ball should move in the opposite direction
#If player hits the ball from a side example - north. The ball should move in the opposite direction
import turtle
import time
import math
# Create the screen
win = turtle.Screen()
win.bgcolor("green")
win.title("Football Game")
win.setup(700, 700)
win.tracer(0)
# player 1
player1 = turtle.Turtle()
player1.speed(0)
player1.shape("square")
player1.color("red")
player1.penup()
player1.goto(0, -200)
# player 2
player2 = turtle.Turtle()
player2.speed(0)
player2.shape("square")
player2.color("orange")
player2.penup()
player2.goto(0, 200)
# Variables area
speed = 20
#player1 goal
p1_goal = turtle.Turtle()
p1_goal.speed(0)
p1_goal.color("orange")
p1_goal.penup()
p1_goal.goto(-60, 345)
p1_goal.pendown()
p1_goal.setheading(90)
p1_goal.backward(55)
p1_goal.right(90)
p1_goal.forward(110)
p1_goal.left(90)
p1_goal.forward(55)
#Player 2 goal
p2_goal = turtle.Turtle()
p2_goal.speed(0)
p2_goal.color("red")
p2_goal.penup()
p2_goal.goto(60, -335)
p2_goal.pendown()
p2_goal.setheading(270)
p2_goal.backward(55)
p2_goal.right(90)
p2_goal.forward(110)
p2_goal.left(90)
p2_goal.forward(55)
#ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.shapesize(stretch_wid=0.9, stretch_len=0.9)
ball.penup()
ball.goto(0, 0)
# Functions area
def p1_go_up():
y = player1.ycor()
y += speed
player1.sety(y)
def p1_go_down():
y = player1.ycor()
y -= speed
player1.sety(y)
def p1_go_left():
x = player1.xcor()
x -= speed
player1.setx(x)
def p1_go_right():
x = player1.xcor()
x += speed
player1.setx(x)
def p2_go_up():
y = player2.ycor()
y -= speed
player2.sety(y)
def p2_go_down():
y = player2.ycor()
y += speed
player2.sety(y)
def p2_go_left():
x = player2.xcor()
x -= speed
player2.setx(x)
def p2_go_right():
x = player2.xcor()
x += speed
player2.setx(x)
def iscollision(t1, t2):
d = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2)) + math.sqrt(math.pow(t1.ycor() - t2.ycor(), 2))
if d < 20:
return True
else:
return False
def wall_collisions(player):
x, y = player.xcor(), player.ycor()
if x > 330:
x = 330
elif x < -330:
x = -330
elif y > 330:
y = 330
elif y < -330:
y = -330
player.setx(x), player.sety(y)
def ball_and_wall_collisions():
x, y = ball.xcor(), ball.ycor()
if x > 330:
x = 330
elif x < -330:
x = -330
elif y > 330:
y = 330
elif y < -330:
y = -330
ball.setx(x), ball.sety(y)
# Keyboard binding
# Player 1 controls
turtle.listen()
turtle.onkeypress(p1_go_up, "Up")
turtle.onkeypress(p1_go_down, "Down")
turtle.onkeypress(p1_go_left, "Left")
turtle.onkeypress(p1_go_right, "Right")
# Player 2 controls
turtle.onkeypress(p2_go_up, "w")
turtle.onkeypress(p2_go_down, "s")
turtle.onkeypress(p2_go_left, "a")
turtle.onkeypress(p2_go_right, "d")
while True:
time.sleep(0.01)
win.update()
# Collision between 2 players
if player1.distance(player2) < 15:
player1.forward(10)
player2.backward(10)
#Collisions between player and wall
wall_collisions(player1)
wall_collisions(player2)
#collisions between wall and ball
ball_and_wall_collisions()
if iscollision(player1, ball):
ball.forward(8)
