{"id":36026,"date":"2021-06-15T09:54:32","date_gmt":"2021-06-15T07:54:32","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=36026"},"modified":"2021-06-15T10:21:12","modified_gmt":"2021-06-15T08:21:12","slug":"how-to-make-a-game-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/como-hacer-un-juego-en-python\/","title":{"rendered":"My first game in Python"},"content":{"rendered":"<p>This week we are going to do our <strong>first game in python<\/strong>. It is a very simple game where the participant must guess a random number chosen automatically in less than 10 attempts with the help of the clues that the system will provide when entering a number.<\/p>\n\n\n\n<p>We are going to <strong>practice loops<\/strong>,<strong> the conditionals<\/strong> and we will learn to<strong> import modules and generate random numbers.<\/strong><\/p>\n\n\n\n<p>If you want to develop your career in<strong>&nbsp;Python<\/strong>, we recommend you visit our<a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\">&nbsp;master in Advanced Programming in Python for Big Data, Hacking and Machine Learning<\/a> and train with the best experts.<\/p>\n\n\n\n<p>As we saw weeks ago, Python includes <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/phyton-predefined-functions\/\">predefined functions<\/a> so we can use them whenever we want, but we can also<strong> import modules<\/strong> that include specific functions for each module and that will make our lives easier when programming.&nbsp;<\/p>\n\n\n\n<p>The only thing we have to do is import the module we want with the declaration<strong> \u201c<em>import<\/em>\u201d<\/strong> followed by the name of the module in question.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">We start by looking at all the game code<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/juego-en-python-1024x941.png\" alt=\"how to make my first game in python\" class=\"wp-image-36027\" width=\"674\" height=\"618\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/juego-en-python-1024x941.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/juego-en-python-300x276.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/juego-en-python-180x165.png 180w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/juego-en-python.png 1308w\" sizes=\"(max-width: 674px) 100vw, 674px\" \/><\/figure><\/div>\n\n\n\n<p>We have started by importing the<strong> module<\/strong> <em>random<\/em> which includes functions related to random numbers:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/import-mi-primero-juego-en-python.png\" alt=\"\" class=\"wp-image-36028\" width=\"287\" height=\"126\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/import-mi-primero-juego-en-python.png 360w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/import-mi-primero-juego-en-python-300x132.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n\n<p>We create a <strong>counter<\/strong> by name &quot;<em>Attempts<\/em>\u201d to show the player how many guesses he has used to guess the number which we will increment each time he enters the while loop.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-mi-primer-juego-en-python.png\" alt=\"\" class=\"wp-image-36029\" width=\"246\" height=\"106\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-mi-primer-juego-en-python.png 330w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-mi-primer-juego-en-python-300x129.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-mi-primer-juego-en-python-326x142.png 326w\" sizes=\"(max-width: 246px) 100vw, 246px\" \/><\/figure><\/div>\n\n\n\n<p>With the function <strong><em>randint<\/em><\/strong>, we generate a random integer number between the two values that we indicate below between the parentheses. In that line, we indicate that the variable <strong><em>x<\/em><\/strong> It will be a random integer between 1 and 50.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/random-mi-primer-juego-en-python.png\" alt=\"\" class=\"wp-image-36030\" width=\"461\" height=\"101\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/random-mi-primer-juego-en-python.png 594w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/random-mi-primer-juego-en-python-300x66.png 300w\" sizes=\"(max-width: 461px) 100vw, 461px\" \/><\/figure><\/div>\n\n\n\n<p>We create a loop <strong><em>while<\/em><\/strong> where as a condition we indicate that <strong><em>Attempts<\/em><\/strong> is less than 10 (with this we already have the 10 attempts). Once inside, we increase the variable <strong><em>Attempts<\/em><\/strong> in 1 and we request the entry of a number and then offer information to the user depending on whether that number is greater, less or equal to the randomly generated one.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-python.png\" alt=\"\" class=\"wp-image-36031\" width=\"619\" height=\"313\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-python.png 924w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-python-300x152.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/intentos-python-768x389.png 768w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/figure><\/div>\n\n\n\n<p>In the last part of the code we will show a message to the player that will depend on whether they have guessed the number correctly or have exceeded the number of attempts. As we can see in the code, we convert the variables to text <strong><em>Attempts<\/em><\/strong> and <strong><em>x<\/em><\/strong> with str().<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-un-juego-en-python-1024x268.png\" alt=\"\" class=\"wp-image-36032\" width=\"676\" height=\"176\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-un-juego-en-python-1024x268.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-un-juego-en-python-300x79.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-un-juego-en-python-768x201.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-un-juego-en-python.png 1406w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/figure><\/div>\n\n\n\n<p>Finally, we leave you some screenshots of the game.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/elige-un-numero-python-1024x361.png\" alt=\"\" class=\"wp-image-36033\" width=\"678\" height=\"239\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/elige-un-numero-python-1024x361.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/elige-un-numero-python-300x106.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/elige-un-numero-python-768x271.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/elige-un-numero-python.png 1368w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python-1024x721.png\" alt=\"\" class=\"wp-image-36034\" width=\"728\" height=\"512\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python-1024x721.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python-300x211.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python-768x541.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python-234x165.png 234w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/numero-magico-en-python.png 1256w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>We explain how to make a game in Python. This game consists of guessing a random number in a maximum of 10 tries. Do not miss it!<\/p>","protected":false},"author":51,"featured_media":36043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-36026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/36026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=36026"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/36026\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/36043"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=36026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=36026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=36026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}