Facebook
From Mammoth Lion, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 127
  1. import turtle
  2. import random
  3. score = 0
  4. high_score = 0
  5. # Setting the window
  6. window = turtle.Screen()
  7. window.title("Space War")
  8. window.bgcolor("white")
  9. window.setup(width=600, height=600)
  10. window.tracer(3)
  11.  
  12. # Player
  13. player = turtle.Turtle()
  14. player.speed(0)
  15. player.shape("triangle")
  16. player.color("brown")
  17. player.penup()
  18. player.goto(0, 0)
  19.  
  20. #CREATE THE BULLET
  21. bullet = turtle.Turtle()
  22. bullet.shape("triangle")
  23. bullet.penup()
  24. bullet.speed(0)
  25. bullet.setheading(player.heading())
  26. bullet.shapesize(0.2, 0.8)
  27. bullet.hideturtle()
  28.  
  29. bulletspeed = 20
  30. bulletstate = "ready"
  31.  
  32.  
  33. # pen
  34. Pen = turtle.Turtle()
  35. Pen.speed(0)
  36. Pen.color("black")
  37. Pen.penup()
  38. Pen.goto(0, 250)
  39. Pen.hideturtle()
  40. Pen.clear()
  41. Pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("courier", 24, "normal"))
  42.  
  43. # Foods
  44. foods = []
  45. # Food
  46. for index in range(4):
  47.     food = turtle.Turtle()
  48.     food.speed(0)
  49.     food.shape("circle")
  50.     food.color("green")
  51.     food.penup()
  52.     foods.append(food)
  53.     food.setposition(random.randint(-250, 250), random.randint(-250, 250))
  54.  
  55. # Pen
  56. pen = turtle.Turtle()
  57. pen.penup()
  58. pen.setposition(-280, -280)
  59. pen.pendown()
  60. pen.pensize(1)
  61. for x in range(4):
  62.     pen.forward(560)
  63.     pen.left(90)
  64. pen.hideturtle()
  65.  
  66. # Variable speed
  67. speed = 1
  68.  
  69.  
  70. # Movement function
  71. def move_right():
  72.     player.right(45)
  73.  
  74.  
  75. def move_left():
  76.     player.left(45)
  77. def fire_bullet():
  78. #DECLARE BULLETSTATE AS GLOBAL IF IT NEEDS CHANGES
  79.     global bulletstate
  80.     if bulletstate == "ready":
  81.         bulletstate = "fire"
  82.         #MOVE BULLET TO JUST ABOVE THE PLAYER
  83.         x = player.xcor()
  84.         y = player.ycor() + 10
  85.         bullet.setposition(x, y)
  86.         bullet.showturtle()
  87.  
  88.  
  89. def increase_speed():
  90.     player.up()
  91.     global speed
  92.     speed += 1
  93.  
  94.  
  95. def decrease_speed():
  96.     player.down()
  97.     global speed
  98.     speed -= 1
  99.  
  100.  
  101. # Keyboard binding
  102. window.listen()
  103. window.onkeypress(move_right, "Right")
  104. window.onkeypress(move_left, "Left")
  105. window.onkeypress(increase_speed, "Up")
  106. window.onkeypress(decrease_speed, "Down")
  107. window.onkeypress(fire_bullet, "space")
  108. # main game loop
  109. while True:
  110.     window.update()
  111.  
  112.     # Default player movement
  113.     player.forward(speed)
  114.  
  115.     # player boundary checking
  116.     if player.xcor() > 260 or player.xcor() < -260 or player.ycor() > 260 or player.ycor() < -260:
  117.         player.right(180)
  118.     #MOVE BULLET
  119.     if bulletstate == "fire":
  120.         y = bullet.ycor()
  121.         y += bulletspeed
  122.         bullet.sety(y)
  123.  
  124.  
  125.     #CHECK IF BULLET HAS GONE TO TOP
  126.     if bullet.ycor() > 275:
  127.         bullet.hideturtle()
  128.         bulletstate = "ready"
  129.  
  130.  
  131.  
  132.     # Move food
  133.     for food in foods:
  134.         food.forward(1)
  135.  
  136.         # Foods boundary checking
  137.         if food.xcor() > 260 or food.xcor() < -260 or food.ycor() > 260 or food.ycor() < -260:
  138.             food.right(180)
  139.  
  140.         # Check collision between player and food
  141.         if food.distance(player) < 20:
  142.             x = random.randint(-260, 260)
  143.             y = random.randint(-260, 260)
  144.             food.goto(x, y)
  145.  
  146.  
  147.  
  148.  
  149.  
  150. window.mainloop()
  151.