Advanced Battleship with Python!

edited May 2016 in Python Mode

Hey, folks! I've asked a couple questions recently on how to get past some specific roadblocks that I ran into while working on this project, but I thought an open discussion could lead to more responses and keep things a little broader. So here's where we are: The window opens and does some theatrical opening sequence, and then you get a bit of a run down, basically just telling you that the game you're playing is battleship. Here's the code:

## Dshango
## BattleShip
import time
startTime = millis()
STATES = 7
flashOff, flashOn, TerminalOn, instructions, ship_placement, PlayerTurn, PCTurn, post_game = range(8)
state = 0
colors = {'red':'#FF0000', 'yellow':'#FFFF00', 'grey':'#666666', 
          'green':'#00FF00', 'sea':'#000066', 'blue':'#0000FF',
          'white':'#FFFFFF', 'black':'#000000', 'grid':'#83F52C'}

x = 1500
y = x / 2
def setup():
    size(x,int(y))
    global z, count, dharma, gridfont, terminalfont
    global a, b, c, d, e, f, g, h, i, j, k
    a, b, c, d, e, f, g, h, i, j = -y+4,-y+4,-y+4,-y+4,-y+4,-y+4,-y+4,-y+4,-y+4,-y+4
    frameRate(60)
    dharma = loadImage('hydra.png')
    z = 0
    count = 0
    gridfont = createFont('RADAR.ttf',y * .04)
    terminalfont = createFont('DOS.ttf',y * .02)
    textAlign(CENTER, CENTER)

################################################### Radar Grid ##########

def grid():
    fill(0,10)
    stroke(colors['grid'])
    rect(0,0, y, y-1)
    fill(colors['grid'])
    textFont(gridfont)
    textAlign(CENTER, CENTER)

    ## Longs
    linestart = y * .05
    figurestart = y * .1
    for n in range(10):
        line(linestart,0, linestart,1000)
        text(n+1, figurestart,y * .025)
        linestart += (y * .095)
        figurestart += (y * .095)

    ## Lat
    linestart = y * .05
    figurestart = y * .1
    lats = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    for n in range(10):
        line(0,linestart, y,linestart)
        text(lats[n], y * .025,figurestart)
        linestart += (y * .095)
        figurestart += (y * .095)

    fill(0,10)
    stroke(0,0)
    rect(0, 0, width, height)

################################################### Sweeping Radar ######

def sweep():
    global z, count
    speed = 1.0
    stroke(colors['grid'])
    line(z,y * .05, z,height)
    z += speed
    z = constrain(z, y * .05, y)
    if z >= y - 1 and z < y:
        z = 0
        count += 1
    if count == 5:
        #z = y
        pass

################################################### Terminal start ######

def intro():
    global dharma
    global startTime
    fill(colors['black'])
    stroke(colors['black'])
    image(dharma, y+y*.25,y*.25, y*.5,y*.5)

################################################### Terminal Text #######

def introText():
    startTime = 8000
    stroke('#83F52C')
    fill('#83F52C')
    text("Dharma Initiative  |  Arknet 2.4.68", y + .5*y, y * .1)
    text('Station 4: The Hydra', y + .5*y, y * .8)
    textFont(terminalfont)
    if millis() > startTime + 2000:
        text('LOAD RADZINSKI.001?', y+.5*y, y*.85)
    if millis() > startTime + 3000:
        text('(Y/N)', y+.5*y, y*.9)

################################################### Instructions ########

def instructions():
    global x, y
    global a, b, c, d, e, f, g, h, i, j
    textFont(terminalfont)
    stroke(colors['grid'])
    fill(0)
    rect(y,0, y-1,y*.335)

    #### TEXT ####
    stroke(colors['grid'])
    fill(colors['grid'])
    text('>: CAPTAIN RADZENSKI!', x*.52,y*.02)
    text('>: THANK HEAVENS YOU\'VE ARRIVED!', x*.52,y*.0425)
    text('>: THE ENEMY FLEET HAS ALREADY MOVED INTO HOSTILE POSITIONS!', x*.52,y*.065) 
    text('>: WE CURRENTLY HAVE ONLY FOUR VESSELS AT OUR DISPOSAL: ', x*.52,y*.0875)
    text('>: THE USAS STALINGRAD, A NUCLEAR POWERED ASSAULT CARRIER', x*.52,y*.1100)
    text('>: TWO ZUMWALT CLASS DESTROYERS, THE USNS ARMAGEDDON AND THE USS AURORAS. ', x*.52,y*.1325)
    text('>: AND A NEARBY COASTGUARD PATROL BOAT, THE "CODFATHER". ', x*.52,y*.1550)
    text('>: THEY\'VE MANAGED TO SCRAMBLE OUR RADAR, SO WE\'LL BE FIRING BLIND.', x*.52,y*.1775)
    text('>: HOWEVER, IT WILL WORK WELL ENOUGH TO SEE OUR OWN SHIPS.', x*.52,y*.2000)
    text('>: CLICK ON THE COORDINATES OF THE GRID THAT YOU WOULD LIKE TO MOVE YOUR VESSELS.', x*.52,y*.2225) 


    #### BLOCKERS ####
    speed = x * .003
    stroke(colors['black'])
    fill(colors['black'])
    rect(x-2,y*.0200, a,y*.0225)
    rect(x-2,y*.0425, b,y*.0225)
    rect(x-2,y*.0650, c,y*.0225)
    rect(x-2,y*.0875, d,y*.0225)
    rect(x-2,y*.1100, e,y*.0225)
    rect(x-2,y*.1325, f,y*.0225)
    rect(x-2,y*.1550, g,y*.0225)
    rect(x-2,y*.1775, h,y*.0225)
    rect(x-2,y*.2000, i,y*.0225)
    rect(x-2,y*.2225, j,y*.0225)
    if a < -4:
         a += speed
    if a >= -4 and b < -4:
         b += speed
    if b >= -4 and c < -4:
         c += speed
    if c >= -4 and d < -4:
         d += speed
    if d >= -4 and e < -4:
         e += speed
    if e >= -4 and f < -4:
         f += speed
    if f >= -4 and g < -4:
         g += speed
    if g >= -4 and h < -4:
         h += speed
    if h >= -4 and i < -4:
         i += speed
    if i >= -4:
         j += speed
    print j

################################################### State Handler #######

#def placement():



################################################### State Handler #######

def handler():
    global state
    def keyPressed():
        global state
        if state == 2 and key == 'y':
            state += 1

     #### Flash State ####
    startTime = millis()
    FlashTime = 2500
    BlinkTime = 2000
    count = 1 
    iterations = 4
    for count in range(iterations):
       if startTime >= BlinkTime:
           startTime = millis()
           if state == flashOff:
               state = flashOn 
       if startTime > FlashTime:
           if state == flashOn:
               state = flashOff
               BlinkTime += 1500
               FlashTime += 1500

    #### Terminal On State ####
    if startTime >= 2000 + 1500 * iterations:
        state = TerminalOn 

    #### Instruction State ####
    keyPressed()

    #### Ship Placement State ####
    global j
    if  j >= 500:
        state = 4
    print state

################################################### Main ################

def draw():
    #background(colors["grey"])
    grid()
    handler()
    fill(0, 2)
    rect(0, 0, width, height)
    if state == 0:
        colors['grid'] = '#666666'
    if state == 1:
        intro()
        colors['grid'] = '#666666'
    if state == 2:
        intro()
        colors['grid'] = '#666666'
        introText()
    if state == 3:
        colors['grid'] = '#83F52C'
        textAlign(LEFT, TOP)
        sweep()
        instructions()
    #if state == 4:

So what we're going for from here is: - Class for grid cells to allow ship placement, picking where to fire, etc - Ship Placement function itself - A turn system for gameplay (human, pc, human, pc) - An AI so that the opponents turn isn't something simple and constant like A1, B1, C1... - And maybe when all of that is finished there can be like random events like vortexes and shark attacks and submarines, to keep things interesting.

Link to the project folder: https://www.dropbox.com/sh/6926lz77lm3qwzz/AAB9Fje1AZH9XA3L78CElVqAa?dl=0

Voice your input! Let's see what we can come up with in one night!

Tagged:
Sign In or Register to comment.