Advertisement
DarkxRift

Untitled

Nov 6th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. import pygame, sys,time , random
  2. from pygame.locals import *
  3.  
  4. black = (0,0,0)
  5. white = (255,255,255)
  6. grey = (127,127,127)
  7.  
  8. display_width = 1000
  9. display_height = 430
  10.  
  11. pygame.init()
  12.  
  13. demo = pygame.mixer.Sound("Hangman/message-Party Rock Anthem.wav")
  14. bgm = pygame.mixer.Sound("Hangman/Sad and Scary Song.wav")
  15.  
  16. #bgm.play(-1)
  17.  
  18. screen = pygame.display.set_mode((display_width,display_height))
  19. pygame.display.set_caption('First Try')
  20.  
  21. clock = pygame.time.Clock()
  22.  
  23. #define loads of codes regarding where the image were located
  24. hangman_p1 = pygame.image.load("Hangman/Hangman_1.png")
  25. hangman_p2 = pygame.image.load("Hangman/Hangman_2.png")
  26. hangman_p3 = pygame.image.load("Hangman/Hangman_3.png")
  27. hangman_p4 = pygame.image.load("Hangman/Hangman_4.png")
  28. hangman_p5 = pygame.image.load("Hangman/Hangman_5.png")
  29. hangman_p6 = pygame.image.load("Hangman/Hangman_6.png")
  30. hangman_p7 = pygame.image.load("Hangman/Hangman_7.png")
  31. hangman_p8 = pygame.image.load("Hangman/Hangman_8.png")
  32. hangman_p9 = pygame.image.load("Hangman/Hangman_9.png")
  33. hangman_p10 = pygame.image.load("Hangman/Hangman_10.png")
  34.  
  35. smile_1 = pygame.image.load("Hangman/creepy_smile_1.jpg")
  36. smile_2 = pygame.image.load("Hangman/creepy_smile_2.jpg")
  37. smile_3 = pygame.image.load("Hangman/creepy_smile_3.jpg")
  38. smile_4 = pygame.image.load("Hangman/creepy_smile_4.jpg")
  39. smile_5 = pygame.image.load("Hangman/creepy_smile_5.jpg")
  40.  
  41. Fruit = "lemon","pineapple","kiwi","banana","watermelon"
  42. Mobile = "motorola","apple","nokia","samsung","oppo"
  43. Furniture = "table","sofa","wardrobe","chair","bed"
  44.  
  45. AllWords = Fruit + Mobile +Furniture
  46.  
  47. charlist = "a b c d e f g h i j k l m n o p q r s v w x y z".split()
  48.  
  49. def getRandomword(wordlist):
  50. wordindex = random.randint(0,len(wordlist)-1)
  51. return wordlist[wordindex]
  52.  
  53. def getFruit():
  54.  
  55. screen.fill(black)
  56.  
  57. hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
  58. hintText, hintText_Size = text_objects('Hint: It is a kind of fruit', hintSize)
  59. hintText_Size.center = ((display_width/2)+80,(display_height/2)-80)
  60. screen.blit(hintText, hintText_Size)
  61.  
  62. pygame.display.flip()
  63.  
  64. return Fruit, gameList == False, gameStart == True
  65.  
  66. def getMobile():
  67.  
  68. screen.fill(black)
  69.  
  70. hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
  71. hintText, hintText_Size = text_objects('Hint: Its a brand of mobile phone', hintSize)
  72. hintText_Size.center = ((display_width/2)+80,(display_height/2)-80)
  73. screen.blit(hintText, hintText_Size)
  74.  
  75. pygame.display.flip()
  76.  
  77. return Mobile, gameList == False, gameStart == True
  78.  
  79. def getFurniture():
  80. hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
  81. hintText, hintText_Size = text_objects('Hint: Its a Furniture', hintSize)
  82. hintText_Size.center = ((display_width/2)+80,(display_height/2)-80)
  83. screen.blit(hintText, hintText_Size)
  84.  
  85. pygame.display.flip()
  86.  
  87. return Furniture, gameList == False, gameStart == True
  88.  
  89. def getRandom():
  90.  
  91. screen.fill(black)
  92.  
  93. hintSize = pygame.font.Font("Hangman/CHILLER.TTF", 50)
  94. hintText, hintText_Size = text_objects('No hint were given', hintSize)
  95. hintText_Size.center = ((display_width/2)+80,(display_height/2)-80)
  96. screen.blit(hintText, hintText_Size)
  97.  
  98. pygame.display.flip()
  99.  
  100. return random.choice(AllWords), gameList == False, gameStart == True
  101.  
  102. def creep_smile():
  103. screen.blit(smile_1,(10,0))
  104. screen.blit(smile_2,(650,0))
  105. screen.blit(smile_3,(500,300))
  106. screen.blit(smile_4,(300,-5))
  107. screen.blit(smile_5,(50,200))
  108.  
  109. def wordlist():
  110.  
  111. for event in pygame.event.get():
  112. if event.type == pygame.KEYDOWN:
  113. choose = pygame.key.name(event.key)
  114.  
  115.  
  116. choice = {
  117. "1": getFruit,
  118. "2": getMobile,
  119. "3": getFurniture,
  120. }
  121.  
  122.  
  123. return choice.get(choose,getRandom)()
  124.  
  125. screen.fill(black)
  126.  
  127. msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 40)
  128. msgText, msgText_Size = text_objects('Select one of the category', msgSize)
  129. msgText_Size.center = ((display_width/2)+50,(display_height/2)-70)
  130. screen.blit(msgText, msgText_Size)
  131.  
  132. msgSize_a = pygame.font.Font("Hangman/timesbd.ttf", 22)
  133. msgText_1, msgText_1_Size = text_objects('1. Fruits', msgSize_a)
  134. msgText_1_Size.center = ((display_width/2)-50,(display_height/2)-40)
  135. screen.blit(msgText_1, msgText_1_Size)
  136.  
  137. msgText_2, msgText_2_Size = text_objects('2. Moblie phones Brand', msgSize_a)
  138. msgText_2_Size.center = ((display_width/2)+22,(display_height/2)-15)
  139. screen.blit(msgText_2, msgText_2_Size)
  140.  
  141. msgText_3, msgText_3_Size = text_objects('3. Furniture', msgSize_a)
  142. msgText_3_Size.center = ((display_width/2)-33,(display_height/2)+8)
  143. screen.blit(msgText_3, msgText_3_Size)
  144.  
  145. msgText_4, msgText_4_Size = text_objects('4. Random', msgSize_a)
  146. msgText_4_Size.center = ((display_width/2)-39,(display_height/2)+30)
  147. screen.blit(msgText_4, msgText_4_Size)
  148.  
  149. pygame.display.flip()
  150.  
  151.  
  152. def display(incorrect_letter_count,correct_letters,secret):
  153.  
  154. penalty()
  155.  
  156. blanks = '_ ' * len(secret)
  157.  
  158. for i in range(len(secret)):
  159. if secret[i] in correct_letters:
  160. blanks = blanks[:i] + secret[i] + blanks[i+1:]
  161.  
  162. for letter in blanks:
  163. textSize = pygame.font.Font("freesansbold.ttf", 30)
  164. secretText1, secretText1_Size = text_objects(blanks, textSize)
  165. secretText1_Size.center = ((display_width/2)+200,(display_height/2)+60)
  166. screen.blit(secretText1, secretText1_Size)
  167.  
  168. pygame.display.flip()
  169.  
  170. def getguess(alreadyguessed):
  171.  
  172. while True:
  173. for event in pygame.event.get():
  174. if event.type == pygame.KEYDOWN:
  175. guess = pygame.key.name(event.key)
  176. if len(guess) != 1:
  177. msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 20)
  178. msgText, msgText_Size = text_objects('Enter single letter please', msgSize)
  179. msgText_Size.center = ((display_width/2)+150,(display_height/2)-40)
  180. screen.blit(msgText, msgText_Size)
  181.  
  182. pygame.display.flip()
  183.  
  184. elif guess in alreadyguessed:
  185. msgSize = pygame.font.Font("Hangman/CHILLER.TTF", 20)
  186. msgText, msgText_Size = text_objects('This letter already guessed', msgSize)
  187. msgText_Size.center = ((display_width/2)+150,(display_height/2)-40)
  188. screen.blit(msgText, msgText_Size)
  189.  
  190. pygame.display.fip()
  191.  
  192. else:
  193. return guess
  194.  
  195. def intro():
  196.  
  197. textSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 120)
  198. introText_1, introText_Size_1 = text_objects('Are You Ready', textSize)
  199. introText_Size_1.center = ((display_width/2),(display_height/2)-80)
  200. screen.blit(introText_1, introText_Size_1)
  201.  
  202. introText_2, introText_Size_2 = text_objects('For the Game?', textSize)
  203. introText_Size_2.center = ((display_width/2),(display_height/2)+50)
  204. screen.blit(introText_2, introText_Size_2)
  205.  
  206. pygame.display.flip()
  207.  
  208. time.sleep(2)
  209.  
  210. wordlist()
  211.  
  212. return gameList == True
  213.  
  214. def buttonYes(msg,x,y,w,h,ic,ac):
  215.  
  216. mouse = pygame.mouse.get_pos()
  217. click = pygame.mouse.get_pressed()
  218. pygame.draw.rect(screen, white,(x-2,y-2,w+5,h+5))
  219.  
  220. if x+w > mouse[0] > x and y+h > mouse[1] > y and click [0] == 1:
  221. pygame.draw.rect(screen, grey,(x,y,w,h))
  222. game()
  223. else:
  224. pygame.draw.rect(screen, black,(x,y,w,h))
  225.  
  226. smallText = pygame.font.Font("freesansbold.ttf", 20)
  227. textSurf,textRect = text_objects(msg, smallText)
  228. textRect.center = ((x+(w/2)),(y+(h/2)))
  229.  
  230. screen.blit(textSurf, textRect)
  231.  
  232. smallText = pygame.font.Font("freesansbold.ttf", 20)
  233. textSurf,textRect = text_objects(msg, smallText)
  234. textRect.center = ((x+(w/2)),(y+(h/2)))
  235.  
  236. screen.blit(textSurf, textRect)
  237.  
  238. def buttonNo(msg,x,y,w,h,ic,ac):
  239.  
  240. mouse = pygame.mouse.get_pos()
  241. click = pygame.mouse.get_pressed()
  242. pygame.draw.rect(screen, white,(x-2,y-2,w+5,h+5))
  243.  
  244. if x+w > mouse[0] > x and y+h > mouse[1] > y and click [0] == 1:
  245. pygame.draw.rect(screen, grey,(x,y,w,h))
  246. for event in pygame.event.get():
  247. if event.type == pygame.QUIT:
  248. pygame.quit()
  249. quit()
  250. else:
  251. pygame.draw.rect(screen, black,(x,y,w,h))
  252.  
  253. smallText = pygame.font.Font("freesansbold.ttf", 20)
  254. textSurf,textRect = text_objects(msg, smallText)
  255. textRect.center = ((x+(w/2)),(y+(h/2)))
  256.  
  257. screen.blit(textSurf, textRect)
  258.  
  259. smallText = pygame.font.Font("freesansbold.ttf", 20)
  260. textSurf,textRect = text_objects(msg, smallText)
  261. textRect.center = ((x+(w/2)),(y+(h/2)))
  262.  
  263. screen.blit(textSurf, textRect)
  264.  
  265.  
  266.  
  267. def text_objects(text, font):
  268. textSurface = font.render(text, True, white)
  269. return textSurface, textSurface.get_rect()
  270.  
  271.  
  272. def penalty():
  273.  
  274. incorrect_letter_count = len(incorrect_letters)
  275.  
  276. if incorrect_letter_count > 0:
  277. screen.blit(hangman_p4, (-40,-28))
  278. if incorrect_letter_count > 1:
  279. screen.blit(hangman_p5, (-58,13))
  280. if incorrect_letter_count > 2:
  281. screen.blit(hangman_p6, (-46,22))
  282. if incorrect_letter_count > 3:
  283. screen.blit(hangman_p7, (-38,5))
  284. if incorrect_letter_count > 4:
  285. screen.blit(hangman_p8, (-55,18))
  286. if incorrect_letter_count > 5:
  287. screen.blit(hangman_p9, (-46,47))
  288. if incorrect_letter_count > 5:
  289. screen.blit(hangman_p10, (-37,16))
  290.  
  291. pygame.display.flip()
  292.  
  293. def game():
  294.  
  295. gameQuit = False
  296.  
  297. screen.blit(hangman_p1, (-72,0))
  298. screen.blit(hangman_p2, (20,75))
  299. screen.blit(hangman_p3, (-137,-15))
  300.  
  301. global correct_letters, incorrect_letters, incorrect_letter_count
  302. incorrect_letters = correct_letters = incorrect_letter_count = ''
  303.  
  304. display(incorrect_letter_count,correct_letters,secret)
  305. guess = getguess(incorrect_letters + correct_letters)
  306.  
  307. click = pygame.mouse.get_pressed()
  308.  
  309. pygame.display.flip()
  310.  
  311. while not gameQuit:
  312. for event in pygame.event.get():
  313. if event.type == pygame.QUIT:
  314. pygame.quit()
  315. quit()
  316.  
  317. if event.type == pygame.KEYDOWN:
  318.  
  319. if guess in secret:
  320. correct_letters = correct_letters + guess
  321. found = True
  322.  
  323. for i in range(len(secret)):
  324. if secret[i] not in correct_letters:
  325. found = False
  326. break
  327.  
  328. if found == True:
  329.  
  330. screen.fill(black)
  331.  
  332. msgSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 80)
  333. msgText, msgText_Size = text_objects('Congratz!! You won!', msgSize)
  334. msgText_Size.center = ((display_width/2),(display_height/2)-70)
  335. screen.blit(msgText, msgText_Size)
  336.  
  337. msgSize1 = pygame.font.Font("Hangman/ITCBLKAD.TTF", 50)
  338. msgText1, msgText1_Size = text_objects('Play again?', msgSize1)
  339. msgText1_Size.center = ((display_width/2),(display_height/2)+30)
  340. screen.blit(msgText1, msgText1_Size)
  341.  
  342. buttonYes("Yes",350,295,80,40,black,grey)
  343. buttonNo("No",550,295,80,40,black,grey)
  344.  
  345. pygame.display.flip()
  346.  
  347. else:
  348. incorrect_letters = incorrect_letters + guess
  349.  
  350. if len(incorrect_letters) == 7:
  351.  
  352. screen.fill(black)
  353.  
  354. display(incorrect_letters,correct_letters,secret)
  355. msgSize = pygame.font.Font("Hangman/ITCBLKAD.TTF", 80)
  356. msgText, msgText_Size = text_objects('You Lose! the word was: ' + secret, msgSize)
  357. msgText_Size.center = ((display_width/2),(display_height/2)-70)
  358. screen.blit(msgText, msgText_Size)
  359.  
  360. msgSize1 = pygame.font.Font("Hangman/ITCBLKAD.TTF", 50)
  361. msgText1, msgText1_Size = text_objects('Play again?', msgSize1)
  362. msgText1_Size.center = ((display_width/2),(display_height/2)+30)
  363. screen.blit(msgText1, msgText1_Size)
  364.  
  365. buttonYes("Yes",350,295,80,40,black,grey)
  366. buttonNo("No",550,295,80,40,black,grey)
  367.  
  368. pygame.display.flip()
  369.  
  370. secret = getRandomword(AllWords)
  371.  
  372. gameList = False
  373. gameStart = False
  374.  
  375. intro()
  376.  
  377. while not gameList:
  378.  
  379. gameList = wordlist()
  380.  
  381. while not gameStart:
  382.  
  383. gameStart = game()
  384.  
  385.  
  386.  
  387.  
  388. clock.tick(42)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement