def main(self): while self.run: self.clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False self.quit() if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() if self.state == "Game": self.check_collision(pos) elif self.state == "Title": if self.play_btn.isOver(pos): self.state = "Game" elif self.quit_btn.isOver(pos): self.run = False self.quit() elif self.state == "PostLostGame" or self.state == "PostWonGame": if self.end_btn.isOver(pos): self.run = False self.quit() elif self.replay_btn.isOver(pos): Game.GUESSED = "" Game.HANGMAN_STATUS = 0 Game.WON = [False] self.letter_coordinates = self.load_buttons() self.word = random_word(self.conn)[1] self.state = "Game" self.draw() self.win_status(Game.WON, Game.HANGMAN_STATUS)
def start_level_setting(): """Sets the initial game variables.""" if game_level == 1: # Easy level set_pattern(words.random_word(level.words_pool()[0])) set_masked_word(words.mask_word(pattern)) set_lives(art.art_easy()[1]) set_level_txt(level.level_name(1)) set_art_img(art.art_easy()[0][lives]) elif game_level == 2: # Medium level set_pattern(words.random_word(level.words_pool()[1])) set_masked_word(words.mask_word(pattern)) set_lives(art.art_medium()[1]) set_level_txt(level.level_name(2)) set_art_img(art.art_medium()[0][lives]) elif game_level == 3: # Hard level set_pattern(words.random_word(level.words_pool()[2])) set_masked_word(words.mask_word(pattern)) set_lives(art.art_hard()[1]) set_level_txt(level.level_name(3)) set_art_img(art.art_hard()[0][lives])
def __init__(self): pygame.init() self.conn = db_connect(str(Game.DIR / "db" / "words.db")) self.word = random_word(self.conn)[1] self.game_font = pygame.freetype.Font( str(Game.DIR / "fonts" / "classic.TTF"), 26) self.word_font = pygame.freetype.SysFont("comicsans", 50) self.win = pygame.display.set_mode((Game.WIDTH, Game.HEIGHT)) self.caption = pygame.display.set_caption("HangThePyMan") self.gameIcon = pygame.image.load(str(Game.DIR / "icon" / "icon.png")) self.replayIcon = pygame.image.load( str(Game.DIR / "icon" / "replay.png")) self.replayIcon = pygame.transform.scale(self.replayIcon, (50, 50)) self.endIcon = pygame.image.load(str(Game.DIR / "icon" / "cross.png")) self.endIcon = pygame.transform.scale(self.endIcon, (50, 50)) self.icon = pygame.display.set_icon(self.gameIcon) self.clock = pygame.time.Clock() self.b_color = (255, 255, 255) self.run = True self.images = self.load_images() self.letter_coordinates = self.load_buttons() self.state = "Title" self.play_btn = Button((37, 164, 206), 300, 150, 200, 100, "Play") self.quit_btn = Button((37, 164, 206), 300, 300, 200, 100, "Quit") self.replay_btn = Button((37, 164, 206), 315, 300, 50, 50, image=self.replayIcon, imagepos=(315, 300)) self.end_btn = Button((37, 164, 206), 415, 300, 50, 50, image=self.endIcon, imagepos=(415, 300)) try: self.version = pkg_resources.get_distribution( "hangthepyman").version except Exception: self.version = "Beta"
def generate(): '''Create translations for num_words most frequent English words''' print('Generating dictionary...') for english_word in tqdm(english.words[:settings.num_words]): if english_word in dictionary: continue if not text.is_word(english_word): dictionary[english_word] = english_word continue compare_to = comparison_sample(english_word) dictionary[english_word] = words.random_word() score = translation_score(english_word, dictionary[english_word], compare_to) for i in trange(settings.num_alternatives): candidate = words.random_edit(dictionary[english_word]) candidate_score = translation_score(english_word, candidate, compare_to) if candidate in dictionary: continue if candidate_score > score: dictionary[english_word] = candidate score = candidate_score
import random from words import random_word # random_word() contains a random word from the word list(words.txt) from images import IMAGES # IMAGES contains a list of images of hangman according to the levels form images.py # all predefined details for the game. lives = 8 random_word = random_word() word_length = len(random_word) hangman = ("_ " * word_length).split() guessed_chars = '' print("Welcome to the Hangman game (word guessing game)\n", "length of word is ", len(hangman)) while lives > 0: print(" lives :", lives, '\n', 'hangman :', (' ').join(hangman), '\n', 'guessed chars :', guessed_chars, '\n') # checking the condition for win(guessed the word) if ('').join(hangman) == random_word: print("Hurrehhh! You won the game :)") break # taking user input in single character (to guess the word) user = input('guess a character for the word : ') user = ((' ').join(user).split()[0]) # condition to check if the character exists in word, # if exists then it will show in hangman otherwise it will show hangman and decrease the lives of the man if user in random_word: for i in range(len(random_word)): if user == random_word[i]: hangman[i] = user
while True: value = '' guess_letter = input('Guess a Letter by user:\n ') if guess_letter == 'hint' and hint_chance == 0: print(Hint(secret_word)) hint_chance = 1 continue else: print('You have already used Hint ') guess_word.append(guess_letter) if guess_letter in secret_word: value = get_word(guess_word, secret_word) print('Good guess: ', value) else: guess_chance += 1 show_image = image_level[counter] counter += 1 print(image[show_image]) if value == secret_word: print('You won the game!\n Thank you for playing! ') break elif (gave_chance - guess_chance) == 0: print('You lost the game! secret code is=', secret_word) break secret_word = random_word() hangman(secret_word)
#!/usr/bin/env python3.7 #from words import random_word, random_words --- This WORKS #from words import random_words --- This FAILS #from words import random_word --- This WORKS from words import random_word print(random_word())
def random_word(): return words.random_word()