Advertisement
tokyoedtech

Falling Skies by @TokyoEdTech

Oct 9th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. # Falling Skies in Python 3 for Beginners
  2. # By @TokyoEdTech
  3. # Part 7: Scoring
  4.  
  5. import turtle
  6. import random
  7.  
  8. score = 0
  9. lives = 3
  10.  
  11. wn = turtle.Screen()
  12. wn.title("Falling Skies by @TokyoEdTech")
  13. wn.bgcolor("green")
  14. wn.setup(width=800, height=600)
  15. wn.tracer(0)
  16.  
  17. # Add the player
  18. player = turtle.Turtle()
  19. player.speed(0)
  20. player.shape("square")
  21. player.color("white")
  22. player.penup()
  23. player.goto(0, -250)
  24. player.direction = "stop"
  25.  
  26. # Create a list of good guys
  27. good_guys = []
  28.  
  29. # Add the good_guys
  30. for _ in range(20):
  31.     good_guy = turtle.Turtle()
  32.     good_guy.speed(0)
  33.     good_guy.shape("circle")
  34.     good_guy.color("blue")
  35.     good_guy.penup()
  36.     good_guy.goto(-100, 250)
  37.     good_guy.speed = random.randint(1, 4)
  38.     good_guys.append(good_guy)
  39.  
  40. # Create a list of bad guys
  41. bad_guys = []
  42.  
  43. # Add the good_guys
  44. for _ in range(20):
  45.     bad_guy = turtle.Turtle()
  46.     bad_guy.speed(0)
  47.     bad_guy.shape("circle")
  48.     bad_guy.color("red")
  49.     bad_guy.penup()
  50.     bad_guy.goto(100, 250)
  51.     bad_guy.speed = random.randint(1, 4)
  52.     bad_guys.append(bad_guy)
  53.  
  54. # Make the pen
  55. pen = turtle.Turtle()
  56. pen.hideturtle()
  57. pen.speed(0)
  58. pen.shape("square")
  59. pen.color("white")
  60. pen.penup()
  61. pen.goto(0, 260)
  62. font = ("Courier", 24, "normal")
  63. pen.write("Score: {}  Lives: {}".format(score, lives), align="center", font=font)
  64.  
  65.  
  66. # Functions
  67. def go_left():
  68.     player.direction = "left"
  69.  
  70. def go_right():
  71.     player.direction = "right"
  72.  
  73.  
  74. # Keyboard Binding
  75. wn.listen()
  76. wn.onkeypress(go_left, "Left")
  77. wn.onkeypress(go_right, "Right")
  78.  
  79. # Main game loop
  80. while True:
  81.     # Update screen
  82.     wn.update()
  83.  
  84.     # Move the player
  85.     if player.direction == "left":
  86.         x = player.xcor()
  87.         x -= 3
  88.         player.setx(x)
  89.  
  90.     if player.direction == "right":
  91.         x = player.xcor()
  92.         x += 3
  93.         player.setx(x)
  94.  
  95.     # Move the good guys
  96.     for good_guy in good_guys:
  97.         y = good_guy.ycor()
  98.         y -= good_guy.speed
  99.         good_guy.sety(y)
  100.  
  101.         # Check if off the screen
  102.         if y < -300:
  103.             x = random.randint(-380, 380)
  104.             y = random.randint(300, 400)
  105.             good_guy.goto(x, y)
  106.  
  107.         # Check for a collision with the player
  108.         if good_guy.distance(player) < 20:
  109.             x = random.randint(-380, 380)
  110.             y = random.randint(300, 400)
  111.             good_guy.goto(x, y)
  112.             score += 10
  113.             pen.clear()
  114.             pen.write("Score: {}  Lives: {}".format(score, lives), align="center", font=font)
  115.  
  116.     # Move the bad guys
  117.     for bad_guy in bad_guys:
  118.         y = bad_guy.ycor()
  119.         y -= bad_guy.speed
  120.         bad_guy.sety(y)
  121.  
  122.         # Check if off the screen
  123.         if y < -300:
  124.             x = random.randint(-380, 380)
  125.             y = random.randint(300, 400)
  126.             bad_guy.goto(x, y)
  127.  
  128.         # Check for a collision with the player
  129.         if bad_guy.distance(player) < 20:
  130.             x = random.randint(-380, 380)
  131.             y = random.randint(300, 400)
  132.             bad_guy.goto(x, y)
  133.             score -= 10
  134.             lives -= 1
  135.             pen.clear()
  136.             pen.write("Score: {}  Lives: {}".format(score, lives), align="center", font=font)
  137.  
  138. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement