Advertisement
c0d3dsk1lls

Hang-Man game, CodedSkills.net

Aug 6th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.48 KB | None | 0 0
  1. #====================================================================================================================================================================================================================
  2. #=============================#-----------------------------------------------------------------------+------------------------------+
  3. #-----------------------------#----------------------------------------------------------------------------------------------------+-----------------------------------+
  4. #       HANG-MAN GAME         #-----------------------------------------------------------------------------------+-----------------------------+----------------------------------+              
  5. #=============================#----------------------------------------------------------+----------------+-------------------+----------------------------+---------+  
  6. #====================================================================================================================================================================================================================
  7. #-----------------------#-------+---------+----------------------------------------+------------------------------------------------+
  8. import pygame  #--------#-------+##-----------------------+------------------------------------+
  9. import random  #--------#-------+##------------------+-------------------------+------------------------------------------+
  10. from tkinter import *   #-------+##----+-------------------------+-------------------+---------------------------+-------------------------------------------------------+
  11. import random  #--------#-------+##------------+-------------------------+-------------------+--------------+-------------------------------------------+
  12. #--------------#----------------+##-------------------+-------------------------+-------------------+-------------------+-------------------------------------+
  13. #++++++++++++++++++++++++++++++++##----+------------------------------+-------------+----------------------------------------------------+-------------------------------------------+
  14. #=============================================================================================================================+
  15. #-------------------------------------------------------------------#-------------------------------------+-------------------+
  16. #-------------------------------------------------------------------#
  17. pygame.init() #-----------------------------------------------------#
  18. winHeight = 600 #---------------------------------------------------#
  19. winWidth = 800 #----------------------------------------------------#
  20. win=pygame.display.set_mode((winWidth,winHeight)) #-----------------#
  21. #-------------------------------------------------------------------#
  22. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  23. #----------ALLOWS SCREEN TO BE RESIZED------------------------------#
  24. # Form screen with 400x400 size ------------------------------------#
  25. # and with resizable -----------------------------------------------#
  26. screen = pygame.display.set_mode((800, 500 ), #---------------------#
  27.                                  pygame.RESIZABLE) #----------------#
  28. # set title---------------------------------------------------------#
  29. pygame.display.set_caption('CodedSkills.net :  _._._Hang-Man_._._') #
  30. #-------------------------------------------------------------------#---------------------------------------+-------------------+
  31. #---------------------------------------#
  32. # initialize global variables/constants #
  33. #---------------------------------------#
  34. BLACK = (0,0, 0)     ##
  35. WHITE = (255,255,255)##
  36. RED = (255,0, 0)     ##
  37. GREEN = (0,255,0)    ##
  38. BLUE = (0,0,255)     ##
  39. LIGHT_BLUE = (0,0,0) ##
  40. #--------------------##
  41. #------------------------------------------------#----------+----------------+-------+----------------------------+
  42. btn_font = pygame.font.SysFont("monospace", 20)  #----+--------+---
  43. guess_font = pygame.font.SysFont("monospace", 24)#-------+--------+---
  44. lost_font = pygame.font.SysFont('monospace', 30) #----------+--------+---
  45. word = ''#   #-----------------------------------#------------+--------+---
  46. #------------#
  47. buttons = [] #
  48. guessed = [] #-----------------------------------#
  49. hangmanPics = [pygame.image.load('hangman0.png'),#
  50.                pygame.image.load('hangman1.png'),#
  51.                pygame.image.load('hangman2.png'),#
  52.                pygame.image.load('hangman3.png'),#
  53.                pygame.image.load('hangman4.png'),#
  54.                pygame.image.load('hangman5.png'),#
  55.                pygame.image.load('hangman6.png')]#
  56. #------------#-----------------------------------#
  57. limbs = 0    #
  58. #------------#
  59. #---------------------------#--------+--------------------------+
  60. def redraw_game_window():   #
  61.     global guessed          #
  62.     global hangmanPics      #
  63.     global limbs            #
  64.     win.fill(RED)           #
  65. #---------------------------#
  66. #----Buttons---------------------#
  67.     for i in range(len(buttons)):#
  68.         if buttons[i][4]:#-------#--#
  69.             pygame.draw. circle(win,#
  70.                          BLACK,#----#-------#
  71.                         (buttons[i][1],     #
  72.                          buttons[i][2]),    #
  73.                          buttons[i][3])     #
  74. #-------------------------------------------#
  75. #-----------------------+----------------------------+------------------------------+---------------------------+--------------+
  76.             pygame.draw.circle (win, #--------------#
  77.                                 buttons[i][0], #----#
  78.                                (buttons[i][1], #----#
  79.                                 buttons[i][2]),#----#
  80.                                 buttons[i][3] - 2)  #
  81. #---------------------------------------------------#
  82. #------------------------------------------------------------------------+-----------------------------+------------------------+-----------------------------------------------------+
  83. #----------------------------------------------------------#
  84.             label = btn_font.render(chr(buttons[i][5]),    #
  85.                                      1, WHITE)             #
  86. #----------------------------------------------------------#-----------------------------------------------------------#
  87. #++++++++++++++++++++++++++++++++++++++++++++                                                                          #
  88. #----------------------------------------------------------------------------------------------------------------------#
  89.             win.blit(label, (buttons[i][1] - (label.get_width() / 2), buttons[i][2] - (label.get_height() / 2)))       #-----------+---------+
  90. #----------------------------------------------------------------------------------------------------------------------#
  91. #++++++++++++++++++++++++++++++++++++++++++++
  92. #--------------------------------------------------#
  93.     spaced = spacedOut(word, guessed) #------------#
  94.     label1 = guess_font.render(spaced, 1, BLACK)   #--------------------+-------------------------------------------------------------------------------------------+
  95.     rect = label1.get_rect()   #-------------------#
  96.     length = rect[2]    #------#
  97. #----------------------------------------------------------------#--------------------------+
  98. #++++++++++++++++++++++++++++++++++++++++++++
  99. #-----------------------#----------------------------------------#----------+
  100.     win.blit(label1,(winWidth/2 - length/2, 400))                #--------------+
  101.     pic = hangmanPics[limbs]                                     #---------------------------------+
  102.     win.blit(pic, (winWidth/2 - pic.get_width()/2 + 20, 150))    #----------------------------------------------------+
  103.     pygame.display.update()                                      #---------+
  104. #----------------------------------------------------------------#---+-------------+
  105. #++++++++++++++++++++++++++++++++++++++++++++
  106. #---------------------------#----------------------+------------------------------------------------------------------------------------------------------------+
  107. def randomWord(): #---------#
  108.     file = open('words.txt')#
  109.     f = file.readlines()    #-------------#
  110.     i = random.randrange(0, len(f) - 1)   #
  111.     return f[i][:-1]   #------------------#
  112. #----------------------#
  113. #++++++++++++++++++++++++++++++++++++++++++++
  114. #-----------------------------------------#-----------------------------------+------------------------------------------+
  115. def hang(guess): #------------------------#----------+---------+--+-----------+
  116.     global word #-------------------------#------------+-----------------+
  117.     if guess.lower() not in word.lower(): #-------------------------+-----------------------------+----------------+-----------------+
  118.         return True #---------------------#---+--------------------------------+-----+
  119.     else: #-------------------------------#------+------------------------+----------------------------+
  120.         return False #--------------------#-----------+-----------+-----------------------+
  121. #-----------------------------------------#---------------+------------------+
  122. #++++++++++++++++++++++++++++++++++++++++++++
  123. #-------------------------------#
  124. def spacedOut(word, guessed=[]):#
  125.     spacedWord = '' #-----------#
  126.     guessedLetters = guessed #--#
  127.     for x in range(len(word)):  #
  128.         if word[x] != ' ': #----#
  129.             spacedWord += '_ '  #---------------------------#
  130.             for i in range(len(guessedLetters)): #----------#
  131.                 if word[x].upper() == guessedLetters[i]: #--#
  132.                     spacedWord = spacedWord[:-2] #----------#
  133.                     spacedWord += word[x].upper() + ' ' #---#
  134.         elif word[x] == ' ':  #-----------------------------#
  135.             spacedWord += ' ' #-----------------------------#
  136.     return spacedWord #-------------------------------------#
  137. #-----------------------------------------------------------#      
  138. #++++++++++++++++++++++++++++++++++++++++++++
  139. #--------------------------------------------------------------------#
  140. def buttonHit(x, y): #-----------------------------------------------#
  141.     for i in range(len(buttons)): #----------------------------------#
  142.         if x < buttons[i][1] + 20 and x > buttons[i][1] - 20: #------#
  143.             if y < buttons[i][2] + 20 and y > buttons[i][2] - 20: #--#
  144.                 return buttons[i][5] #-------------------------------#
  145.     return None #----------------------------------------------------#
  146. #--------------------------------------------------------------------#
  147. #++++++++++++++++++++++++++++++++++++++++++++
  148. #---------------------------#
  149. def end(winner=False): #----#
  150.     global limbs #-----------#----------------------------#
  151.     lostTxt = 'You Lost, press any key to play again...'  #
  152.     winTxt = 'WINNER!, press any key to play again...'    #
  153.     redraw_game_window() #--------------------------------#
  154.     pygame.time.delay(1000) #-----------------------------#
  155.     win.fill(GREEN)#--------------------------------------#
  156.     if winner == True: #-------------------------------------------#-------------------------------------------------------------------------+
  157.         label = lost_font.render(winTxt, 1, BLACK) #---------------#
  158.     else: #--------------------------------------------------------#--------------------------------------+
  159.         label = lost_font.render(lostTxt, 1, BLACK)   #------------#
  160.     wordTxt = lost_font.render(word.upper(), 1, BLACK)   #---------#    
  161.     wordWas = lost_font.render('The phrase was: ', 1, BLACK)   #---#
  162.     win.blit(wordTxt, (winWidth/2 - wordTxt.get_width()/2, 295))   #----------+-----------------+
  163.     win.blit(wordWas, (winWidth/2 - wordWas.get_width()/2, 245))   #
  164.     win.blit(label, (winWidth / 2 - label.get_width() / 2, 140))   #
  165.     pygame.display.update()  #-------------------------------------#----------------------+--------------------------------+
  166.     again = True #-----------#
  167.     while again: #--------------------------------------------------------+---------------------------------------+--------------------------+----------------+
  168.         for event in pygame.event.get():       #
  169.             if event.type == pygame.QUIT:      #
  170.                 pygame.quit()                  #
  171.             if event.type == pygame.KEYDOWN:   #
  172.                 again = False                  #
  173.     reset()                                    #
  174. #----------------------------------------------#
  175. #++++++++++++++++++++++++++++++++++++++++++++
  176. #---------------------------------#--------------------+-----------------------+---------------+
  177. def reset():  #-------------------#
  178.     global limbs  #---------------#
  179.     global guessed #--------------#
  180.     global buttons #--------------#
  181.     global word  #----------------#
  182.     for i in range(len(buttons)): #
  183.         buttons[i][4] = True #----#--------+-----+
  184.     limbs = 0  #---------#---#
  185.     guessed = []  #------#
  186.     word = randomWord()  #
  187. #-----------#------------#
  188. #MAINLINE   #
  189. #-----------#
  190. #++++++++++++++++++++++++++++++++++++++++++++
  191. #-------------------------------------------------------------#
  192. # Setup buttons ----------------------------------------------#
  193. increase = round(winWidth / 13) #-----------------------------#
  194. for i in range(26): #-----------#-----------------------------#
  195.     if i < 13: #----#
  196.         y = 40 #----#------------#
  197.         x = 25 + (increase * i)  #
  198.     else: #----------------------#------#
  199.         x = 25 + (increase * (i - 13))  #
  200.         y = 85 #------------------------#-------------------#
  201.     buttons.append([LIGHT_BLUE, x, y, 20, True, 65 + i])    #-----------+
  202.     # buttons.append([color, x_pos, y_pos, radius, visible, char])-----------------+
  203. #---------------------------------------------------------------------------------------------------------------------+
  204. #++++++++++++++++++++++++++++++++++++++++++++
  205. #------------------------#
  206. word = randomWord()      #--------------------------+----------------------------+-------------+
  207. inPlay = True #----------#
  208. while inPlay: #-----------------3
  209.     redraw_game_window() #------#
  210.     pygame.time.delay(10) #---------------------------------#------------+-------------------------------------+
  211.     for event in pygame.event.get(): #----------------------#-----+------------------------+
  212.         if event.type == pygame.QUIT: #---------------------#
  213.             inPlay = False #--------------------------------#
  214.         if event.type == pygame.KEYDOWN: #------------------#
  215.             if event.key == pygame.K_ESCAPE: #--------------#----------+------------------------------------------------------+
  216.                 inPlay = False #----------------------------#
  217.         if event.type == pygame.MOUSEBUTTONDOWN: #----------#
  218.             clickPos = pygame.mouse.get_pos() #-------------#
  219.             letter = buttonHit(clickPos[0], clickPos[1])    #
  220.             if letter != None: #-------------------#--------#
  221.                 guessed.append(chr(letter)) #------#
  222.                 buttons[letter - 65][4] = False    #
  223.                 if hang(chr(letter)): #------------#
  224.                     if limbs != 5: #--#
  225.                         limbs += 1 #
  226.                     else:  #-------#
  227.                         end() #
  228.                 else: #-----------------------------#
  229.                     print(spacedOut(word, guessed)) #----------------#----------+---------------------------------------------------------------
  230.                     if spacedOut(word, guessed).count('_') == 0:     #
  231.                         end(True)                                    #
  232. pygame.quit()                                                        #
  233. #--------------------------------------------------------------------#
  234. #===================================================================================================================================================================================================
  235.                                                                                      ##   //  
  236.    #########\   ########    #######\\    ##########   #######\\        /#######\\    ##  //     ######     ##      ##       /#######\\
  237.    ##           ##|--|##    ##      ##   ##           ##      ##      ||-            ## //        ##       ##      ##      ||-
  238.    ##           ##|  |##    ##       ##  #######      ##       ##  -- \\#######\\    ## \\        ##       ##      ##      \\#######\\
  239.    ##           ##|--|##    ##      ##   ##           ##      ##               ||    ##  \\       ##       ##      ##               ||
  240.    #########/   ########    #######//    ##########   #######//       \\######//     ##   \\    ######     #####   #####   \\######//
  241. #--------------------------------------------------------------------------------------------------------------------------------------------------------
  242. #--------------------------------------------------------------------------------------------------------------------------------------------------------
  243. #--------------------------------------------------------------------------------------------------------------------------------------------------------  
  244. #                                                           https://codedskills.net
  245. #--------------------------------------------------------------------------------------------------------------------------------------------------------
  246. #--------------------------------------------------------------------------------------------------------------------------------------------------------
  247. #--------------------------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement