Hangman Game using Python
Data science projects in pondicherry
Create New

Hangman Game using Python

Project period

09/03/2020 - 09/30/2020

Views

70

0

Project Category

Computer Science



Hangman Game using Python
Hangman Game using Python

This project is based upon games. Nowadays most of the games are mentally disturbing youngsters and they waste more time by playing many useless games linke playing shooting games, etc. Instead of these games we can play some useful games like hangman, puzzle games, solving puzzles, finding words etc. By playing useless games on mobiles, tabs or whatever it is, most of the youngsters (young people) are addicted to that. To save time or learn something they can make use of time to learn by playing useful games. To make it useful for them, I introduced this hangman game by adding music.

Why: Problem statement

The problem in the world is most of the youngsters are addicted by playing useless games like Shooting games etc. The reason why I chose this game is because my brother used to play some useless games like traffic games, Subway Surf etc. By playing continuously he is addicted to that. So the reason is I thought to introduce a useful game which will help him. Most of the time he used to spend in you tube and also addicted by using mobiles and tabs. So I introduced this hangman game to play and develop his knowledge which will help him to learn more and also to make it more interesting for him to play and to make it more useful for him. I thought that the hangman game would be interesting, which means like word games. That is why I introduced this game to him.

How: Solution description

Hangman is a classic guessing game for two or more players. One player thinks of a word and the other(s) tries to guess it by suggesting letters, within a certain number of guesses. How can we take that idea and write a functional computer program around it? First, we’ll need to create the word that will act as our “answer”. Then, we’ll need to ask the user to guess a letter. Then, we will need to check to see if that letter is included in the “answer” word. These last two steps will need to be repeated (in a loop) until all of the letters are successfully guessed. In Python, the following is code for a functioning Hangman game. Make sure to read the comments to understand what is happening in each part of the code. 

Type the code, save the file with a .py extension, and run the program in the Terminal. If you receive any error messages, determine what the problems are likely to be and debug your code. 

  • Once you can run the program with no error messages, try it out. Does it work? How do you know if it is working correctly? 

  • Write code to make this program react to INCORRECT letters by printing “BAD GUESS!”. 

  • In Hangman, the user only gets 6 incorrect guesses before they lose and the game is over. Write code to implement this.

  • Rather than hard-coding the answer as “hangman”, randomly select a word from a LIST of words to use as the answer. 

  • Rather than hard-coding the answer as “hangman”, randomly select a word from an EXTERNAL FILE to use as the answer. 

  • Use a Python library to access a dictionary on the internet, and use a randomly selected word from that as the answer.

  • Create a visualization for the game using the Turtle library. 

How is it different from competition

The main difference for this project, the others created normal games that did not improve the skill and other thing was the existing game only created for stress relieving. So that our proposed project helps both the things like stress relieving and improving the skills. The Hangman game is very useful to play. In this game, the advantage is this game will be very interesting to play and also I added music. While playing this game with music it will create an interest within us and we will play interestingly. While playing this hangman game, it will help us to know about things and words we don't know. Playing word games with music is such a interesting thing. It also improves knowledge.

Who are your customers

This Hangman game is mainly proposed for all the youngsters, children and who loves to play games. This game is very interesting and easy to play by children also. It will help to increase the english skill of the users. Most of the youngsters used to play some useless games like shooting games, so that most of the people are addicted to that. To overcome this I will suggest useful games like word playing games to make their brain more powerful and to create an interest within them. Instead of playing useless games they can play like this word games, puzzle games, Hangman etc.,

Project Phases and Schedule

Phase 1: Study various games in Python.

Phase 2: Write code by using the Python syntax such as If statements, Loops, and Lists which supports this game.

Phase 3: Testing and Debugging.

Resources Required

Anaconda tool: Anaconda is a free and open-source distribution of the python 

and R programming languages for scientific computing (Data science, Machine learning applications, Large-scale data processing, Predictive analysis, etc.) that aims to simplify package management and deployment. 

Languages used: Python 3.7

Python 3.7: Python 3.7 is officially released. This new python version  has been in development since September 2016, And now we all  get to enjoy the results of the core developer’s hard work. While the documentation gives a good overview of the new features, This article will take a deep dive into some of the biggest pieces of news. 

Text editor:  A Jupyter notebook is similar to the IPython console, but, instead of only being able to work with a single line of code at a time, you can easily edit and re-execute any code that has been written in a notebook. Furthermore, you can save a notebook, and thus return to it later. Additionally, a notebook provides many terrific features. For instance, you can embed visualizations of data within a notebook, and write blocks of nicely-formatted text (using the Markdown syntax), for presenting and explaining the contents of the notebook.

Download:
Project Code Code copy
/* Your file Name : Hangman Music.ipynb */
/* Your coding Language : python */
/* Your code snippet start here */
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playsound import playsound\n",
    "\n",
    "\n",
    "#Created By Abhinav Khare\n",
    "#References : https://docs.python.org/3.4/library/turtle.html\n",
    "\n",
    "#--------------------------import modules--------------------------------------------------------------------#\n",
    "import random\n",
    "import turtle \n",
    "import sys\n",
    "import time\n",
    "\n",
    "\n",
    "#----------------------------global variables-------------------------------------#\n",
    "    \n",
    "\n",
    "flagW=False\n",
    "flagL=False\n",
    "flafA=False\n",
    "flagWr=False\n",
    "gameFlag=True\n",
    "wrongCount=0\n",
    "hmTC=(0,0)\n",
    "hm=turtle.Turtle()\n",
    "hm.speed(0)\n",
    "state=turtle.Turtle()\n",
    "state.ht()\n",
    "playsound('myfiles.mp3')\n",
    "\n",
    "\n",
    "#---------------------------turtle library related functions---------------------------------------------------#\n",
    "def correct():\n",
    "        state.clear()\n",
    "        state.color(\"green\")\n",
    "        state.write(\"Correct!!\",font=(\"Arial\",50,\"bold\"))\n",
    "def lose():\n",
    "        state.clear()\n",
    "        state.color(\"white\")\n",
    "        state.write(\"YOU LOSE!!\", font=(\"Arial\",50,\"bold\"))\n",
    "        cordxy=letter[0].pos()\n",
    "        temp=turtle.Turtle()\n",
    "        temp.ht()\n",
    "        temp.speed(0)\n",
    "        temp.color(\"white\")\n",
    "        temp.penup()\n",
    "        temp.setpos(cordxy[0],cordxy[1]+40)\n",
    "        temp.pendown()\n",
    "        temp.write(\"This was the movie:\",align=\"left\", font=(\"Arial\", 30, \"bold\"))\n",
    "        \n",
    "        for i in range(len(movie)):\n",
    "                if(movie[i] not in user_list_correct):\n",
    "                        letter[i].color(\"red\")\n",
    "                        letter[i].write(movie[i],align=\"left\", font=(\"Arial\", 20, \"bold\"))\n",
    "        \n",
    "def win():\n",
    "\tstate.clear()\n",
    "\tstate.color(\"white\")\n",
    "\tstate.write(\"YOU WIN!!\", font=(\"Arial\",50,\"bold\"))\n",
    "def already():\n",
    "\t\n",
    "\t\t\n",
    "\tstate.clear()\n",
    "\tstate.color(\"green\")\n",
    "\tstate.write(\"YOU Already pressed that key!!\", font=(\"Arial\",50,\"bold\"))\n",
    "\t\n",
    "def wrong():\n",
    "\tstate.clear()\n",
    "\tstate.color(\"red\")\n",
    "\tstate.write(\"wrong\", font=(\"Arial\",50,\"bold\"))\n",
    "\t\n",
    "def A():\n",
    "\t\t\tevaluate(movie,\"A\")\n",
    "def B():\n",
    "\t\t\tevaluate(movie,\"B\")\n",
    "def C():\n",
    "\t\t\tevaluate(movie,\"C\")\n",
    "def D():\n",
    "\t\t\tevaluate(movie,\"D\")\n",
    "def E():\n",
    "\t\t\tevaluate(movie,\"E\")\n",
    "def F():\n",
    "\t\t\tevaluate(movie,\"F\")\n",
    "def G():\n",
    "\t\t\tevaluate(movie,\"G\")\n",
    "def H():\n",
    "\t\t\tevaluate(movie,\"H\")\n",
    "def I():\n",
    "\t\t\tevaluate(movie,\"I\")\n",
    "def J():\n",
    "\t\t\tevaluate(movie,\"J\")\n",
    "def K():\n",
    "\t\t\tevaluate(movie,\"K\")\n",
    "def L():\n",
    "\t\t\tevaluate(movie,\"L\")\n",
    "def M():\n",
    "\t\t\tevaluate(movie,\"M\")\n",
    "def N():\n",
    "\t\t\tevaluate(movie,\"N\")\n",
    "def O():\n",
    "\t\t\tevaluate(movie,\"O\")\n",
    "def P():\n",
    "\t\t\tevaluate(movie,\"P\")\n",
    "def Q():\n",
    "\t\t\tevaluate(movie,\"Q\")\n",
    "def R():\n",
    "\t\t\tevaluate(movie,\"R\")\n",
    "def S():\n",
    "\t\t\tevaluate(movie,\"S\")\n",
    "def T():\n",
    "\t\t\tevaluate(movie,\"T\")\n",
    "def U():\n",
    "\t\t\tevaluate(movie,\"U\")\n",
    "def V():\n",
    "\t\t\tevaluate(movie,\"V\")\n",
    "def W():\n",
    "\t\t\tevaluate(movie,\"W\")\n",
    "def X():\n",
    "\t\t\tevaluate(movie,\"X\")\n",
    "def Y():\n",
    "\t\t\tevaluate(movie,\"Y\")\n",
    "def Z():\n",
    "\t\t\tevaluate(movie,\"Z\")\n",
    "\n",
    "#function which controls the drawing of hangman figure.\n",
    "def Hangman(num):\n",
    "    global hmTC\n",
    "    if(num==0):\n",
    "            \n",
    "            hm.penup()\n",
    "            hm.setpos(-670,-340)\n",
    "            hm.pendown()\n",
    "            hm.ht()\n",
    "            hm.begin_fill()\n",
    "            hm.fd(400)\n",
    "            hm.lt(90)\n",
    "            hm.fd(50)\n",
    "            hm.lt(90)\n",
    "            hm.fd(390)\n",
    "    \n",
    "            hm.rt(90)\n",
    "            hm.fd(600)\n",
    "            hm.rt(90)\n",
    "            hm.fd(400)\n",
    "            hm.rt(90)\n",
    "            hm.fd(20)\n",
    "            hm.lt(90)\n",
    "            hm.fd(10)\n",
    "            \n",
    "            \n",
    "            # print(hmTC[0])\n",
    "            hm.lt(90)\n",
    "            hmTC=hm.pos()\n",
    "            hm.fd(30)\n",
    "            \n",
    "            hm.lt(90)\n",
    "            hm.fd(420)\n",
    "            hm.lt(90)\n",
    "            hm.fd(550)\n",
    "            hm.end_fill()\n",
    "            #hm.end_fill()\n",
    "            hm.pensize(5)\n",
    "    elif(num==1):\n",
    "            hm.penup()\n",
    "            hm.setpos(hmTC[0],hmTC[1])\n",
    "            hm.rt(90)\n",
    "            hm.pendown()\n",
    "            hm.circle(40)\n",
    "            #print(hm.pos())\n",
    "    elif(num==2):\n",
    "            hm.penup()\n",
    "            hm.setpos(hmTC[0],hmTC[1]-80)\n",
    "            hm.lt(90)\n",
    "            hm.pendown()\n",
    "            hm.fd(140)\n",
    "            hmTC=hm.pos()\n",
    "    elif(num==3):\n",
    "            hm.penup()\n",
    "            hm.rt(30)\n",
    "            hm.pendown()\n",
    "            hm.fd(100)\n",
    "            hm.rt(60)\n",
    "            hm.fd(10)\n",
    "            hm.penup()\n",
    "            hm.rt(180)\n",
    "            hm.fd(10)\n",
    "            hm.rt(30)\n",
    "            hm.fd(100)\n",
    "            hm.rt(90)\n",
    "            hm.pendown()\n",
    "    elif(num==4):\n",
    "            hm.penup()\n",
    "            hm.setpos(hmTC[0],hmTC[1])\n",
    "            \n",
    "            hm.pendown()\n",
    "            hm.lt(60)\n",
    "            hm.fd(100)\n",
    "            hm.lt(60)\n",
    "            hm.fd(10)\n",
    "            #hm.rt(90)\n",
    "            \n",
    "            \n",
    "           \n",
    "    elif(num==5):\n",
    "            hm.penup()\n",
    "            hm.setpos(hmTC[0],hmTC[1])\n",
    "            hm.lt(90)\n",
    "            hm.fd(90)\n",
    "            hm.lt(180)\n",
    "            hm.rt(30)\n",
    "            hm.pendown()\n",
    "            hm.fd(80)\n",
    "            hm.rt(60)\n",
    "            \n",
    "            \n",
    "            \n",
    "    elif(num==6):\n",
    "            hm.penup()\n",
    "            hm.setpos(hmTC[0],hmTC[1])\n",
    "            hm.rt(90)\n",
    "            hm.fd(90)\n",
    "            hm.lt(180)\n",
    "            hm.lt(30)\n",
    "            hm.pendown()\n",
    "            hm.fd(80)\n",
    "            \n",
    "    elif(num==7):\n",
    "            hm.penup()\n",
    "            hm.setpos(-235.00,265.00)\n",
    "            hm.rt(90)\n",
    "            hm.pendown()\n",
    "            hm.circle(4)\n",
    "    elif(num==8):\n",
    "            hm.penup()\n",
    "            hm.setpos(-265,265.00)\n",
    "            hm.rt(90)\n",
    "            hm.pendown()\n",
    "            hm.circle(4)\n",
    "    \n",
    "    else:\n",
    "        hm.penup()\n",
    "        hm.setpos(hm.pos()[0]+25,hm.pos()[1]-35)\n",
    "        hm.pendown()\n",
    "        hm.rt(30)\n",
    "        hm.circle(10,180)\n",
    "#creates boxes to type characters in    \n",
    "def createDash(num):\n",
    "    dash=[]\n",
    "    letter=[]\n",
    "    for i in range(num):\n",
    "        #print(movie_space_position)\n",
    "        dash.append(turtle.Turtle())\n",
    "        dash[i].color(\"#800000\",\"white\")\n",
    "        dash[i].pensize(4)\n",
    "        letter.append(turtle.Turtle())      \n",
    "        dash[i].ht()\n",
    "        dash[i].speed(0)\n",
    "        letter[i].ht()      \n",
    "        dash[i].penup()\n",
    "        letter[i].penup()       \n",
    "        dash[i].setpos(-600+i*50,-200)\n",
    "        letter[i].setpos(-595+i*50,-200)        \n",
    "        dash[i].pendown()\n",
    "        letter[i].pendown()\n",
    "        if(i in movie_space_position):\n",
    "                    dash[i].penup()\n",
    "                    continue\n",
    "        dash[i].begin_fill()\n",
    "        dash[i].fd(30)\n",
    "        dash[i].lt(90)\n",
    "        dash[i].fd(30)\n",
    "        dash[i].lt(90)\n",
    "        dash[i].fd(30)\n",
    "        dash[i].lt(90)\n",
    "        dash[i].fd(30)\n",
    "        dash[i].end_fill()\n",
    "    return letter\n",
    "\t\t\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#----------------------------core working of the game------------------------------#\n",
    "def evaluate(movie,user_input):\n",
    "    global wrongCount\n",
    "    global gameFlag\n",
    "    if(gameFlag==True):\n",
    "        if(user_input in movie and user_input not in user_list_correct):\n",
    "            tempPositionList=[]\n",
    "            for i in range(len(movie)):\n",
    "                if(movie[i]==user_input):\n",
    "                    letter[i].write(user_input,align=\"left\", font=(\"Arial\", 20, \"bold\"))\n",
    "                    user_list_correct.append(user_input)\n",
    "                    tempPositionList.append(i)\n",
    "                    correct()                    \n",
    "                    if(len(user_list_correct)==len(movie)):\n",
    "                        win()\n",
    "                        gameFlag=False\n",
    "            \n",
    "                    \n",
    "                    \n",
    "                \n",
    "                \n",
    "            #return tempPositionList\n",
    "        \n",
    "        else:\n",
    "            if(user_input in user_list_correct):\n",
    "                already()\n",
    "                \n",
    "            else:\n",
    "                wrong()\n",
    "                user_list_wrong.append(user_input)\n",
    "                wrongCount+=1\n",
    "                if(wrongCount>=9):\n",
    "                    lose()\n",
    "                    gameFlag=False\n",
    "                \n",
    "                \n",
    "                Hangman(wrongCount)\n",
    "    \n",
    "            \n",
    "    \n",
    "f = open(\"movies.txt\")\n",
    "movie_list=[]\n",
    "for line in f:\n",
    "    line = line.strip()\n",
    "    movie_list.append(line)\n",
    "\n",
    "movie = random.choice(movie_list)\n",
    "movie=movie.upper()\n",
    "l = len(movie)\n",
    "movie = list(movie)\n",
    "movie_space_count=0\n",
    "movie_space_position=[]\n",
    "user_list_correct  = []\n",
    "user_list_wrong = []\n",
    "user_input=[]\n",
    "for c in range(len(movie)):\n",
    "    if(movie[c]==' '):\n",
    "        movie_space_count+=1\n",
    "        movie_space_position.append(c)\n",
    "        user_list_correct.append(' ')\n",
    "\n",
    "    \n",
    "\n",
    "wn = turtle.Screen()\n",
    "wn.setup (width=1920, height=1080, startx=0, starty=0)\n",
    "wn.bgpic(\"imain1.gif\")\n",
    "wn.tracer(2)\n",
    "Hangman(0)\n",
    "letter = createDash(len(movie))\n",
    "wn.tracer(1)\n",
    "wn.onkey(A,\"a\")\n",
    "wn.onkey(B,\"b\")\n",
    "wn.onkey(A,\"a\")\n",
    "wn.onkey(A,\"A\")\n",
    "wn.onkey(B,\"b\")\n",
    "wn.onkey(B,\"B\")\n",
    "wn.onkey(C,\"c\")\n",
    "wn.onkey(C,\"C\")\n",
    "wn.onkey(D,\"d\")\n",
    "wn.onkey(D,\"D\")\n",
    "wn.onkey(E,\"e\")\n",
    "wn.onkey(E,\"E\")\n",
    "wn.onkey(F,\"f\")\n",
    "wn.onkey(F,\"F\")\n",
    "wn.onkey(G,\"g\")\n",
    "wn.onkey(G,\"G\")\n",
    "wn.onkey(H,\"h\")\n",
    "wn.onkey(H,\"H\")\n",
    "wn.onkey(I,\"i\")\n",
    "wn.onkey(I,\"I\")\n",
    "wn.onkey(J,\"j\")\n",
    "wn.onkey(J,\"J\")\n",
    "wn.onkey(K,\"k\")\n",
    "wn.onkey(K,\"K\")\n",
    "wn.onkey(L,\"l\")\n",
    "wn.onkey(L,\"L\")\n",
    "wn.onkey(M,\"m\")\n",
    "wn.onkey(M,\"M\")\n",
    "wn.onkey(N,\"n\")\n",
    "wn.onkey(N,\"N\")\n",
    "wn.onkey(O,\"o\")\n",
    "wn.onkey(O,\"O\")\n",
    "wn.onkey(P,\"p\")\n",
    "wn.onkey(P,\"P\")\n",
    "wn.onkey(Q,\"q\")\n",
    "wn.onkey(Q,\"Q\")\n",
    "wn.onkey(R,\"r\")\n",
    "wn.onkey(R,\"R\")\n",
    "wn.onkey(S,\"s\")\n",
    "wn.onkey(S,\"S\")\n",
    "wn.onkey(T,\"t\")\n",
    "wn.onkey(T,\"T\")\n",
    "wn.onkey(U,\"u\")\n",
    "wn.onkey(U,\"U\")\n",
    "wn.onkey(V,\"v\")\n",
    "wn.onkey(V,\"V\")\n",
    "wn.onkey(W,\"w\")\n",
    "wn.onkey(W,\"W\")\n",
    "wn.onkey(X,\"x\")\n",
    "wn.onkey(X,\"X\")\n",
    "wn.onkey(Y,\"y\")\n",
    "wn.onkey(Y,\"Y\")\n",
    "wn.onkey(Z,\"z\")\n",
    "wn.onkey(Z,\"Z\")\n",
    "if(gameFlag==True):\n",
    "\twn.listen()\n",
    "\n",
    "turtle.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
View on Github
Hangman Project

Comments

Leave a Comment

Post a Comment

Are you Interested in this project?


Do you need help with a similar project? We can guide you. Please Click the Contact Us button.


Contact Us

Social Sharing