Advertisement
kingbode

Untitled

Nov 7th, 2023
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. """
  2. 1- used arrow keys instead of a,s,w,d
  3. 2- the obstacles start very fast at the startup , player died soon !!, so I made it slower
  4. 3- changed the number of obstacles and the condition of creation of them
  5. 4- added speed variable,  you can change the value of it by time or by score
  6. 5- added function check_obstacles() # return True if all obstacles have been passed behind the shape
  7. 6- added menu options to play again or quit
  8.  
  9. """
  10.  
  11. import turtle
  12. import random
  13.  
  14. # Create a screen
  15. window = turtle.Screen()
  16. window.title('Moving Block')
  17. window.bgcolor('black')
  18. # Set up the screen's attributes, including the border
  19. window.setup(width=600, height=400) # Set the window size
  20. window.tracer(0) # Turn off automatic screen updates
  21.  
  22. # Create a turtle shape
  23. shape = turtle.Turtle()
  24. shape.shape('square')
  25. shape.color('white')
  26. shape.penup()
  27.  
  28. # print(shape.xcor(), shape.ycor())
  29.  
  30. # List to store obstacle
  31. obstacles = []
  32.  
  33. # Set the boundaries
  34. border_left = -290
  35. border_right = 290
  36. border_top = 190
  37. border_bottom = -190
  38.  
  39. # Set the speed
  40. speed = 20
  41. # Function to move the shape forward
  42. def move_forward():
  43.     new_x = shape.xcor() + speed
  44.     if border_left < new_x < border_right:
  45.         shape.setx(new_x)
  46.  
  47. # Function to move the shape backward
  48. def move_backward():
  49.     new_x = shape.xcor() - speed
  50.     if border_left < new_x < border_right:
  51.         shape.setx(new_x)
  52.  
  53. # Function to move shape up
  54. def move_up():
  55.     new_y = shape.ycor() + speed
  56.     if border_bottom < new_y < border_top:
  57.         shape.sety(new_y)
  58.  
  59. # Function to move shape down
  60. def move_down():
  61.     new_y = shape.ycor() - speed
  62.     if border_bottom < new_y < border_top:
  63.         shape.sety(new_y)
  64.  
  65. # Function to create a random obstacle
  66. def createObstacle():
  67.     obstacle = turtle.Turtle()
  68.  
  69.     obstacle.shape('circle')  # You can change the shape as desired
  70.     obstacle.color('red')
  71.     obstacle.penup()
  72.     # obstacle.goto(border_right, random.randint(border_bottom, border_top))
  73.     obstacle.goto(border_right + random.randint(0, 100), random.randint(border_bottom, border_top))
  74.     obstacles.append(obstacle)
  75.  
  76. # Function to move obstacles to the left
  77. def move_obstacles():
  78.     for obstacle in obstacles:
  79.         obstacle.setx(obstacle.xcor() - .1) # You can adjust the speed as needed by changing the value of .1
  80.  
  81.         if obstacle.xcor() < border_left:
  82.             obstacles.remove(obstacle)
  83.             obstacle.hideturtle()
  84.  
  85. # Function to check for collisions
  86. def check_collisions():
  87.     for obstacle in obstacles:
  88.         if shape.distance(obstacle) < 20:  # You can adjust the collision threshold as needed
  89.             shape.goto(0, 0)  # Reset the shape's position
  90.             died()
  91.             return True
  92.     return False
  93.  
  94. def check_obstacles():
  95.     # return True if all obstacles have been passed behind the shape
  96.     for obstacle in obstacles:
  97.         if obstacle.xcor() > border_left and obstacle.xcor() < border_right and obstacle.xcor() > shape.xcor():
  98.             return False
  99.     return True
  100.  
  101. def clearObstacles():
  102.     for obstacle in obstacles:
  103.         obstacle.hideturtle()
  104.     obstacles.clear()
  105.  
  106. # Function that outputs when you lose
  107. def died():
  108.     # Write on the screen
  109.     turtle.penup()
  110.     turtle.goto(-280, 100) # Set the position where the text will be written
  111.     turtle.color('white')
  112.     turtle.write('You Died', align='left', font=('comicsans', 20, 'bold'))
  113.  
  114.     # Write on the screen Play Again
  115.     turtle.penup()
  116.     turtle.goto(-280, 50) # Set the position where the text will be written
  117.     turtle.color('white')
  118.     turtle.write('Play Again ? ( press space to start again )', align='left', font=('comicsans', 20, 'bold'))
  119.  
  120.     # Write on the screen Quit
  121.     turtle.penup()
  122.     turtle.goto(-280, 0) # Set the position where the text will be written
  123.     turtle.color('white')
  124.     turtle.write('Quit ?  ( press Esc to Quit )', align='left', font=('comicsans', 20, 'bold'))
  125.  
  126.     # hide the turtle
  127.     turtle.hideturtle()
  128.  
  129.  
  130. def quitGame():
  131.     window.bye()
  132.  
  133. def main():
  134.     # Clear the obstacles
  135.     clearObstacles()
  136.  
  137.     # Reset the shape's position
  138.     shape.goto(0, 0)
  139.  
  140.     # clear the screen
  141.     turtle.clear()
  142.  
  143.     # Bind the key right arrow to forward
  144.     window.onkeypress(move_forward,key='Right')
  145.  
  146.     # Bind key left arrow to backward
  147.     window.onkeypress(move_backward, key='Left')
  148.  
  149.     # Bind key UP arrow to up
  150.     window.onkeypress(move_up, key='Up')
  151.  
  152.     # Bind key down arrow to down
  153.     window.onkeypress(move_down, key='Down')
  154.  
  155.     # Bind key space to restart
  156.     window.onkeypress(main, key='space')
  157.  
  158.     # Bind key Esc to quit
  159.     window.onkeypress(quitGame, key='Escape')
  160.  
  161.     # Listen for key events
  162.     window.listen()
  163.  
  164.     # Continuous movement loop
  165.     while True:
  166.  
  167.         move_obstacles()
  168.         if check_collisions():
  169.             break
  170.         window.update() # Update the screen
  171.  
  172.         # Randomly create obstacles
  173.         # check if all obstacles have been passed behind the shape
  174.         if len(obstacles)<15 or check_obstacles(): # create an obstacle if there are no obstacles
  175.             if random.randint(1, 50) == 1:  # controls the frequency of obstacle creation by changing the value of 50
  176.                 createObstacle()
  177.  
  178.     # Keep the window open
  179.     turtle.done()
  180.     print('Game Over')
  181.  
  182.  
  183. if __name__ == '__main__':
  184.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement