Facebook
From Scribby Eider, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 36
  1. # space invaders
  2. # set up screen
  3. import turtle
  4. import os
  5.  
  6. #set up the screen
  7. wn = turtle.Screen()
  8. wn.bgcolor("black")
  9. wn.title("space invaders")
  10.  
  11. #Draw border
  12. border_pen = turtle.Turtle()
  13. border_pen.speed(0)
  14. border_pen.color("white")
  15. border_pen.penup()
  16. border_pen.setposition(-300,-300)
  17. border_pen.pendown()
  18. border_pen.pensize(3)
  19. for side in range(4):
  20.     border_pen.fd(600)
  21.     border_pen.lt(90)
  22. border_pen.hideturtle()
  23.  
  24. # create the player turtle
  25. player = turtle.Turtle()
  26. player.color("blue")
  27. player.shape("triangle")
  28. player.penup()
  29. player.speed(0)
  30. player.setposition(0,-250)
  31. player.setheading(90)
  32.  
  33. # player moveing
  34. playerspeed = 15
  35.  
  36. # create the enemy
  37. enemy = turtle.Turtle()
  38. enemy.color("red")
  39. enemy.shape("circle")
  40. enemy.penup()
  41. enemy.speed(0)
  42. enemy.setposition(-200,250)
  43. espeed = 2
  44.  
  45. def move_left():
  46.     x = player.xcor()
  47.     x -= playerspeed
  48.     if x < -280:
  49.         x = -280
  50.     player.setx(x)
  51.  
  52. def move_right():
  53.     x = player.xcor()
  54.     x += playerspeed
  55.     if x > 280:
  56.         x = 280
  57.     player.setx(x)
  58.  
  59.  
  60. # create keyboard bindings
  61. turtle.listen()
  62. turtle.onkeypress(move_left, "Left")
  63. turtle.onkeypress(move_right, "Right")
  64. turtle.mainloop()
  65.  
  66.  
  67.  
  68. #main game loop
  69. while True:
  70.  
  71.     #move the enemy
  72.     x = enemy.xcor()
  73.     x += espeed
  74.     enemy.setx(x)
  75.  
  76.     #Move the enemy back and down
  77.     if enemy.xcor() > 280:
  78.         espeed *= 1
  79.  
  80.     if enemy.xcor() < -280:
  81.         espeed *= 1
  82.