def load_rule(resource_path): global rule try: rule = save.load("int", "logic", 6, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic6.int", resource_path("saves/settings/"), get_language()) rule = 1
def load_background(resource_path): global background try: background = save.load("str", "logic", 1, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic1.str", resource_path("saves/settings/"), get_language()) background = "space"
def set_background(bg, resource_path): global background background = bg try: save.save(background, "str", "logic", 1, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic1.str", resource_path("saves/settings/"), get_language(), False)
def load_language(resource_path): global language try: language = save.load("str", "logic", 5, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic5.str", resource_path("saves/settings/"), "english") language = "english"
def set_difficulty(difficult, resource_path): # setzt die neue Schwierigkeit global difficulty difficulty = difficult try: save.save(difficulty, "str", "logic", 4, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic4.str", resource_path("saves/settings/"), get_language(), False)
def set_rule(rules, resource_path): # setzt die Regeln auf das Angeklickte global rule rule = rules try: save.save(rule, "int", "logic", 6, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic6.int", resource_path("saves/settings/"), get_language(), False)
def save_player(resource_path, language, add_dir): """ saves the list required to continue a game without the player being able to click previously hit fields :param resource_path: Func; returns the resource path to a relative path :param language: str; langauge all texts are currently displayed in :param add_dir: str; additional directory where loaded game is found """ try: save.save(hit_list, 'lis', 'player', 1, resource_path, add_dir) # saves the hit list [[0, 1,...], [], ...] except FileNotFoundError: chat.add_missing_message("", resource_path("saves/"), language, False)
def set_language(sprache, resource_path): """ sets a new language when the user changes the language :param sprache: str; german/english/latin, language the program is now displaying its texts in :param resource_path: Func; returns the resource path to a relative path """ global language language = sprache try: save.save(language, "str", "logic", 5, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic5.str", resource_path("saves/settings/"), get_language(), False)
def _play_tastatur_sound(resource_path): """ plays a clicking sound everytime keyborad input is recognized :param resource_path: Func; returns the resource path to a relative path """ channel4 = pygame.mixer.Channel(3) # chooses channel for keyboard sound try: sound = pygame.mixer.Sound(resource_path("assets/sounds/tastatur.wav")) # takes the keyboard sound except FileNotFoundError: chat.add_missing_message("tastatur.wav", resource_path("assets/sounds/"), get_langauge()) else: sound.set_volume(sound_volume) # sets the volume to the current sound volume channel4.play(sound) # plays keyboard sound
def save_ship(resource_path, language, add_dir): """ saves the ship so that they can be loaded to continue the game :param resource_path: Func; returns the resource path to a relative path :param language: str; language all texts are currently displayed in :param add_dir: str; additional directory where loaded game is found """ try: save.save(ship, 'lis', 'ship', 1, resource_path, add_dir) # saves all ships to saves folder except FileNotFoundError: # directory could not be found chat.add_missing_message("hit.wav", resource_path("saves"), language, False) # displays error message
def save_enemy(resource_path, language, add_dir): """ saves enemy's remaining moves :param resource_path: Func; returns a resource path to a relative path :param language: str; language all texts are currently displayes in :param add_dir: str; additional directory used to save game sfrom different difficulties """ try: save.save(shots, 'lis', 'enemy', 1, resource_path, add_dir) # saves reamining shots of the enemy except FileNotFoundError: chat.add_missing_message("", resource_path("saves/"), language, False) # displays error message
def _play_click_sound(resource_path): """ plays a clicking sound everytime mouse input is recognized :param resource_path: Func; returns the resource path to a relative path """ channel = pygame.mixer.Channel(1) # chooses channel for mouse sound try: sound = pygame.mixer.Sound(resource_path("assets/sounds/click.wav")) # takes the mouse sound except FileNotFoundError: chat.add_missing_message("click.wav", resource_path("assets/sounds/"), get_language()) else: sound.set_volume(sound_volume) # sets the volume to the current sound volume channel.play(sound) # plays mouse sound
def save_playfield(resource_path, language, add_dir): """ saves the playfield, used to continue games after closing the program :param resource_path: Func; returns the resource path to a relative path :param language: str; language all texts are currently dispalyed in :param add_dir: str; additional directory where loaded game is found """ try: save.save(big_fields, 'lis', 'playfield', 1, resource_path, add_dir) # saves board except FileNotFoundError: chat.add_missing_message("", resource_path("saves/"), language, False) else: save.save(small_fields, 'lis', 'playfield', 2, resource_path, add_dir) # saves tiles on the board
def _init_window(resource_path, language): """ initializes the window the game is played in :param resource_path: Func, returns a path to pictures and sounds :param language: str; language all texts are currently displayed in """ global g_screen try: icon = pygame.image.load(resource_path("assets/images/icon.jpg")) # loads icon except pygame.error: # file is not found ch.add_missing_message("icon.jpg", resource_path("assets/images/"), language) else: # file is found pygame.display.set_icon(icon) # creates an icon pygame.display.set_caption("schiffe_versenken v.alpha.4") # creates a caption g_screen = pygame.display.set_mode(get_screen_size(), resizable)
def _play_hit_sound(resource_path, sound_volume, language): """ plays the sound that indicates a ship part being hit :param resource_path: Func; returns the resource path to a relative path :param sound_volume: float; 0 - 1, volume of sounds :param language: str; language all texts are currently displayed in """ channel3 = pygame.mixer.Channel(2) # opens a channel try: sound = pygame.mixer.Sound(resource_path( "assets/sounds/hit.wav")) # creates the sound as a Sound except FileNotFoundError: chat.add_missing_message("hit.wav", resource_path("assets/sounds/"), language) else: sound.set_volume(sound_volume) # sets the volume channel3.play(sound) # plays the sound once
def __init__enemy(load, language, resource_path, length_x, length_y, ship_coordinates=[[[0]]], difficulty="easy", ships=[[[0]]], add_dir=""): """ initializes enemy module and enemy's move :param load: bool; game is loaded and not newly created :param language: str; language all texts are currently displayed in :param resource_path: Func; returns a resoucre path to a relative path :param length_x: int; board size in x direction :param length_y: int; board size in y direction :param ship_coordinates: list[list[int, int], ...]; list with all coordiantes inhabiting a ship :param difficulty: str; easy/medium/hard/impossible :param ships: list[list[list[Ship, ...], ...], ...]; list containing all ships :param add_dir: str; additional directory used to save game sfrom different difficulties :return: bool or None; loading failed """ global shots if load: # game is laoded try: shots = save.load('lis', 'enemy', 1, resource_path, add_dir) # shots are loaded except FileNotFoundError: chat.add_missing_message("enemy1.lis", resource_path("saves/"), language) # adds message to chat return True # interrupts loading and creates new game else: # game is newly created shots = [ [2, 2] ] # sets a start value so that in game function can be more easily readable _create_moves_enemy(difficulty=difficulty, field_count=(length_x, length_y), ship_coordinates=ship_coordinates, ships=ships) # creates enemy's moves
def _play_music(volume): """ plays the background music :param volume: float; volume of the music between 0 and 1 """ global channel5 global music channel5 = pg.mixer.Channel(4) # chooses a channel try: music = pg.mixer.Sound( resource_path("assets/music/deepblue.wav")) # gets the music except FileNotFoundError: chat.add_missing_message("deepblue.wav", resource_path("assets/music/"), lg.get_language()) else: music.set_volume(volume) # sets the sound of the music channel5.play(music, loops=-1) # plays the music
def __init__logic(resource_path): global win_type global aufgabe global zustand load_language(resource_path) try: theme_num = save.load('int', 'logic', 7, resource_path, 'settings/') except FileNotFoundError: chat.add_missing_message("logic7.int", resource_path("saves/settings/"), get_language()) theme_num = 1 load_theme(resource_path, theme_num) load_background(resource_path) load_music_volume(resource_path) load_sound_volume(resource_path) load_difficulty(resource_path) load_rule(resource_path) aufgabe = "main" zustand = "start" win_type = "fullscreen" ip.__init__input()
def __init__playfield(load, language, orienation, field_size, field_count_x, field_count_y, resource_path, add_dir): """ creates boards and their tiles as well as writing on boards' side when game is loaded, loads boards and their tiles instead :param load: bool; game is loaded insted of being newly created :param language: str; language all texts are currently displayed in :param orienation: str; width/height, depending on what is bigger :param field_size: float; size of a virtual field that is determined by the size of the window that inhabits the GUI :param field_count_x: int; quantity of tiles on one board in horizontal direction :param field_count_y: int; quantity of tiles on one board in vertical direction :param resource_path: Func; returns the resource path to a relative path :param add_dir: str; additional directory where loaded game is found """ if load: # saved game is loaded global big_fields global small_fields try: big_fields = save.load('lis', 'playfield', 1, resource_path, add_dir) # loads both boards except FileNotFoundError: chat.add_missing_message("playfield1.lis", resource_path("saves/"), language) return True try: small_fields = save.load('lis', 'playfield', 2, resource_path, add_dir) # loads boards' tiles except FileNotFoundError: chat.add_missing_message("playfield2.lis", resource_path("saves/"), language) return True else: # new game is created _create_big_fields(field_size, field_count_x, field_count_y, orienation) # creates both boards _create_small_fields(field_size, orienation) # creates boards' tiles _create_writings(field_size) # creates writings on the side of the boards
def __init__player(load, resource_path, language, add_dir): """ initializes player and its required lists to check for doublicating inputs :param load: bool; whether the game is loaded or newly created :param resource_path: Func; returns the resource path to a relative path :param language: str; lnguage all texts are currently displayed in :param add_dir: str; additional directory where loaded game is found """ global hit_list global feld global done_clicks feld = [-1, -1] # creates a list that contains the currently clicked field with a control value done_clicks = [[0, 0]] # creates control to undo clicks when another field is clicked instead if load: # loads control for already targeted fields try: hit_list = save.load('lis', 'player', 1, resource_path, add_dir) except FileNotFoundError: chat.add_missing_message("player1.lis", resource_path("saves/"), language) return True else: # creates control for already targeted fields hit_list = [[0 for _ in range(10)] for _ in range(10)]
def _draw_background(screen, background, resource_path, language): """ displays background :param screen: Surface; surface covering the whole window :param background: str; picture displayed as background :param resource_path: Func; returns the full resource path when given a relative path :param language: str; language all texts are currently dispalyed in """ global ocean_new global space_new global drawn_new if background == "ocean": try: bg = pygame.image.load(resource_path("assets/images/ocean.jpg")) # loads ocean picture except pygame.error: # picture flle is not found if ocean_new: ch.add_missing_message("ocean.jpg", resource_path("assets/images/"), language) ocean_new = False elif background == "space": try: bg = pygame.image.load(resource_path("assets/images/space.jpg")) # loads space picture except pygame.error: # picture file is not found if space_new: ch.add_missing_message("space.jpg", resource_path("assets/images/"), language) space_new = False else: # background == "drawn" try: bg = pygame.image.load(resource_path("assets/images/drawn.jpg")) # loads ugly picture except pygame.error: # pictue file is not found if drawn_new: ch.add_missing_message("drawn.jpg", resource_path("assets/images/"), language) drawn_new = False try: screen.blit(bg, (0, 0)) # displays loaded picture except NameError: # loading a picture failed screen.fill(BLACK) # fills screen black
def set_ships(load, resource_path, language, add_dir, field_size, random_placement=True, normal=False): """ creates ships as Ship, in a way that the player's ships are in ship[0] and the enemy's ships are in ship[1] and places all ships on random locations on the playfield Ship is a ship that can be hit, located on the playfield and shown in the GUI used once in the beginning of the game :param load: bool; whether game is loaded or a new one is created :param resource_path: Func; returns the resource_path to a relative_path :param language: str; language all texts are currently displayed in :param add_dir: str; additional directory where loaded game is found :param field_size: float; size of one virtual field :param normal: ships are set by the player :param random_placement: bool; ships are placed randomly and not by the player """ global ship if load: # game is loaded try: ship = save.load('lis', 'ship', 1, resource_path, add_dir) # loads ships except FileNotFoundError: # ships file has not been found chat.add_missing_message("ship1.lis", resource_path("saves/"), language) # adds message to chat return True # interrupts loading and creates new game elif normal: _set_rand_pos(True) else: # game is newly created ship = [] # creates list later holding all ships for i in range(2): # creates individual list for each palyer ship.append([]) for j in range(ship_count): # goes through number of ships ship[i].append(0) # creates ten ships for each player that are intact, but are not yet located anywhere on the playfield for j in range( 4 ): # creates four ships as FisherShip2, a Ship with two segments ship[i][j] = Ship([[-1, -1, 3], [-1, -1, 3]], identification_number=j + 1, player=i, color=(255, 50 * j, 255), length=2, field_size=field_size) for j in range( 3 ): # creates three ships as FisherShip2, a Ship with three segments d = j + 4 ship[i][d] = Ship([[-1, -1, 3], [-1, -1, 3], [-1, -1, 3]], identification_number=d + 1, player=i, color=(50 * j, 100, 255), length=3, field_size=field_size) # creates two ships as ContainerShip4, a Ship with four segments ship[i][7] = Ship( [[-1, -1, 3], [-1, -1, 3], [-1, -1, 3], [-1, -1, 3]], identification_number=8, player=i, color=(100, 255, 50), length=4, field_size=field_size) ship[i][8] = Ship( [[-1, -1, 3], [-1, -1, 3], [-1, -1, 3], [-1, -1, 3]], identification_number=9, player=i, color=(100, 255, 100), length=4, field_size=field_size) # creates one ship as Kreuzer5, a ship wih five segments ship[i][9] = Ship([[-1, -1, 3], [-1, -1, 3], [-1, -1, 3], [-1, -1, 3], [-1, -1, 3]], identification_number=10, player=i, color=(255, 100, 0), length=5, field_size=field_size) if random_placement: _set_rand_pos() # sets all ships to random positions else: _set_default_pos(field_size, resource_path)