Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. """ I WOULD FINISH THIS BUT SINCE ITS IMPOSSIBLE TO DELETE TURTLES, THE GAME GETS EXTREMELY LAGGY. I WILL NOW WORK ON PACMAN. """
  2.  
  3.  
  4.  
  5.  
  6. from turtle import *
  7. import time
  8. import re
  9. from tkinter import messagebox
  10. import tkinter
  11.  
  12. # Name Definition
  13. name = input("What is your name?")
  14. while ":" in name:
  15. name = input("Sorry, please do not use a colon in your name.")
  16.  
  17. # Keybind Setup (Quite inefficient)
  18. def wasdSetup():
  19. global leftBind
  20. global rightBind
  21. global keybinds
  22. leftBind = "a"
  23. rightBind = "d"
  24. keybinds = "WD"
  25. tk.destroy()
  26. def arrowSetup():
  27. global leftBind
  28. global rightBind
  29. global keybinds
  30. leftBind = "Left"
  31. rightBind = "Right"
  32. keybinds = "ARROWS"
  33. tk.destroy()
  34.  
  35. # Button Widgets
  36. tk = tkinter.Tk()
  37. tk.title("Choose your input method.")
  38. tk.minsize(500, 100)
  39. tk.resizable(False, False)
  40. frame = tkinter.Frame(tk)
  41. frame.pack()
  42. option1 = tkinter.Button(frame, text = "WD", command = wasdSetup)
  43. option1.pack(side = tkinter.LEFT)
  44. option2 = tkinter.Button(frame, text = "Arrows", command = arrowSetup)
  45. option2.pack(side = tkinter.RIGHT)
  46. tk.mainloop()
  47.  
  48. # Window Setup
  49. window = Screen()
  50. def windowSetup(): # Function so it can be called at end of game
  51. width = 600
  52. height = 600
  53. window.setup(width, height)
  54. window.tracer(0) # Turn off automatic window updates
  55. window.bgcolor("#000000")
  56. window.title("Space Invaders")
  57. windowSetup()
  58.  
  59. # Player setup
  60. player = Turtle()
  61. player.speed(0)
  62. player.facing = "idle" # Start without moving
  63. player.shape("square")
  64. player.color("#ffffff")
  65. player.penup()
  66. player.goto(0, -280)
  67.  
  68. # Enemy variables
  69. eVar = Turtle()
  70. eVar.movementsNeeded = 4
  71. eVar.movementsComplete = 0
  72. eVar.movementDirection = "right"
  73.  
  74. # Shape registry
  75. window.register_shape("bullet.gif")
  76.  
  77. # Score turtle
  78. currentScore = Turtle()
  79. currentScore.hideturtle()
  80.  
  81. bullets = []
  82. enemies = []
  83.  
  84. delay = 0.075
  85.  
  86. score = 0
  87.  
  88. def left():
  89. player.facing = "left"
  90. def right():
  91. player.facing = "right"
  92.  
  93. def shoot():
  94. bullet = Turtle()
  95. bullet.speed(0)
  96. bullet.shape("bullet.gif")
  97. bullet.penup()
  98. bullet.goto(player.xcor(), player.ycor() + 20)
  99. bullets.append(bullet)
  100.  
  101. def cheat():
  102. xPos = -200
  103. yPos = -200
  104. for i in range(54):
  105. bullet = Turtle()
  106. bullet.speed(0)
  107. bullet.shape("bullet.gif")
  108. bullet.penup()
  109. bullet.goto(xPos, yPos)
  110. bullets.append(bullet)
  111. xPos += 30
  112. if xPos > 250:
  113. yPos += 30
  114. xPos = -250
  115.  
  116. # Keybind listener
  117. window.listen()
  118. window.onkeypress(left, leftBind)
  119. window.onkeypress(right, rightBind)
  120. window.onkeypress(shoot, "space")
  121. window.onkeypress(cheat, "c")
  122.  
  123. def createEnemies(amount):
  124. xPos = -200
  125. yPos = 270
  126. for i in range(amount):
  127. enemy = Turtle()
  128. enemy.speed(0)
  129. enemy.shape("square")
  130. enemy.color("#ffffff")
  131. enemy.penup()
  132. enemy.downRequired = False
  133. enemy.movementDir = "right"
  134. enemy.movedDown = False
  135. enemy.goto(xPos, yPos)
  136. enemies.append(enemy)
  137. xPos += 30
  138. if xPos > 200:
  139. yPos -= 30
  140. xPos = -200
  141.  
  142. def movePlayer():
  143. if player.facing == "left":
  144. player.setx(player.xcor() - 20)
  145. if player.facing == "right":
  146. player.setx(player.xcor() + 20)
  147.  
  148. def moveBullets():
  149. for bullet in bullets:
  150. if bullet.ycor() < 320:
  151. bullet.sety(bullet.ycor() + 20)
  152. else:
  153. bullets.remove(bullet)
  154.  
  155. def moveEnemies():
  156. print(score)
  157. for e in enemies:
  158. if eVar.movementDirection == "right":
  159. e.setx(e.xcor() + 20)
  160. elif eVar.movementDirection == "left":
  161. e.setx(e.xcor() - 20)
  162. eVar.movementsComplete += 1
  163.  
  164. createEnemies(42)
  165.  
  166. enemyMovementDelay = 10
  167. enemyMovementCounter = 0
  168. hasMovedCounter = 0
  169. # Constant loop
  170. while True:
  171. window.update()
  172.  
  173. if len(enemies) == 0:
  174. createEnemies(42)
  175. score += 1000
  176. if enemyMovementDelay != 3:
  177. enemyMovementDelay -= 1
  178. enemyMovementCounter = 0
  179. eVar.movementsNeeded = 4
  180. eVar.movementsComplete = 0
  181. eVar.movementDirection = "right"
  182.  
  183. # Write score
  184. currentScore.clear()
  185. currentScore = Turtle()
  186. currentScore.pencolor("#ffffff")
  187. currentScore.hideturtle()
  188. currentScore.penup()
  189. currentScore.speed(0)
  190. currentScore.goto(-270, -270)
  191. currentScore.write(score, font=("Arial", 25, "normal"))
  192.  
  193. if player.xcor() > 290:
  194. player.goto(player.xcor() - 580, player.ycor())
  195. elif player.xcor() < -290:
  196. player.goto(player.xcor() + 580, player.ycor())
  197.  
  198. movePlayer()
  199. moveBullets()
  200.  
  201. if enemyMovementCounter == enemyMovementDelay:
  202. if eVar.movementsComplete == eVar.movementsNeeded:
  203. for e in enemies:
  204. e.sety(e.ycor() - 20)
  205. eVar.movementsComplete = 0
  206. eVar.movementsNeeded = 8
  207. enemyMovementCounter = 0
  208. if eVar.movementDirection == "right":
  209. eVar.movementDirection = "left"
  210. else:
  211. eVar.movementDirection = "right"
  212. else:
  213. moveEnemies()
  214. enemyMovementCounter = 0
  215.  
  216. for e in enemies:
  217. for b in bullets:
  218. if e.distance(b) < 20:
  219. enemies.remove(e)
  220. e.goto(-1500, -1500)
  221. bullets.remove(b)
  222. b.reset()
  223. score += 10
  224.  
  225.  
  226. enemyMovementCounter += 1
  227. hasMovedCounter += 1
  228. time.sleep(delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement