Python 播放器的碰撞检测

Python 播放器的碰撞检测,python,turtle-graphics,pong,Python,Turtle Graphics,Pong,我的代码就像pong,只是我试着让它成为4个玩家。球和球员之间的碰撞是有效的,但它只朝一个方向移动。我想这样做,如果球击中球员的上边缘,球就会上升,如果球击中球员的一侧,球就会飞到那个方向。现在球只朝对角线方向移动 import turtle import math wn = turtle.Screen() wn.title("PMH HANDBALL") wn.bgcolor("Black") wn.setup(width=800, height=600) wn.tracer(0) def

我的代码就像pong,只是我试着让它成为4个玩家。球和球员之间的碰撞是有效的,但它只朝一个方向移动。我想这样做,如果球击中球员的上边缘,球就会上升,如果球击中球员的一侧,球就会飞到那个方向。现在球只朝对角线方向移动

import turtle
import math

wn = turtle.Screen()
wn.title("PMH HANDBALL")
wn.bgcolor("Black")
wn.setup(width=800, height=600)
wn.tracer(0)

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()- 
t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
    if distance < 50:
        return True
    else:
        return False

#Handball court top right
court = turtle.Turtle()
court.speed(0)
court.color("white")
court.hideturtle()
court.goto(400, 0)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)

#handball court top left
court.speed(0)
court.color("white")
court.hideturtle()
court.goto(-400, 0)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)

#Handball court bottom left
court.speed(0)
court.color("white")
court.hideturtle()
court.goto(-400, -400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)

#handball court bottom right
court.speed(0)
court.color("white")
court.hideturtle()
court.goto(0, -400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)
court.left(90)
court.forward(400)



#handball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(50, 50)
ball.dx = 2
ball.dy = -2


#player 1
player1 = turtle.Turtle()
player1.speed(0)
player1.shape("square")
player1.color("red")
player1.shapesize(stretch_wid=4, stretch_len=4)
player1.penup()
player1.goto(100, 200)

#player 2
player2 = turtle.Turtle()
player2.speed(0)
player2.shape("square")
player2.color("yellow")
player2.shapesize(stretch_wid=4, stretch_len=4)
player2.penup()
player2.goto(100, -200)

#player 3
player3 = turtle.Turtle()
player3.speed(0)
player3.shape("square")
player3.color("green")
player3.shapesize(stretch_wid=4, stretch_len=4)
player3.penup()
player3.goto(-100, 200)

#player 4
player4 = turtle.Turtle()
player4.speed(0)
player4.shape("square")
player4.color("blue")
player4.shapesize(stretch_wid=4, stretch_len=4)
player4.penup()
player4.goto(-100, -200)


#function
def player1_up():
    y = player1.ycor()
    y += 20
    player1.sety(y)

    x = player1.xcor()
    x += 20

def player1_down():
    y = player1.ycor()
    y -= 20
    player1.sety(y)

def player1_right():
    x = player1.xcor()
    x += 20
    player1.setx(x)

def player1_left():
    x = player1.xcor()
    x -= 20
    player1.setx(x)

def player2_up():
    y = player2.ycor()
    y += 20
    player2.sety(y)

def player2_down():
    y = player2.ycor()
    y -= 20
    player2.sety(y)

def player2_right():
    x = player2.xcor()
    x += 20
    player2.setx(x)

def player2_left():
    x = player2.xcor()
    x += -20
    player2.setx(x)

def player3_up():
    y = player3.ycor()
    y += 20
    player3.sety(y)

    x = player1.xcor()
    x += 20

def player3_down():
    y = player3.ycor()
    y -= 20
    player3.sety(y)

def player3_right():
    x = player3.xcor()
    x += 20
    player3.setx(x)

def player3_left():
     x = player3.xcor()
     x -= 20
     player3.setx(x)

def player4_up():
    y = player4.ycor()
    y += 20
    player4.sety(y)

    x = player4.xcor()
    x += 20

def player4_down():
    y = player4.ycor()
    y -= 20
    player4.sety(y)

def player4_right():
    x = player4.xcor()
    x += 20
    player4.setx(x)

def player4_left():
     x = player4.xcor()
     x -= 20
     player4.setx(x)

#keyboard bind
wn.listen()
wn.onkeypress(player1_up, "i")
wn.onkeypress(player1_down, "k")
wn.onkeypress(player1_right, "l")
wn.onkeypress(player1_left, "j")

wn.onkeypress(player2_up, "Up")
wn.onkeypress(player2_down, "Down")
wn.onkeypress(player2_right, "Right")
wn.onkeypress(player2_left, "Left")

wn.onkeypress(player3_up, "w")
wn.onkeypress(player3_down, "s")
wn.onkeypress(player3_right, "d")
wn.onkeypress(player3_left, "a")

wn.onkeypress(player4_up, "t")
wn.onkeypress(player4_down, "g")
wn.onkeypress(player4_right, "h")
wn.onkeypress(player4_left, "f")





#main loop that keeps window running
while True:
    wn.update()


    #boundary checking topright
    if player1.xcor() > 360:
        player1.goto(360, player1.ycor())

    if player1.xcor() < 25:
        player1.goto(40, player1.ycor())

    if player1.ycor() > 360:
        player1.goto(player1.xcor(), 360)

    if player1.ycor() < 35:
        player1.goto(player1.xcor(), 40)

#Boundary checking bottomright
    if player2.xcor() > 360:
        player2.goto(360, player2.ycor())

    if player2.xcor() < 25:
        player2.goto(40, player2.ycor())

    if player2.ycor() > -40:
        player2.goto(player2.xcor(), -40)

    if player2.ycor() < -360:
        player2.goto(player2.xcor(), -360)

#boundary checking topleft
    if player3.xcor() > -40:
        player3.goto(-40, player3.ycor())

    if player3.xcor() < -360:
        player3.goto(-360, player3.ycor())

    if player3.ycor() > 360:
        player3.goto(player3.xcor(), 360)

    if player3.ycor() < 35:
        player3.goto(player3.xcor(), 40)

#boundary checking bottomleft
    if player4.xcor() > -40:
        player4.goto(-40, player4.ycor())

    if player4.xcor() < -360:
        player4.goto(-360, player4.ycor())

    if player4.ycor() > -40:
    player4.goto(player4.xcor(), -40)

    if player4.ycor() < -360:
        player4.goto(player4.xcor(), -360)


#ball movement
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    #border checking for ball

    if ball.ycor() > 420:
        ball.goto(50,50)
        ball.dy *= -1


    if ball.ycor() < -420:
        ball.goto(50,50)
        ball.dy *= 1

    if ball.xcor() > 420:
        ball.goto(50, 50)
        ball.dx *= -1

    if ball.xcor() < -420:
        ball.goto(50, 50)
        ball.dx *= -1


    #player and ball collisons
    if isCollision(ball, player4):
        ball.dy *= -1
        ball.dx *= -1

    if isCollision(ball, player3):
        ball.dy *= -1
        ball.dx *= -1

    if isCollision(ball, player2):
        ball.dy *= -1
        ball.dx *= -1

    if isCollision(ball, player1):
        ball.dy *= -1
        ball.dx *= -1
导入海龟
输入数学
wn=tutle.Screen()
wn.标题(“PMH手球”)
wn.bgcolor(“黑色”)
wn.设置(宽度=800,高度=600)
wn.tracer(0)
def聚合(t1、t2):
距离=math.sqrt(math.pow(t1.xcor()-
t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
如果距离小于50:
返回真值
其他:
返回错误
#手球场右上角
court=乌龟
速度(0)
颜色(“白色”)
法院
后藤法院(400,0)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
#手球场左上角
速度(0)
颜色(“白色”)
法院
后藤法院(-400,0)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
#手球场左下角
速度(0)
颜色(“白色”)
法院
后藤法院(-400,-400)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
#手球场右下角
速度(0)
颜色(“白色”)
法院
后藤法院(0,-400)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
左(90)
前进法院(400)
#手球
ball=海龟。海龟()
球速(0)
球形(“方形”)
球。颜色(“白色”)
ball.penup()
球。后藤(50,50)
ball.dx=2
ball.dy=-2
#玩家1
player1=海龟。海龟()
播放器1.速度(0)
播放器1.形状(“方形”)
播放者1.颜色(“红色”)
player1.形状大小(拉伸宽度=4,拉伸长度=4)
player1.penup()
玩家1.转到(100200)
#玩家2
player2=海龟。海龟()
播放机2.速度(0)
播放器2.形状(“方形”)
播放者2.颜色(“黄色”)
player2.形状大小(拉伸宽度=4,拉伸长度=4)
player2.penup()
玩家2。转到(100,-200)
#玩家3
player3=海龟。海龟()
播放器3.速度(0)
播放器3.形状(“方形”)
播放者3.颜色(“绿色”)
player3.形状大小(拉伸宽度=4,拉伸长度=4)
player3.penup()
玩家3.转到(-100200)
#玩家4
player4=海龟。海龟()
播放器4.速度(0)
播放器4.形状(“方形”)
播放者4.颜色(“蓝色”)
player4.形状大小(拉伸宽度=4,拉伸长度=4)
player4.penup()
玩家4。转到(-100,-200)
#作用
def播放器1_up():
y=player1.ycor()
y+=20
玩家1.赛蒂(y)
x=player1.xcor()
x+=20
def播放器1_向下()
y=player1.ycor()
y-=20
玩家1.赛蒂(y)
def播放器1_right():
x=player1.xcor()
x+=20
player1.setx(x)
def播放器1_左()
x=player1.xcor()
x-=20
player1.setx(x)
def播放器2_up():
y=player2.ycor()
y+=20
玩家2.赛蒂(y)
def播放器2_向下()
y=player2.ycor()
y-=20
玩家2.赛蒂(y)
def播放器2_right():
x=player2.xcor()
x+=20
player2.setx(x)
def播放器2_左()
x=player2.xcor()
x+=-20
player2.setx(x)
def播放器3_up():
y=player3.ycor()
y+=20
玩家3.赛蒂(y)
x=player1.xcor()
x+=20
def播放器3_向下()
y=player3.ycor()
y-=20
玩家3.赛蒂(y)
def播放器3_right():
x=player3.xcor()
x+=20
player3.setx(x)
def播放器3_左()
x=player3.xcor()
x-=20
player3.setx(x)
def播放器4_up():
y=player4.ycor()
y+=20
球员4.赛蒂(y)
x=player4.xcor()
x+=20
def播放器4_向下()
y=player4.ycor()
y-=20
球员4.赛蒂(y)
def播放器4_right():
x=player4.xcor()
x+=20
player4.setx(x)
def播放器4_左()
x=player4.xcor()
x-=20
player4.setx(x)
#键盘绑定
听着
wn.onkeypress(播放者1向上,“i”)
wn.onkeypress(播放机1向下,“k”)
wn.onkeypress(播放器1_右,“l”)
wn.onkeypress(播放器1_左,“j”)
wn.onkeypress(播放器2向上,“向上”)
wn.onkeypress(播放机2向下,“向下”)
wn.onkeypress(播放器2_右,“右”)
wn.onkeypress(播放器2_左,“左”)
wn.onkeypress(播放者3向上,“w”)
wn.onkeypress(播放者3_向下,“s”)
wn.onkeypress(播放器3_右,“d”)
wn.onkeypress(播放器3_左,“a”)
wn.onkeypress(播放者4向上,“t”)
wn.onkeypress(播放机4向下,“g”)
wn.onkeypress(播放器4_右,“h”)
wn.onkeypress(播放器4_左,“f”)
#保持窗口运行的主循环
尽管如此:
wn.update()
#右上角边界检查
如果player1.xcor()大于360:
player1.goto(360,player1.ycor())
如果player1.xcor()小于25:
player1.goto(40,player1.ycor())
如果player1.ycor()大于360:
player1.goto(player1.xcor(),360)
如果player1.ycor()小于35:
player1.goto(player1.xcor(),40)
#右下边界检查
如果player2.xcor()大于360:
player2.goto(360,player2.ycor())
如果player2.xcor()小于25:
player2.goto(40,player2.ycor())
如果player2.ycor()>-40:
player2.goto(player2.xcor(),-40)
如果player2.ycor()<-360:
player2.goto(player2.xcor(),-360)
#左上角边界检查
如果player3.xcor()>-40:
player3.goto(-40,player3.ycor())
如果player3.xcor()小于-360:
player3.goto(-360,player3.ycor())
如果player3.ycor()大于360:
player3.goto(player3.xcor(),360)
如果player3.ycor()小于35:
player3.goto(player3.xcor(),40)
#边界检查左下角
如果player4.xcor()>-40:
player4.goto(-40,player4.ycor())
如果player4.xcor()小于-360:
player4.goto(-360,player4.ycor())
如果player4.ycor()>-40:
player4.goto(player4.xcor(),-40)
如果player4.ycor()<-360:
player4.goto(player4.xcor(),-360)
#球的运动
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
#球的边界检查
如果ball.ycor()大于420:
球。后藤(50,50)
ball.dy*=-1
如果ball.ycor()小于-4
from turtle import Screen, Turtle
from functools import partial
from random import choice

WIDTH, HEIGHT = 800, 600

CURSOR_SIZE = 20
PLAYER_SIZE = 80

def isCollision(t1, t2):
    return t1.distance(t2) < (PLAYER_SIZE + CURSOR_SIZE) / 2

# Event handlers
def player_up(player):
    player.sety(player.ycor() + 20)

def player_down(player):
    player.sety(player.ycor() - 20)

def player_right(player):
    player.setx(player.xcor() + 20)

def player_left(player):
    player.setx(player.xcor() - 20)

screen = Screen()
screen.title("PMH HANDBALL")
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('black')
screen.tracer(0)

# Handball court

court = Turtle()
court.hideturtle()
court.color('white')
court.width(4)

for x, y in ((0, -HEIGHT/2), (-WIDTH/2, -HEIGHT/2), (-WIDTH/2, 0), (0, 0)):
    court.goto(x, y)
    for _ in range(2):
        court.forward(WIDTH/2)
        court.left(90)
        court.forward(HEIGHT/2)
        court.left(90)

# Handball
ball = Turtle()
ball.shape('circle')
ball.color('white')

ball.penup()

ball.dx = 2
ball.dy = 2
ball.dx *= choice([-1, 1])
ball.dy *= choice([-1, 1])

# Player 1
player1 = Turtle()
player1.shape('square')
player1.color('red')
player1.shapesize(PLAYER_SIZE / CURSOR_SIZE)
player1.penup()
player1.goto(WIDTH/4, HEIGHT/4)

# Player 2
player2 = player1.clone()
player2.color('yellow')
player2.goto(WIDTH/4, -HEIGHT/4)

# Player 3
player3 = player1.clone()
player3.color('green')
player3.goto(-WIDTH/4, HEIGHT/4)

# Player 4
player4 = player1.clone()
player4.color('blue')
player4.goto(-WIDTH/4, -HEIGHT/4)

# Keyboard bindings
screen.onkeypress(partial(player_up, player1), 'i')
screen.onkeypress(partial(player_down, player1), 'k')
screen.onkeypress(partial(player_right, player1), 'l')
screen.onkeypress(partial(player_left, player1), 'j')

screen.onkeypress(partial(player_up, player2), 'Up')
screen.onkeypress(partial(player_down, player2), 'Down')
screen.onkeypress(partial(player_right, player2), 'Right')
screen.onkeypress(partial(player_left, player2), 'Left')

screen.onkeypress(partial(player_up, player3), 'w')
screen.onkeypress(partial(player_down, player3), 's')
screen.onkeypress(partial(player_right, player3), 'd')
screen.onkeypress(partial(player_left, player3), 'a')

screen.onkeypress(partial(player_up, player4), 't')
screen.onkeypress(partial(player_down, player4), 'g')
screen.onkeypress(partial(player_right, player4), 'h')
screen.onkeypress(partial(player_left, player4), 'f')

screen.listen()

# Main loop that keeps window running
while True:
    # boundary checking top right
    if player1.xcor() > WIDTH/2 - PLAYER_SIZE/2:
        player1.setx(WIDTH/2 - PLAYER_SIZE/2)
    elif player1.xcor() < PLAYER_SIZE/2:
        player1.setx(PLAYER_SIZE/2)

    if player1.ycor() > HEIGHT/2 - PLAYER_SIZE/2:
        player1.sety(HEIGHT/2 - PLAYER_SIZE/2)
    elif player1.ycor() < PLAYER_SIZE/2:
        player1.sety(PLAYER_SIZE/2)

    # boundary checking bottom right
    if player2.xcor() > WIDTH/2 - PLAYER_SIZE/2:
        player2.setx(WIDTH/2 - PLAYER_SIZE/2)
    elif player2.xcor() < PLAYER_SIZE/2:
        player2.setx(PLAYER_SIZE/2)

    if player2.ycor() > -PLAYER_SIZE/2:
        player2.sety(-PLAYER_SIZE/2)
    elif player2.ycor() < PLAYER_SIZE/2 - HEIGHT/2:
        player2.sety(PLAYER_SIZE/2 - HEIGHT/2)

    # boundary checking top left
    if player3.xcor() > -PLAYER_SIZE/2:
        player3.setx(-PLAYER_SIZE/2)
    elif player3.xcor() < PLAYER_SIZE/2 - WIDTH/2:
        player3.setx(PLAYER_SIZE/2 - WIDTH/2)

    if player3.ycor() > HEIGHT/2 - PLAYER_SIZE/2:
        player3.sety(HEIGHT/2 - PLAYER_SIZE/2)
    elif player3.ycor() < PLAYER_SIZE/2:
        player3.sety(PLAYER_SIZE/2)

    # boundary checking bottom left
    if player4.xcor() > -PLAYER_SIZE/2:
        player4.setx(-PLAYER_SIZE/2)
    elif player4.xcor() < PLAYER_SIZE/2 - WIDTH/2:
        player4.setx(PLAYER_SIZE/2 - WIDTH/2)

    if player4.ycor() > -PLAYER_SIZE/2:
        player4.sety(-PLAYER_SIZE/2)
    elif player4.ycor() < PLAYER_SIZE/2 - HEIGHT/2:
        player4.sety(PLAYER_SIZE/2 - HEIGHT/2)

    # Ball movement
    x, y = ball.position()
    x += ball.dx
    y += ball.dy

    # Border checking for ball

    if not - (WIDTH/2 + CURSOR_SIZE/2) < x < WIDTH/2 + CURSOR_SIZE/2:
        ball.home()
        ball.dx *= choice([-1, 1])
        ball.dy *= choice([-1, 1])
        continue
    elif not - (HEIGHT/2 + CURSOR_SIZE/2) < y < HEIGHT/2 + CURSOR_SIZE/2:
        ball.home()
        ball.dx *= choice([-1, 1])
        ball.dy *= choice([-1, 1])
        continue

    ball.setposition(x, y)

    # Player and ball collisions

    # Simplistic player and ball collision logic
    for player in [player1, player2, player3, player4]:
        if isCollision(ball, player):
            p_x, p_y = player.position()

            if abs(p_x - x) > abs(p_y - y):
                ball.dx *= -1
            else:
                ball.dy *= -1
            break

    screen.update()