python - Detect moving position in order to test collision using Python3 turtle -


import turtle import time import random  # randomizing ball's y scale pong_y = (random.random()) * 350 # setting screan size size_x = 1280 size_y = 760 turtle.setup(size_x, size_y)  # 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  = 0 down = 1 # starting p1's direction == p1direction = # starting p2's direction == down p2direction = down   # function def p1up():     global p1direction     p1direction =   # down functionw def p1down():     global p1direction     p1direction = down   # p2 function def p2up():     global p2direction     p2direction =   # 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 moovment of p1 def move_p1():     global square_size, time_step, p2_x, p2_y     # defining p1's x , 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 , y     player2_pos = player2.pos()     p2_x = player2_pos[0]     p2_y = player2_pos[1]      # choosing position 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 , -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 , -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() 

in code, reason, second collision, 1 player 1 (the right player) doesn't work, though i'm using same if condition both of players (if -80 < ball.pos()[0] - player1.pos()[0] < 80 , -80 < ball.pos()[1] - player1.pos()[1] < 80 , , if -80 < player2.pos()[0] - ball.pos()[0] < 80 , -80 < player2.pos()[1] - ball.pos()[1] < 80). (btw, in move_ball() function) please me!

looking on code, need go , read global statement , when is, , isn't needed. should take more advantage of turtle has offer, instead of:

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

we can use turtle distance() method:

if ball.distance(player1) < 80: 

and take advantage of turtle headings. here's simplification of code playable:

from random import randint, choice turtle import turtle, screen  # setting screen size size_x, size_y = 1280, 760 screen = screen() screen.setup(size_x, size_y)  # speed/grid square_size = 20  # randomizing 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 , 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  # & 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 & 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 on         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 , ball.distance(player1) < 80 or ball.heading() == 180 , 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() 

it still needs work real game, of course.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -