battleship.py - /usr/bin/env python coding utf-8 Game based...

This preview shows page 1 - 3 out of 8 pages.

#!/usr/bin/env python # -*- coding: utf-8 -*- """ Game based on tutorials by Al Sweigart in his book 'Making Games with Python & Pygame" """ # Importing pygame modules from pygame.locals import random, sys, pygame # Set variables, like screen width and height # globals FPS = 30 REVEALSPEED = 8 WINDOWWIDTH = 800 WINDOWHEIGHT = 600 TILESIZE = 40 MARKERSIZE = 40 BUTTONHEIGHT = 20 BUTTONWIDTH = 40 TEXT_HEIGHT = 25 TEXT_LEFT_POSN = 10 BOARDWIDTH = 10 BOARDHEIGHT = 10 DISPLAYWIDTH = 200 EXPLOSIONSPEED = 10 XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * TILESIZE) - DISPLAYWIDTH - MARKERSIZE) / 2) YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * TILESIZE) - MARKERSIZE) / 2) BLACK = ( 0, 0, 0) WHITE = (255, 255, 255) GREEN = ( 0, 204, 0) GRAY = ( 60, 60, 60) BLUE = ( 0, 50, 255) YELLOW = (255, 255, 0) DARKGRAY =( 40, 40, 40) BGCOLOR = GRAY BUTTONCOLOR = GREEN TEXTCOLOR = WHITE TILECOLOR = GREEN BORDERCOLOR = BLUE TEXTSHADOWCOLOR = BLUE SHIPCOLOR = YELLOW HIGHLIGHTCOLOR = BLUE def main(): global DISPLAYSURF, FPSCLOCK, BASICFONT, HELP_SURF, HELP_RECT, NEW_SURF, \ NEW_RECT, SHOTS_SURF, SHOTS_RECT, BIGFONT, COUNTER_SURF, \ COUNTER_RECT, HBUTTON_SURF, EXPLOSION_IMAGES pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) BASICFONT = pygame.font.Font('freesansbold.ttf', 20) BIGFONT = pygame.font.Font('freesansbold.ttf', 50) # create buttons HELP_SURF = BASICFONT.render("HELP", True, WHITE) HELP_RECT = HELP_SURF.get_rect()
Image of page 1

Subscribe to view the full document.

HELP_RECT.topleft = (WINDOWWIDTH - 180, WINDOWHEIGHT - 350) NEW_SURF = BASICFONT.render("NEW GAME", True, WHITE) NEW_RECT = NEW_SURF.get_rect() NEW_RECT.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 200) # 'Shots:' label SHOTS_SURF = BASICFONT.render("Shots: ", True, WHITE) SHOTS_RECT = SHOTS_SURF.get_rect() SHOTS_RECT.topleft = (WINDOWWIDTH - 750, WINDOWHEIGHT - 570) # Explosion graphics EXPLOSION_IMAGES = [ pygame.image.load("img/blowup1.png"), pygame.image.load("img/blowup2.png"), pygame.image.load("img/blowup3.png"),pygame.image.load("img/blowup4.png"), pygame.image.load("img/blowup5.png"),pygame.image.load("img/blowup6.png")] pygame.display.set_caption('Battleship') while True: shots_taken = run_game() show_gameover_screen(shots_taken) def run_game(): revealed_tiles = generate_default_tiles(False) # main board object, main_board = generate_default_tiles(None) ship_objs = ['battleship','cruiser1','cruiser2','destroyer1','destroyer2', 'destroyer3','submarine1','submarine2','submarine3','submarine4'] main_board = add_ships_to_board(main_board, ship_objs) mousex, mousey = 0, 0 counter = [] # counter to track number of shots fired xmarkers, ymarkers = set_markers(main_board) while True: # counter display (it needs to be here in order to refresh it) COUNTER_SURF = BASICFONT.render(str(len(counter)), True, WHITE) COUNTER_RECT = SHOTS_SURF.get_rect() COUNTER_RECT.topleft = (WINDOWWIDTH - 680, WINDOWHEIGHT - 570) # draw the buttons DISPLAYSURF.fill(BGCOLOR) DISPLAYSURF.blit(HELP_SURF, HELP_RECT) DISPLAYSURF.blit(NEW_SURF, NEW_RECT) DISPLAYSURF.blit(SHOTS_SURF, SHOTS_RECT) DISPLAYSURF.blit(COUNTER_SURF, COUNTER_RECT) draw_board(main_board, revealed_tiles) draw_markers(xmarkers, ymarkers) mouse_clicked = False check_for_quit() for event in pygame.event.get(): if event.type == MOUSEBUTTONUP: if HELP_RECT.collidepoint(event.pos): DISPLAYSURF.fill(BGCOLOR) show_help_screen() elif NEW_RECT.collidepoint(event.pos): main() else:
Image of page 2
mousex, mousey = event.pos
Image of page 3
You've reached the end of this preview.
  • Fall '12
  • kay
  • xrange, windowHeight, MarkerSize, tiley

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern