Advertisement
kingbode

Untitled

Nov 7th, 2023
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import turtle
  2. import random
  3.  
  4. # Create a screen
  5. window = turtle.Screen()
  6. window.title('Moving Block')
  7. window.bgcolor('black')
  8. # Set up the screen's attributes, including the border
  9. window.setup(width=600, height=400) # Set the window size
  10. window.tracer(0) # Turn off automatic screen updates
  11.  
  12. # Create a turtle shape
  13. shape = turtle.Turtle()
  14. shape.shape('square')
  15. shape.color('white')
  16. shape.penup()
  17.  
  18. # List to store obstacle
  19. obstacles = []
  20.  
  21. # Set the boundaries
  22. border_left = -290
  23. border_right = 290
  24. border_top = 190
  25. border_bottom = -190
  26.  
  27. # Function to move the shape forward
  28. def move_forward():
  29.     new_x = shape.xcor() + 10
  30.     if border_left < new_x < border_right:
  31.         shape.setx(new_x)
  32.  
  33. # Function to move the shape backward
  34. def move_backward():
  35.     new_x = shape.xcor() - 10
  36.     if border_left < new_x < border_right:
  37.         shape.setx(new_x)
  38.  
  39. # Function to move shape up
  40. def move_up():
  41.     new_y = shape.ycor() + 10
  42.     if border_bottom < new_y < border_top:
  43.         shape.sety(new_y)
  44.  
  45. # Function to move shape down
  46. def move_down():
  47.     new_y = shape.ycor() - 10
  48.     if border_bottom < new_y < border_top:
  49.         shape.sety(new_y)
  50.  
  51. # Function to create a random obstacle
  52. def createObstacle():
  53.     obstacle = turtle.Turtle()
  54.  
  55.     obstacle.shape('circle')  # You can change the shape as desired
  56.     obstacle.color('red')
  57.     obstacle.penup()
  58.     obstacle.goto(border_right, random.randint(border_bottom, border_top))
  59.     obstacles.append(obstacle)
  60.  
  61. # Function to move obstacles to the left
  62. def move_obstaclees():
  63.     for obstacle in obstacles:
  64.         obstacle.setx(obstacle.xcor() - 1)
  65.  
  66.         if obstacle.xcor() < border_left:
  67.             obstacles.remove(obstacle)
  68.             obstacle.hideturtle()
  69.  
  70. # Function to check for collisions
  71. def check_collisions():
  72.     for obstacle in obstacles:
  73.         if shape.distance(obstacle) < 20:  # You can adjust the collision threshold as needed
  74.             shape.goto(0, 0)  # Reset the shape's position
  75.             died()
  76.  
  77. # Bind the key d to forward
  78. window.onkeypress(move_forward, 'd')
  79.  
  80. # Bind key a to backward
  81. window.onkeypress(move_backward, 'a')
  82.  
  83. # Bind key w to up
  84. window.onkeypress(move_up, 'w')
  85.  
  86. # Bind key s to down
  87. window.onkeypress(move_down, 's')
  88.  
  89. # Listen for key events
  90. window.listen()
  91.  
  92.  
  93. # Function that outputs when you lose
  94. def died():
  95.     # Write on the screen
  96.     turtle.penup()
  97.     turtle.goto(-50, 100) # Set the position where the text will be written
  98.     turtle.color('white')
  99.     turtle.write('You Died', align='left', font=('comicsans', 20, 'bold'))
  100.  
  101.  
  102. # Continuous movement loop
  103. while True:
  104.     move_obstaclees()
  105.     check_collisions()
  106.     window.update() # Update the screen
  107.  
  108.     # Randomly create obstacles
  109.     if random.randint(1, 100) == 1:
  110.         createObstacle()
  111.  
  112. # Keep the window open
  113. turtle.done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement