Advertisement
bngtan

space invader with dsa attempt

Mar 29th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. import turtle
  2. import os
  3. import math
  4. import random
  5.  
  6. list = []
  7. c_id = 0
  8. mlst = []
  9.  
  10. def adduser():
  11. global c_id
  12. print("\n----- Add User -----")
  13. idn = len(list) + 1
  14. firstn = input("First name: ")
  15. lastn = input("Last name: ")
  16. ema = input("Email: ")
  17. search = bsearch(mlst, ema)
  18. if search == 1:
  19. print("\n!!! Email has already been used. Try again!!!\n")
  20. main()
  21. else:
  22. c_id+=1
  23. passw = input("Password: ")
  24. print(">>> Student added with ID: {}".format(c_id))
  25. user = {
  26. "ID" : idn,
  27. "Firstname" : firstn,
  28. "Lastname" : lastn,
  29. "Mail" : ema,
  30. "Password" : passw}
  31.  
  32. mlst.append(ema)
  33. mailsort(mlst, "Mail")
  34. list.append(user)
  35. wait()
  36. main()
  37.  
  38. def dispuser():
  39. global list
  40. dictsort(list, "ID")
  41. print("\n----- Display Users -----")
  42. print("ID\t\t\t", "Name\t\t\t\t", "Email")
  43. for i in range(len(list)):
  44. name = str(list[i]["Firstname"] + " " + list[i]["Lastname"])
  45. ID = str(list[i]["ID"])
  46. mail = str(list[i]["Mail"])
  47.  
  48. print(ID,"\t\t\t",name,"\t\t\t",mail)
  49.  
  50. def bsearch(mlst, maill):
  51. first = 0
  52. last = len(mlst)-1
  53. found = 0
  54. while first <= last and not found:
  55. midpoint = (first + last)//2
  56. if mlst[midpoint] == maill:
  57. found = 1
  58. else:
  59. if maill < mlst[midpoint]:
  60. last = midpoint - 1
  61. else:
  62. first = midpoint + 1
  63. return found
  64.  
  65. def mailsort(mlst, maill):
  66. for fillslot in range(len(mlst) -1, 0 , -1):
  67. positionOfMax = 0
  68. for location in range(1, fillslot + 1):
  69. if mlst[location] > mlst[positionOfMax]:
  70. positionOfMax = location
  71.  
  72. temp = mlst[fillslot]
  73. mlst[fillslot] = mlst[positionOfMax]
  74. mlst[positionOfMax] = temp
  75.  
  76. def dictsort(list, idd):
  77. for fillslot in range(len(list) -1, 0 , -1):
  78. positionOfMax = 0
  79. for location in range(1, fillslot + 1):
  80. if list[location][idd] > list[positionOfMax][idd]:
  81. positionOfMax = location
  82.  
  83. temp = list[fillslot]
  84. list[fillslot] = list[positionOfMax]
  85. list[positionOfMax] = temp
  86.  
  87. def wait():
  88. wait = input("\nPress <ENTER> to continue...")
  89.  
  90. def main():
  91. print("\n----- Menu -----")
  92. print("""A - Add user
  93. D - Display Users
  94. P - Play game
  95. E - Exit""")
  96. ch = input("\n>> Enter your choice: ")
  97. while (1):
  98. if ch.lower()=='a':
  99. adduser()
  100. elif ch.lower()=='d':
  101. dispuser()
  102. wait()
  103. main()
  104. return False
  105. elif ch.lower()=='p':
  106.  
  107. wn = turtle.Screen()
  108. wn.bgcolor("black")
  109. wn.title("Space Guardian")
  110. #wn.bgpic("space_invaders_background.gif")
  111.  
  112. score = 0
  113. #draw score
  114. score_pen = turtle.Turtle()
  115. score_pen.speed(0)
  116. score_pen.color("white")
  117. score_pen.penup()
  118. score_pen.setposition(-290, 280)
  119. scorestring = "Score: %s" %score
  120. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  121. score_pen.hideturtle()
  122.  
  123. #shapes
  124. turtle.register_shape("invader.gif")
  125. turtle.register_shape("player.gif")
  126. #border
  127. border_pen = turtle.Turtle()
  128. border_pen.speed(0)
  129. border_pen.color("white")
  130. border_pen.penup()
  131. border_pen.setposition(-300,-300)
  132. border_pen.pendown()
  133. border_pen.pensize(3)
  134. for side in range(4):
  135. border_pen.fd(600)
  136. border_pen.lt(90)
  137. border_pen.hideturtle()
  138.  
  139.  
  140. #player
  141. player = turtle.Turtle()
  142. player.color("blue")
  143. player.shape("triangle")
  144. player.penup()
  145. player.speed(0)
  146. player.setposition(0,-250)
  147. player.setheading(90)
  148.  
  149. playerspeed = 15
  150.  
  151.  
  152.  
  153. #number of enemies
  154. number_of_enemies = 2
  155. #list of enemies
  156. enemies = []
  157.  
  158. #add enemies to the list
  159. for i in range(number_of_enemies):
  160. enemies.append(turtle.Turtle())
  161.  
  162. for enemy in enemies:
  163. enemy.color("red")
  164. enemy.shape("circle")
  165. enemy.penup()
  166. enemy.speed(0)
  167. x = random.randint(-200,200)
  168. y = random.randint(100,250)
  169. enemy.setposition(x,y)
  170.  
  171. enemyspeed= 3
  172.  
  173.  
  174.  
  175.  
  176. #player weapon
  177. bullet = turtle.Turtle()
  178. bullet.color("yellow")
  179. bullet.shape("triangle")
  180. bullet.penup()
  181. bullet.speed(0)
  182. bullet.setheading(90)
  183. bullet.shapesize(0.5,0.5)
  184. bullet.hideturtle()
  185.  
  186. bulletspeed = 25
  187.  
  188. #weapon state
  189. # ready to fire and fire
  190. bulletstate = "ready"
  191. #move function
  192.  
  193. def move_left():
  194. x = player.xcor()
  195. x -= playerspeed
  196. #player border boundary
  197. if x <- 280:
  198. x= -280
  199. player.setx(x)
  200.  
  201. def move_right():
  202. x = player.xcor()
  203. x += playerspeed
  204. #player border boundary
  205. if x > 280:
  206. x= 280
  207. player.setx(x)
  208.  
  209. #bullet fn.
  210. def fire_bullet():
  211. global bulletstate
  212. if bulletstate == "ready":
  213. bulletstate = "fire"
  214.  
  215. if not bullet.isvisible():
  216. x = player.xcor()
  217. y = player.ycor() + 10
  218. bullet.setposition(x,y)
  219. bullet.showturtle()
  220.  
  221. #bullet above player
  222. x = player.xcor()
  223. y = player.ycor()
  224. bullet.setposition(x, y+10)
  225. bullet.showturtle()
  226.  
  227. def isCollision(t1, t2):
  228. distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
  229. if distance < 15:
  230. return True
  231. else:
  232. return False
  233.  
  234.  
  235.  
  236. wn.listen()
  237. wn.onkeypress(move_left, "Left")
  238. wn.onkeypress(move_right, "Right")
  239. wn.onkeypress(fire_bullet,"space")
  240.  
  241.  
  242. #main game loop
  243. while True:
  244.  
  245. for enemy in enemies:
  246. #move enemy
  247. x = enemy.xcor()
  248. x += enemyspeed
  249. enemy.setx(x)
  250.  
  251. #enemy boundary check
  252. if enemy.xcor() > 280:
  253. #move all enemies down
  254. for e in enemies:
  255. y = e.ycor()
  256. y -= 40
  257. e.sety(y)
  258. #change direction
  259. enemyspeed *= -1
  260.  
  261. if enemy.xcor() < - 280:
  262. #move all enemies down
  263. for e in enemies:
  264. y = e.ycor()
  265. y -= 40
  266. e.sety(y)
  267. #change direction
  268. enemyspeed *= -1
  269.  
  270. #collision w bullet and enemy
  271. if isCollision(bullet,enemy):
  272. bullet.hideturtle()
  273. bulletstate = "ready"
  274. bullet.setposition(0, -400)
  275. x = random.randint(-200,200)
  276. y = random.randint(100,250)
  277. enemy.setposition(x,y)
  278.  
  279. #update score
  280. score += 10
  281. scorestring = "Score: %s" %score
  282. score_pen.clear()
  283. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  284.  
  285. if isCollision(player,enemy):
  286. player.hideturtle()
  287. enemy.hideturtle()
  288. print("Game over")
  289. break
  290.  
  291. #Boundary check
  292. if enemy.xcor() > 300 or enemy.xcor() < -300:
  293. print("GAME OVER")
  294. t1 = turtle.Turtle()
  295. t1.pencolor("#8e072b")
  296. t1.hideturtle()
  297. t1.penup()
  298. t1.write("GAME OVER", align = "center", font=("Silkscreen",20))
  299. t1.setposition(0,-350)
  300. t1.penup()
  301.  
  302.  
  303. if enemy.ycor() > 300 or enemy.ycor() < -300:
  304. print("Game OVER")
  305. t1 = turtle.Turtle()
  306. t1.pencolor("#8e072b")
  307. t1.hideturtle()
  308. t1.penup()
  309. t1.write("GAME OVER", align = "center", font=("Silkscreen",20))
  310. t1.setposition(0,-350)
  311. t1.penup()
  312. quit()
  313.  
  314.  
  315.  
  316. #move bullet
  317. if bulletstate == "fire":
  318. y = bullet.ycor()
  319. y += bulletspeed
  320. bullet.sety(y)
  321.  
  322. if bullet.ycor() > 275:
  323. bullet.hideturtle()
  324. bulletstate = "ready"
  325.  
  326. if bullet.isvisible():
  327. y = bullet.ycor() + bulletspeed
  328. bullet.sety(y)
  329.  
  330.  
  331. wn.mainloop()
  332. delay = input("enter to finish")
  333.  
  334. elif ch.lower()=='e':
  335. print("\nThank you for coming!")
  336. raise SystemExit()
  337. else:
  338. print("\n!!! Invalid input. Try again !!!\n")
  339. main()
  340.  
  341.  
  342. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement