Advertisement
Guest User

falling skies part 5. 2

a guest
Apr 7th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import turtle
  2. import random
  3.  
  4. wn = turtle.Screen()
  5. wn.title("Falling Skies by Charan")
  6. wn.bgcolor("green")
  7. wn.setup(width=800, height=600)
  8. wn.tracer(0)
  9.  
  10. #Add the player
  11. player = turtle.Turtle()
  12. player.speed(0)
  13. player.shape("square")
  14. player.color("white")
  15. player.penup()
  16. player.goto(0, -250)
  17. player.direction = "stop"
  18.  
  19. #Create a list of good_guys
  20. good_guys = []
  21.  
  22. #Add the good_guy
  23. for _ in range(20):
  24.     good_guy = turtle.Turtle()
  25.     good_guy.speed(0)
  26.     good_guy.shape("circle")
  27.     good_guy.color("blue")
  28.     good_guy.penup()
  29.     good_guy.goto(0, 250)
  30.     good_guys.append(good_guy)    
  31.  
  32. #Function
  33. def go_left():
  34.     player.direction = "left"
  35.  
  36. def go_right():
  37.     player.direction = "right"
  38.  
  39.  
  40. #Keyboard bindings
  41. wn.listen()
  42. wn.onkeypress(go_left, "Left")
  43. wn.onkeypress(go_right, "Right")
  44.  
  45. #Main game loop
  46. while True:
  47.     #Update the screen
  48.     wn.update()
  49.    
  50.     # Move the player
  51.     if player.direction == "left":
  52.         x = player.xcor()
  53.         x -= 3
  54.         player.setx(x)
  55.  
  56.     if player.direction == "right":
  57.         x = player.xcor()
  58.         x += 3
  59.         player.setx(x)
  60.  
  61.     #Move the good_guys
  62.     for good_guy in good_guys:
  63.         y = good_guy.ycor()
  64.         y -= 3
  65.         good_guy.sety(y)
  66.  
  67.         #Check if off the screen
  68.         if y < -300:
  69.             x = random.randint(-380, 380)
  70.             y = random.randint(300, 400)
  71.             good_guy.goto(x, y)
  72.  
  73.         #Check for a collision with the player
  74.         if good_guy.distance(player) < 20:
  75.             x = random.randint(-380, 380)
  76.             y = random.randint(300, 400)
  77.             good_guy.goto(x, y)
  78.  
  79.  
  80.  
  81. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement