Battleship_5_2.py - import pygame from math import from...

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

import pygame from math import * from Ship_Class import Block import time from Enemy_ship_placement import AI_placeships from shootingAI import AIfire pygame.init() # This is a youtube link to help learn # # Colors black = (50, 50, 50) white = (255, 255, 255) grey = (122, 122, 122) dark_grey = (75, 75, 75) maroon = (80, 0, 0) red = (200, 0, 0) bright_red = (235, 0, 0) light_grey = (200, 200, 200) # Set display info display_width = 800 display_height = 800 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption("Battleship") # These are the images Up = pygame.transform.scale(pygame.image.load("Up.png"), (40, 98)) Up_rect = Up.get_rect() Down = pygame.transform.scale(pygame.image.load("Down.png"), (40, 98)) Down_rect = Down.get_rect() Left = pygame.transform.scale(pygame.image.load("Left.png"), (98, 40)) Left_rect = Left.get_rect() Right = pygame.transform.scale(pygame.image.load("Right.png"), (98, 40)) Right_rect = Right.get_rect() clock = pygame.time.Clock() #AI Firing hitShot = [] def quit_function(): """ This will allow the user to quit using the exit button built into Windows. :return: None """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() def text_objects(text, font, color=white): """ This creates text objects :param text: string :param font: some font :param color: color, tuple, default white :return: text surface, text rectangle """ textsurface = font.render(text, True, color) return textsurface, textsurface.get_rect()
def create_text(text, font, color=white, position=(0, 0), anchor="topleft", should_return=False): """ This will create text and blit it to the screen at the position :param text: Some string :param font: Some font :param color: Some color, either in variable form or tuple. :param position: tuple position,(x,y) :param anchor: This is the anchor point. :param should_return: If true, will return surface and rect :return: None, blits to screen. If should_return=True, then returns surface and rect """ text_surface, text_rectangle = text_objects(text, font, color) if anchor == "topleft": text_rectangle.topleft = position elif anchor == "center": text_rectangle.center = position elif anchor == "topright": text_rectangle.topright = position else: raise AttributeError # This will give an error for the programmer if should_return: return text_surface, text_rectangle gameDisplay.blit(text_surface, text_rectangle) def convert_to_chunk(position, team, board_size=325, num_squares=8): """ This will calculate the chunk for ease :param position: pixel position :param team: board team, player one or player two :param board_size: size of the board :param num_squares: number of squares :return: tuple (chunx,chunky) """ posx, posy = position if team.lower().strip() == "player one": # This is for the player one board board_pos = (50, 237) try: pass except: if board_pos[0] + board_size < posx or board_pos[1] + board_size < posy: raise ValueError("The position of the block exceeds the board") elif team.lower().strip() == "player two": # This is for the player two board board_pos = (425, 237) try: pass except: if board_pos[0] + board_size < posx or board_pos[1] + board_size < posy: raise ValueError("The position of the block exceeds the board") else: raise AttributeError("The team given does not match any on record") # programmer error

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture