Python Forum
How can i change the direction of the ball based on player's xcor and ycor collisions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i change the direction of the ball based on player's xcor and ycor collisions
#1
#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

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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Change elements of array based on position of input data Cola_Reb 6 2,110 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,759 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  Project Direction bclanton50 1 1,323 Jan-06-2022, 11:38 PM
Last Post: lucasbazan
Question How to understand the vector/direction mason321 0 1,102 Dec-14-2021, 10:57 PM
Last Post: mason321
  bouncing ball with variable collision points (in time) Zhaleh 1 2,362 Jul-24-2020, 02:54 PM
Last Post: Marbelous
  how can I change xcor and ycor of the turtle if i am using classes MohammedSohail 1 2,345 May-22-2020, 06:28 PM
Last Post: deanhystad
  Pointer in the right direction? Viking 5 2,722 Apr-22-2020, 06:14 PM
Last Post: Viking
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,313 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  Length and direction cosines of lines tarikrr 1 1,762 Nov-15-2019, 04:16 AM
Last Post: SheeppOSU
  Some direction needed Patriot1017 3 2,484 Sep-03-2019, 05:44 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020