Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 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. player_x = 0
  37. player_y = 0
  38. while (player):
  39. if (player_ship <= 25) and (player_ship >= 1):
  40. if player_ship <= 5:
  41. player_x = int((player_ship - 1)*70)
  42. elif player_ship <= 10:
  43. player_x = int((player_ship - 6 )*70)
  44. elif player_ship <= 15:
  45. player_x = int((player_ship - 11)*70)
  46. elif player_ship <= 20:
  47. player_x = int((player_ship - 16)*70)
  48. elif player_ship <= 25:
  49. player_x = int((player_ship - 21)*70)
  50. player = False
  51. else:
  52. player_ship = int(input("Place your ship from the left "))
  53. player = True
  54.  
  55.  
  56. if (player_ship <= 25) and (player_ship >= 1):
  57. if player_ship <= 5:
  58. player_y = 0
  59. elif player_ship <= 10:
  60. player_y = (1*70)
  61. elif player_ship <= 15:
  62. player_y = (2*70)
  63. elif player_ship <= 20:
  64. player_y = (3*70)
  65. elif player_ship <= 25:
  66. player_y = (4*70)
  67. player = False
  68. else:
  69. player_ship = int(input("Place your ship from the left "))
  70. player = True
  71.  
  72. #Filling in player's ship at desired coordinated
  73. setFill("yellow")
  74. rect(player_x,player_y,210,70)
  75.  
  76. #Computer opponent choosing the coordinates for its ship
  77. comp_x = (random.randint(0,2)*70+450)
  78. comp_y = (random.randint(0,4)*70)
  79.  
  80. #Player and Computer fire
  81.  
  82. fire = True
  83. ship1 = False
  84. ship2 = False
  85. ship3 = False
  86. ship4 = False
  87. ship5 = False
  88. ship6 = False
  89.  
  90. while (fire):
  91. pTurn = False;
  92. player_fire = int(input("Where will you fire? "))
  93. if (player_fire <= 25) and (player_fire >= 1):
  94. if player_fire <= 5:
  95. fire_x = int(((player_fire - 1)*70)+450)
  96. elif player_fire <= 10:
  97. fire_x = int(((player_fire - 6 )*70+450))
  98. elif player_fire <= 15:
  99. fire_x = int(((player_fire - 11)*70+450))
  100. elif player_fire <= 20:
  101. fire_x = int(((player_fire - 16)*70+450))
  102. elif player_fire <= 25:
  103. fire_x = int(((player_fire - 21)*70+450))
  104. pTurn = True
  105. else:
  106. player_fire = int(input("Where will you fire? "))
  107. pTurn = True
  108.  
  109.  
  110. while(player_fire <= 25) and (player_fire >= 1):
  111. if player_fire <= 5:
  112. fire_y = 0
  113. elif player_fire <= 10:
  114. fire_y = int(1*70)
  115. elif player_fire <= 15:
  116. fire_y = int(2*70)
  117. elif player_fire <= 20:
  118. fire_y = int(3*70)
  119. elif player_fire <= 25:
  120. fire_y = int(4*70)
  121. pTurn = True
  122. else:
  123. player_fire = int(input("Where will you fire? "))
  124. pTurn = True;
  125.  
  126. setFill("blue")
  127. rect(fire_x,fire_y,70,70)
  128. if fire_y == comp_y:
  129. if fire_x == comp_x:
  130. ship4 = True
  131. print("Player Hit!")
  132. setFill("pink")
  133. rect(fire_x,fire_y,70,70)
  134. elif fire_x+70 == comp_x:
  135. ship5 = True
  136. print("Player Hit!")
  137. setFill("pink")
  138. rect(fire_x,fire_y,70,70)
  139. elif fire_x+140 == comp_x:
  140. ship6 = True
  141. print("Player Hit!")
  142. setFill("pink")
  143. rect(fire_x,fire_y,70,70)
  144. else: print("Player Miss!")
  145. if ship4 and ship5 and ship6:
  146. print("Player Wins!")
  147. break
  148.  
  149. comp_x_fire = (random.randint(0,4)*70)
  150. comp_y_fire = (random.randint(0,4)*70)
  151.  
  152. setFill("purple")
  153. rect(comp_x_fire,comp_y_fire,70,70)
  154. comp_fire = ((comp_x_fire / 70) + 5(comp_y_fire /70))
  155. print("Computer fires on ",int(comp_fire))
  156. if player_y == comp_y_fire:
  157. if player_x == comp_x_fire:
  158. ship1 = True
  159. print("Computer Hit!")
  160. setFill("orange")
  161. rect(comp_x_fire,comp_y_fire,70,70)
  162. elif player_x+70 == comp_x_fire:
  163. ship2 = True
  164. print("Computer Hit!")
  165. setFill("orange")
  166. rect(comp_x_fire,comp_y_fire,70,70)
  167. elif player_x+140 == comp_x_fire:
  168. ship3 = True
  169. print("Computer Hit!")
  170. setFill("orange")
  171. rect(comp_x_fire,comp_y_fire,70,70)
  172. else: print("Computer Miss!")
  173. if ship1 and ship2 and ship3:
  174. print("Computer Wins!")
  175. break
  176. fire = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement