检测移动位置以便使用Python3龟测试碰撞

时间:2017-08-15 09:06:14

标签: python python-3.x turtle-graphics

import turtle
import time
import random

# randomizing the ball's Y scale
pong_y = (random.random()) * 350
# setting screan size
SIZE_X = 1280
SIZE_Y = 760
turtle.setup(SIZE_X, SIZE_Y)

# the speed/grid
SQUARE_SIZE = 20

# refresh every 100 miliseconds
TIME_STEP = 100

TIME_STEP_BALL = 10

# cloning turtles
player1 = turtle.clone()
player2 = turtle.clone()
ball = turtle.clone()

ball.ht()
ball.penup()
ball.fillcolor("red")
ball.shape("circle")
turtle.ht()

# setting starting players position
player1.penup()
player1.shape("square")
player1.ht()
player1.goto(SIZE_X / 2 - SQUARE_SIZE - 2, 0)
player1.st()
player2.penup()
player2.shape("square")
player2.ht()
player2.goto(-SIZE_X / 2 + SQUARE_SIZE + 2, 0)
player2.st()
ball.goto(- SIZE_X / 2 + SQUARE_SIZE, 0)
ball.st()
# defining ARROW keys
UP_ARROW = "Up"
DOWN_ARROW = "Down"

# giving values

UP = 0
DOWN = 1
# starting p1's direction == up
p1direction = UP
# starting p2's direction == down
p2direction = DOWN


# up function
def p1up():
    global p1direction
    p1direction = UP


# down functionw
def p1down():
    global p1direction
    p1direction = DOWN


# p2 up function
def p2up():
    global p2direction
    p2direction = UP


# p2 down function
def p2down():
    global p2direction
    p2direction = DOWN


# detecting key press of p1
turtle.onkeypress(p1up, "Up")
turtle.onkeypress(p1down, "Down")
# detecting keypress of p2
turtle.onkeypress(p2up, "w")
turtle.onkeypress(p2down, "s")

turtle.listen()


# making the moovment of p1
def move_p1():
    global SQUARE_SIZE, TIME_STEP, p2_x, p2_y
    # defining p1's x and y
    player1_pos = player1.pos()
    p1_x = player1_pos[0]
    p1_y = player1_pos[1]
    if p1direction == UP:
        player1.goto(p1_x, p1_y + SQUARE_SIZE)
    if p1direction == DOWN:
        player1.goto(p1_x, p1_y - SQUARE_SIZE)

    turtle.ontimer(move_p1, TIME_STEP)


# p2 function
def move_p2():
    global SQUARE_SIZE, TIME_STEP, p1_x, p1_y, player1_pos
    # defining p2's x and y
    player2_pos = player2.pos()
    p2_x = player2_pos[0]
    p2_y = player2_pos[1]

    # choosing which position to go
    if p2direction == UP:
        player2.goto(p2_x, p2_y + SQUARE_SIZE)
    if p2direction == DOWN:
        player2.goto(p2_x, p2_y - SQUARE_SIZE)

    turtle.ontimer(move_p2, TIME_STEP)


def move_ball():
    global pong_y
    ball_pos = ball.pos()
    ball_x = ball_pos[0]
    ball_y = ball_pos[1]
    if ball_pos[0] < -SIZE_X / 2:
        print("hey")

    if -80 < ball.pos()[0] - player1.pos()[0] < 80 and -80 < ball.pos()[1] - player1.pos()[1] < 80:
        ball.goto(ball_x - SIZE_X, pong_y)
    if -80 < player2.pos()[0] - ball.pos()[0] < 80 and -80 < player2.pos()[1] - ball.pos()[1] < 80:
        print("suc")
        ball.goto(ball_x + SIZE_X, pong_y)


move_p2()
move_p1()
move_ball()
turtle.mainloop()

在我的代码中,出于某种原因,即使我对两个玩家使用相同的if条件,第二次碰撞,玩家1(右玩家)的碰撞也不起作用({ {1}}和if -80 < ball.pos()[0] - player1.pos()[0] < 80 and -80 < ball.pos()[1] - player1.pos()[1] < 80。(顺便说一下,它在if -80 < player2.pos()[0] - ball.pos()[0] < 80 and -80 < player2.pos()[1] - ball.pos()[1] < 80)函数中)请帮助我!

1 个答案:

答案 0 :(得分:0)

查看您的代码,您需要返回并阅读global语句以及何时,并且不需要。你应该更多地利用龟提供的东西,而不是:

if -80 < ball.pos()[0] - player1.pos()[0] < 80 and -80 < ball.pos()[1] - player1.pos()[1] < 80:

我们可以使用turtle distance()方法:

if ball.distance(player1) < 80:

同样利用龟头标题。这是我对您的代码的简化,实际上可以播放:

from random import randint, choice
from turtle import Turtle, Screen

# setting screen size
SIZE_X, SIZE_Y = 1280, 760
screen = Screen()
screen.setup(SIZE_X, SIZE_Y)

# the speed/grid
SQUARE_SIZE = 20

# randomizing the ball's Y scale
pong_y = randint(SQUARE_SIZE - SIZE_Y / 2, SIZE_Y / 2 - SQUARE_SIZE)

# refresh every 100 milliseconds
TIME_STEP = 100

# create turtles and set player starting positions
player1 = Turtle('square', visible=False)
player1.speed('fastest')
player1.penup()
player1.setx(SIZE_X / 2 - SQUARE_SIZE * 2)
player1.setheading(choice([-90, 90]))
player1.showturtle()

player2 = Turtle('square', visible=False)
player2.speed('fastest')
player2.penup()
player2.setx(SQUARE_SIZE * 2 - SIZE_X / 2)
player2.setheading(choice([-90, 90]))
player2.showturtle()

ball = Turtle('circle', visible=False)
ball.speed('fastest')
ball.penup()
ball.fillcolor('red')
ball.goto(0, pong_y)
ball.setheading(choice([180, 0]))
ball.showturtle()

# giving values
UP, DOWN = 0, 1

# up & down functions
def p1up():
    screen.onkeypress(None, 'Up')
    player1.setheading(90)
    screen.onkeypress(p1up, 'Up')

def p1down():
    screen.onkeypress(None, 'Down')
    player1.setheading(-90)
    screen.onkeypress(p1down, 'Down')

# p2 up & down functions
def p2up():
    screen.onkeypress(None, 'w')
    player2.setheading(90)
    screen.onkeypress(p2up, 'w')

def p2down():
    screen.onkeypress(None, 's')
    player2.setheading(-90)
    screen.onkeypress(p2down, 's')

# detect key press of p1
screen.onkeypress(p1up, 'Up')
screen.onkeypress(p1down, 'Down')

# detect keypress of p2
screen.onkeypress(p2up, 'w')
screen.onkeypress(p2down, 's')

screen.listen()

# move objects
def move_p1():
    player1.forward(SQUARE_SIZE)
    screen.ontimer(move_p1, TIME_STEP)

def move_p2():
    player2.forward(SQUARE_SIZE)
    screen.ontimer(move_p2, TIME_STEP)

def move_ball():
    global pong_y

    ball_x = ball.xcor()

    if ball_x < -SIZE_X / 2 or ball_x > SIZE_X / 2:  # out of bounds, start over
        ball.hideturtle()
        pong_y = randint(SQUARE_SIZE - SIZE_Y / 2, SIZE_Y / 2 - SQUARE_SIZE)
        ball.goto(0, pong_y)
        ball.setheading(choice([180, 0]))
        ball.showturtle()

    elif ball.heading() == 0 and ball.distance(player1) < 80 or ball.heading() == 180 and ball.distance(player2) < 80:
        ball.setheading(180 - ball.heading())  # player hit ball, reverse direction

    ball.forward(SQUARE_SIZE)

    screen.ontimer(move_ball, TIME_STEP)

move_p2()
move_p1()
move_ball()

screen.mainloop()

当然,仍然需要努力成为真正的游戏。