Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Collier, R. "Lectures Notes for COMP1005B - Introduction to Computer Science I" [PDF documents]. Retrieved
- #from cuLearn: https://www.carleton.ca/culearn/ (Winter 2016).
- from SimpleGraphics import*
- import random
- #Make the Grid
- box = True
- z = 1
- for y in range(0,300,70):
- for x in range(0,350,70):
- rect(x,y,70,70)
- setFont("Times, 24, bold")
- text(x+35,y+35,z)
- z+= 1
- box = not box
- z=1
- for y2 in range(0,300,70):
- for x2 in range(450,800,70):
- rect(x2,y2,70,70)
- setFont("Times, 24, bold")
- text(x2+35,y2+35,z)
- z+=1
- box = not box
- box = not box
- setFont("Times, 50, bold")
- text(175,400, "Human")
- setFont("Times, 50, bold")
- text(625,400, "PC")
- #Placing the player's ship
- player = True
- player_ship = int(input("Place your ship from the left "))
- player_x = 0
- player_y = 0
- while (player):
- if (player_ship <= 25) and (player_ship >= 1):
- if player_ship <= 5:
- player_x = int((player_ship - 1)*70)
- elif player_ship <= 10:
- player_x = int((player_ship - 6 )*70)
- elif player_ship <= 15:
- player_x = int((player_ship - 11)*70)
- elif player_ship <= 20:
- player_x = int((player_ship - 16)*70)
- elif player_ship <= 25:
- player_x = int((player_ship - 21)*70)
- player = False
- else:
- player_ship = int(input("Place your ship from the left "))
- player = True
- if (player_ship <= 25) and (player_ship >= 1):
- if player_ship <= 5:
- player_y = 0
- elif player_ship <= 10:
- player_y = (1*70)
- elif player_ship <= 15:
- player_y = (2*70)
- elif player_ship <= 20:
- player_y = (3*70)
- elif player_ship <= 25:
- player_y = (4*70)
- player = False
- else:
- player_ship = int(input("Place your ship from the left "))
- player = True
- #Filling in player's ship at desired coordinated
- setFill("yellow")
- rect(player_x,player_y,210,70)
- #Computer opponent choosing the coordinates for its ship
- comp_x = (random.randint(0,2)*70+450)
- comp_y = (random.randint(0,4)*70)
- #Player and Computer fire
- fire = True
- ship1 = False
- ship2 = False
- ship3 = False
- ship4 = False
- ship5 = False
- ship6 = False
- while (fire):
- pTurn = False;
- player_fire = int(input("Where will you fire? "))
- if (player_fire <= 25) and (player_fire >= 1):
- if player_fire <= 5:
- fire_x = int(((player_fire - 1)*70)+450)
- elif player_fire <= 10:
- fire_x = int(((player_fire - 6 )*70+450))
- elif player_fire <= 15:
- fire_x = int(((player_fire - 11)*70+450))
- elif player_fire <= 20:
- fire_x = int(((player_fire - 16)*70+450))
- elif player_fire <= 25:
- fire_x = int(((player_fire - 21)*70+450))
- pTurn = True
- else:
- player_fire = int(input("Where will you fire? "))
- pTurn = True
- while(player_fire <= 25) and (player_fire >= 1):
- if player_fire <= 5:
- fire_y = 0
- elif player_fire <= 10:
- fire_y = int(1*70)
- elif player_fire <= 15:
- fire_y = int(2*70)
- elif player_fire <= 20:
- fire_y = int(3*70)
- elif player_fire <= 25:
- fire_y = int(4*70)
- pTurn = True
- else:
- player_fire = int(input("Where will you fire? "))
- pTurn = True;
- setFill("blue")
- rect(fire_x,fire_y,70,70)
- if fire_y == comp_y:
- if fire_x == comp_x:
- ship4 = True
- print("Player Hit!")
- setFill("pink")
- rect(fire_x,fire_y,70,70)
- elif fire_x+70 == comp_x:
- ship5 = True
- print("Player Hit!")
- setFill("pink")
- rect(fire_x,fire_y,70,70)
- elif fire_x+140 == comp_x:
- ship6 = True
- print("Player Hit!")
- setFill("pink")
- rect(fire_x,fire_y,70,70)
- else: print("Player Miss!")
- if ship4 and ship5 and ship6:
- print("Player Wins!")
- break
- comp_x_fire = (random.randint(0,4)*70)
- comp_y_fire = (random.randint(0,4)*70)
- setFill("purple")
- rect(comp_x_fire,comp_y_fire,70,70)
- comp_fire = ((comp_x_fire / 70) + 5(comp_y_fire /70))
- print("Computer fires on ",int(comp_fire))
- if player_y == comp_y_fire:
- if player_x == comp_x_fire:
- ship1 = True
- print("Computer Hit!")
- setFill("orange")
- rect(comp_x_fire,comp_y_fire,70,70)
- elif player_x+70 == comp_x_fire:
- ship2 = True
- print("Computer Hit!")
- setFill("orange")
- rect(comp_x_fire,comp_y_fire,70,70)
- elif player_x+140 == comp_x_fire:
- ship3 = True
- print("Computer Hit!")
- setFill("orange")
- rect(comp_x_fire,comp_y_fire,70,70)
- else: print("Computer Miss!")
- if ship1 and ship2 and ship3:
- print("Computer Wins!")
- break
- fire = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement