Advertisement
xosski

Space invaders

Dec 4th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import turtle
  2. import random
  3. import time
  4.  
  5. # Game setup
  6. koko = 1000
  7. turtle.bgcolor("black")
  8. turtle.speed(0)
  9. turtle.ht()
  10. turtle.setundobuffer(1)
  11. turtle.tracer(1)
  12.  
  13. # Define Sprite class
  14. class Sprite(turtle.Turtle):
  15. def __init__(self, spriteshape, color, startx, starty):
  16. turtle.Turtle.__init__(self, shape=spriteshape)
  17. self.speed(0)
  18. self.penup()
  19. self.color(color)
  20. self.goto(startx, starty)
  21. self.speed = 0.1
  22.  
  23. def move(self):
  24. self.fd(self.speed)
  25.  
  26. # Boundary detection (prevents sprite from leaving screen)
  27. if self.xcor() > koko/2 - 10:
  28. self.setx(koko/2 - 10)
  29. self.rt(60)
  30.  
  31. if self.xcor() < -koko/2 + 10:
  32. self.setx(-koko/2 + 10)
  33. self.rt(60)
  34.  
  35. if self.ycor() > koko/2 - 10:
  36. self.sety(koko/2 - 10)
  37. self.rt(60)
  38.  
  39. if self.ycor() < -koko/2 + 10:
  40. self.sety(-koko/2 + 10)
  41. self.rt(60)
  42.  
  43. # Player class inheriting Sprite
  44. class Player(Sprite):
  45. def __init__(self, spriteshape, color, startx, starty):
  46. Sprite.__init__(self, spriteshape, color, startx, starty)
  47. self.speed = 3
  48. self.lives = 3
  49.  
  50. def turn_left(self):
  51. self.lt(45)
  52.  
  53. def turn_right(self):
  54. self.rt(45)
  55.  
  56. def accelerate(self):
  57. if self.speed < 7:
  58. self.speed += 0.2
  59.  
  60. def decelerate(self):
  61. if self.speed > -5:
  62. self.speed -= 0.2
  63.  
  64. def shoot(self):
  65. bullet = Bullet("square", "yellow", self.xcor(), self.ycor())
  66. bullet.setheading(90)
  67. return bullet
  68.  
  69. # Bullet class
  70. class Bullet(Sprite):
  71. def __init__(self, spriteshape, color, startx, starty):
  72. Sprite.__init__(self, spriteshape, color, startx, starty)
  73. self.speed = 10
  74.  
  75. def move(self):
  76. self.fd(self.speed)
  77.  
  78. # Enemy class
  79. class Enemy(Sprite):
  80. def __init__(self, spriteshape, color, startx, starty):
  81. Sprite.__init__(self, spriteshape, color, startx, starty)
  82. self.speed = 2
  83.  
  84. def move(self):
  85. self.fd(self.speed)
  86. # Boundary check to reverse direction and move down
  87. if self.xcor() > koko/2 - 10 or self.xcor() < -koko/2 + 10:
  88. self.rt(180)
  89. self.sety(self.ycor() - 30)
  90.  
  91. # Game class
  92. class Game:
  93. def __init__(self):
  94. self.level = 1
  95. self.score = 0
  96. self.state = "playing"
  97. self.pen = turtle.Turtle()
  98. self.lives = 3
  99.  
  100. def draw_border(self):
  101. self.pen.speed(0)
  102. self.pen.color("white")
  103. self.pen.pensize(3)
  104. self.pen.penup()
  105. self.pen.goto(-koko/2, koko/2)
  106. self.pen.pendown()
  107. for _ in range(4):
  108. self.pen.fd(koko)
  109. self.pen.rt(90)
  110. self.pen.penup()
  111. self.pen.ht()
  112.  
  113. def update_score(self):
  114. self.pen.clear()
  115. self.pen.color("white")
  116. self.pen.penup()
  117. self.pen.goto(-koko/2 + 10, koko/2 - 30)
  118. self.pen.write(f"Score: {self.score} Lives: {self.lives}", font=("Arial", 16, "normal"))
  119.  
  120. def game_over(self):
  121. self.pen.clear()
  122. self.pen.color("red")
  123. self.pen.penup()
  124. self.pen.goto(0, 0)
  125. self.pen.write("GAME OVER", align="center", font=("Arial", 24, "normal"))
  126. self.pen.goto(0, -30)
  127. self.pen.write(f"Final Score: {self.score}", align="center", font=("Arial", 16, "normal"))
  128.  
  129. # Initialize the game
  130. game = Game()
  131. game.draw_border()
  132.  
  133. # Create the player
  134. player = Player("triangle", "white", 0, -koko/2 + 50)
  135.  
  136. # Create enemies
  137. enemies = []
  138. for _ in range(5):
  139. enemy = Enemy("circle", "red", random.randint(-koko//2 + 10, koko//2 - 10), random.randint(koko//2 - 50, koko//2 - 200))
  140. enemies.append(enemy)
  141.  
  142. # Keyboard bindings
  143. turtle.onkey(player.turn_left, "Left")
  144. turtle.onkey(player.turn_right, "Right")
  145. turtle.onkey(player.accelerate, "Up")
  146. turtle.onkey(player.decelerate, "Down")
  147. turtle.onkey(lambda: player.shoot(), "space")
  148. turtle.listen()
  149.  
  150. # Main game loop
  151. bullets = []
  152. while game.state == "playing":
  153. for bullet in bullets[:]:
  154. bullet.move()
  155. # Check if bullet hits an enemy
  156. for enemy in enemies[:]:
  157. if bullet.distance(enemy) < 20:
  158. bullet.hideturtle()
  159. enemy.hideturtle()
  160. bullets.remove(bullet)
  161. enemies.remove(enemy)
  162. game.score += 10
  163. break
  164.  
  165. for enemy in enemies:
  166. enemy.move()
  167.  
  168. # Check for collision between player and enemies
  169. for enemy in enemies[:]:
  170. if player.distance(enemy) < 20:
  171. enemy.hideturtle()
  172. enemies.remove(enemy)
  173. game.lives -= 1
  174. if game.lives == 0:
  175. game.state = "game_over"
  176. game.game_over()
  177. break
  178.  
  179. # Update score and lives
  180. game.update_score()
  181.  
  182. time.sleep(0.02)
  183.  
  184. # End of game
  185. input("Press Enter to exit...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement