Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #Collier, R. "Lectures Notes for COMP1005B - Introduction to Computer Science I" [PDF documents]. Retrieved
  2. #from cuLearn: https://www.carleton.ca/culearn/ (Winter 2016).
  3.  
  4. from SimpleGraphics import*
  5. import random
  6.  
  7. #Make the Grid
  8. box = True
  9. z = 1
  10. for y in range(0,300,70):
  11. for x in range(0,350,70):
  12. rect(x,y,70,70)
  13. setFont("Times, 24, bold")
  14. text(x+35,y+35,z)
  15. z+= 1
  16. box = not box
  17.  
  18. z=1
  19. for y2 in range(0,300,70):
  20. for x2 in range(450,800,70):
  21. rect(x2,y2,70,70)
  22. setFont("Times, 24, bold")
  23. text(x2+35,y2+35,z)
  24. z+=1
  25. box = not box
  26. box = not box
  27.  
  28. setFont("Times, 50, bold")
  29. text(175,400, "Human")
  30. setFont("Times, 50, bold")
  31. text(625,400, "PC")
  32.  
  33. #Placing the player's ship
  34. player = True
  35. player_ship = int(input("Place your ship from the left "))
  36. while (player):
  37. if (player_ship <= 25) and (player_ship >= 1):
  38. if player_ship <= 5:
  39. player_x = int((player_ship - 1)*70)
  40. elif player_ship <= 10:
  41. player_x = int((player_ship - 6 )*70)
  42. elif player_ship <= 15:
  43. player_x = int((player_ship - 11)*70)
  44. elif player_ship <= 20:
  45. player_x = int((player_ship - 16)*70)
  46. elif player_ship <= 25:
  47. player_x = int((player_ship - 21)*70)
  48. player = False
  49. else:
  50. player_ship = int(input("Place your ship from the left "))
  51. player = True
  52.  
  53.  
  54. if (player_ship <= 25) and (player_ship >= 1):
  55. if player_ship <= 5:
  56. player_y = 0
  57. elif player_ship <= 10:
  58. player_y = (1*70)
  59. elif player_ship <= 15:
  60. player_y = (2*70)
  61. elif player_ship <= 20:
  62. player_y = (3*70)
  63. elif player_ship <= 25:
  64. player_y = (4*70)
  65. player = False
  66. else:
  67. player_ship = int(input("Place your ship from the left "))
  68. player = True
  69.  
  70. #Filling in player's ship at desired coordinated
  71. setFill("yellow")
  72. rect(player_x,player_y,210,70)
  73.  
  74. #Computer opponent choosing the coordinates for its ship
  75. comp_x = (random.randint(0,2)*70+450)
  76. comp_y = (random.randint(0,4)*70)
  77.  
  78. #Player and Computer fire
  79.  
  80. fire = True
  81. ship1 = False
  82. ship2 = False
  83. ship3 = False
  84. ship4 = False
  85. ship5 = False
  86. ship6 = False
  87.  
  88. while (fire):
  89. pTurn = False;
  90. player_fire = int(input("Where will you fire? "))
  91. if (player_fire <= 25) and (player_fire >= 1):
  92. if player_fire <= 5:
  93. fire_x = int(((player_fire - 1)*70)+450)
  94. elif player_fire <= 10:
  95. fire_x = int(((player_fire - 6 )*70+450))
  96. elif player_fire <= 15:
  97. fire_x = int(((player_fire - 11)*70+450))
  98. elif player_fire <= 20:
  99. fire_x = int(((player_fire - 16)*70+450))
  100. elif player_fire <= 25:
  101. fire_x = int(((player_fire - 21)*70+450))
  102. pTurn = True
  103. else:
  104. player_fire = int(input("Where will you fire? "))
  105. pTurn = True
  106.  
  107.  
  108. if (player_fire <= 25) and (player_fire >= 1):
  109. if player_fire <= 5:
  110. fire_y = 0
  111. elif player_fire <= 10:
  112. fire_y = int(1*70)
  113. elif player_fire <= 15:
  114. fire_y = int(2*70)
  115. elif player_fire <= 20:
  116. fire_y = int(3*70)
  117. elif player_fire <= 25:
  118. fire_y = int(4*70)
  119. pTurn = True
  120. else:
  121. player_fire = int(input("Where will you fire? "))
  122. pTurn = True;
  123.  
  124. setFill("blue")
  125. rect(fire_x,fire_y,70,70)
  126. if fire_y == comp_y:
  127. if fire_x == comp_x:
  128. ship4 = True
  129. print("Player Hit!")
  130. setFill("pink")
  131. rect(fire_x,fire_y,70,70)
  132. elif fire_x+70 == comp_x:
  133. ship5 = True
  134. print("Player Hit!")
  135. setFill("pink")
  136. rect(fire_x,fire_y,70,70)
  137. elif fire_x+140 == comp_x:
  138. ship6 = True
  139. print("Player Hit!")
  140. setFill("pink")
  141. rect(fire_x,fire_y,70,70)
  142. else: print("Player Miss!")
  143. if ship4 and ship5 and ship6:
  144. print("Player Wins!")
  145. break
  146.  
  147. comp_x_fire = (random.randint(0,4)*70)
  148. comp_y_fire = (random.randint(0,4)*70)
  149.  
  150. setFill("purple")
  151. rect(comp_x_fire,comp_y_fire,70,70)
  152. print("Computer fires on ",int(comp_y_fire/70),int(comp_x_fire/70))
  153. if player_y == comp_y_fire:
  154. if player_x == comp_x_fire:
  155. ship1 = True
  156. print("Computer Hit!")
  157. setFill("orange")
  158. rect(comp_x_fire,comp_y_fire,70,70)
  159. elif player_x+70 == comp_x_fire:
  160. ship2 = True
  161. print("Computer Hit!")
  162. setFill("orange")
  163. rect(comp_x_fire,comp_y_fire,70,70)
  164. elif player_x+140 == comp_x_fire:
  165. ship3 = True
  166. print("Computer Hit!")
  167. setFill("orange")
  168. rect(comp_x_fire,comp_y_fire,70,70)
  169. else: print("Computer Miss!")
  170. if ship1 and ship2 and ship3:
  171. print("Computer Wins!")
  172. break
  173. fire = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement