furas

Python - turtle change direction on click

Jun 13th, 2018
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from turtle import *
  2. import turtle
  3.  
  4. #screen
  5. screen=turtle.Screen()
  6. screen.bgcolor('lightyellow')
  7. #boundary square
  8. bound=turtle.Turtle()
  9. bound.up()
  10. bound.setposition(-300,-300)
  11. bound.down()
  12. bound.pensize(5)
  13. bound.speed(0)
  14. for side in range(4):
  15.     bound.forward(600)
  16.     bound.left(90)
  17. bound.hideturtle()
  18. #creates players
  19. player1=turtle.Turtle()
  20. player2=turtle.Turtle()
  21. #creates shape
  22. player1.shape('turtle')
  23. player1.speed(0)
  24. player2.shape('square')
  25.  
  26. #not to draw and position
  27. player1.penup()
  28. player2.penup()
  29. player1.left(90)
  30. player2.right(90)
  31.  
  32.  
  33. #movement and buttons
  34. #L
  35. pen=turtle.Turtle()
  36. pen1=turtle.Turtle()
  37.  
  38. pen.hideturtle()
  39. pen.speed(0)
  40. pen.up()
  41. pen.goto(-329,-524)
  42. pen.down()
  43. for i in range(2):
  44.     pen.forward(80)
  45.     pen.left(90)
  46.     pen.forward(80)
  47.     pen.left(90)
  48.  
  49. pen.write('L', font=('Arial',15,'normal'))
  50.  
  51. #R
  52. pen1.hideturtle()
  53. pen1.speed(0)
  54. pen1.up()
  55. pen1.goto(200,-524)
  56. pen1.down()
  57. for i in range(2):
  58.     pen1.forward(80)
  59.     pen1.left(90)
  60.     pen1.forward(80)
  61.     pen1.left(90)
  62.  
  63. pen1.write('R', font=('Arial',15,'normal'))
  64.  
  65. #movement
  66. def on_click(x, y):
  67.     #print(x,y)
  68.  
  69.     if -300 < x < 0 and -300 < y < 300: # left part of rectangle
  70.         player1.left(30)
  71.         player1.color('red')
  72.     elif 0 < x < 300 and -300 < y < 300: # right part of rectangle
  73.         player1.right(30)
  74.         player1.color('blue')
  75.  
  76.  
  77. turtle.listen()
  78.  
  79. turtle.onscreenclick(on_click, 1)
  80.    
  81. speed = 5
  82.  
  83. while True:
  84.     player1.forward(speed)
  85.     player2.forward(speed)
  86.     if player1.xcor() > 300 or player1.xcor() < -300:
  87.         player1.right(180)
  88.     if player1.ycor() > 300 or player1.ycor() < -300:
  89.         player1.right(180)
  90.  
  91. turtle.done()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×