{"id":14950,"date":"2021-04-25T09:20:58","date_gmt":"2021-04-25T09:20:58","guid":{"rendered":"https:\/\/www.askpython.com\/?p=14950"},"modified":"2022-05-19T10:42:18","modified_gmt":"2022-05-19T10:42:18","slug":"python-pygame","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-pygame","title":{"rendered":"Python Pygame: An Easy Introduction"},"content":{"rendered":"\n<p>Hey there fellow learner! Today you will be learning about the Python Pygame. <\/p>\n\n\n\n<p>Python Pygame is usually used to build games and other graphics. One can control all the logic and graphics. There are no worries about the background complexities related to audio and video.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Started with Python Pygame<\/h2>\n\n\n\n<p>Before jumping to major things let&#8217;s look at a simple code given below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport pygame \nfrom pygame.locals import * \npygame.init() \ngame_display = pygame.display.set_mode((800, 600)) \npygame.display.set_caption(&#039;My First Game&#039;) \ndef event_handler(): \n    for event in pygame.event.get(): \n        if (event.type == QUIT): \n            pygame.quit() \n            quit() \nwhile True: \n    event_handler() \n    pygame.display.update()\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>import pygame<\/strong> and <strong>from pygame. locals import * &#8211;<\/strong> It allows access to all the pygame functionalities and inner modules<\/li><li><strong>pygame.init() &#8211; <\/strong>Initialize pygame to provide easy access to functions and automatically start up all the pygame modules that need to be initialized.<\/li><li><strong>pygame. display.set_mode((width, height)) &#8211; <\/strong>It displays a window of a particular height and width.<\/li><li><strong>pygame. display.set_caption(&#8216;My First Game&#8217;) &#8211; <\/strong>Adding the name of the game on the top of the title screen.<\/li><li><strong>Function to handle the events &#8211; <\/strong>One needs to define a function to handle the events happening on the screen. For now, we take into consideration one event i.e. closing the window on pressing &#8216;X&#8217; on the window.<\/li><li><strong>pygame. display.update() &#8211; <\/strong>It is used to make the necessary updates on the display.<\/li><\/ul>\n\n\n\n<p>The picture below shows the result of the code mentioned above. Just a basic black screen. Yep!<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"790\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_window_pygame.png\" alt=\"Initial Window Python Pygame\" class=\"wp-image-14962\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_window_pygame.png 1001w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_window_pygame-300x237.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_window_pygame-768x606.png 768w\" sizes=\"auto, (max-width: 1001px) 100vw, 1001px\" \/><figcaption>Initial Window Pygame<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Objects to the Python Pygame Output<\/h2>\n\n\n\n<p>The above screen is absolutely boring. Just a black screen and nothing else. Let&#8217;s start adding few elements to the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Inserting an Image<\/h3>\n\n\n\n<p>First, it is required to change the background color to white and then loading the image from the directory. Then, upload the image at a certain position. The code for the same is shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ngame_display.fill(&#039;white&#039;)\nimg = pygame.image.load(&#039;img1.png&#039;)\ngame_display.blit(img, (0, 0))\n<\/pre><\/div>\n\n\n<p>The screen below shows the output of the code typed above.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"790\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_image_pygame.png\" alt=\"Insert Image Python Pygame\" class=\"wp-image-14971\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_image_pygame.png 1001w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_image_pygame-300x237.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_image_pygame-768x606.png 768w\" sizes=\"auto, (max-width: 1001px) 100vw, 1001px\" \/><figcaption>Insert Image Pygame<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Inserting Shapes<\/h3>\n\n\n\n<p>In the Pygame library, one can draw specific pixels, lines, circles, rectangles, and any polygon by simply specifying the points to draw between. <\/p>\n\n\n\n<p><strong>Recommended read &#8211; <a href=\"https:\/\/www.askpython.com\/python-modules\/python-turtle\" class=\"rank-math-link\">How to use Python Turtle for drawing objects?<\/a><\/strong><\/p>\n\n\n\n<p><strong>Inserting Rectangle <\/strong>&#8211; To draw a rectangle, one needs to use pygame.draw.rect() which takes a number of parameters including screen name, the color of the rectangle, and dimensions of the rectangle ( x,y, width, height).<\/p>\n\n\n\n<p>The code below displays a red rectangle on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pygame.draw.rect(game_display, 'red', (50, 20, 120, 100))<\/code><\/pre>\n\n\n\n<p><strong>Inserting Circles <\/strong>&#8211; Inserting circles require parameters such as Screen name, color, and coordinates of the center and the radius of the circle.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pygame.draw.circle(game_display, 'yellow', (150,170),40)<\/code><\/pre>\n\n\n\n<p>The output of the two shapes drawn on screen is as follows.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"790\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_shape_pygame.png\" alt=\"Insert Shape Python Pygame\" class=\"wp-image-14976\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_shape_pygame.png 1001w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_shape_pygame-300x237.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Insert_shape_pygame-768x606.png 768w\" sizes=\"auto, (max-width: 1001px) 100vw, 1001px\" \/><figcaption>Insert Shape Pygame<\/figcaption><\/figure><\/div>\n\n\n\n<p>Refer to the <a href=\"https:\/\/www.pygame.org\/docs\/ref\/draw.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Pygame.draw official documentation<\/a> to learn all the different shapes that you can draw using this module. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a simple scenery using Python Pygame<\/h2>\n\n\n\n<p>The code below displays a simple scenery on the screen. Try it out on your own to get the same results!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pygame \nfrom pygame.locals import * \npygame.init() \ngame_display = pygame.display.set_mode((800, 250)) \npygame.display.set_caption(&#039;My First Game&#039;)\n\npygame.draw.rect(game_display, &#039;lightblue&#039;, (0, 0, 800, 200))\npygame.draw.circle(game_display, &#039;yellow&#039;, (400,160),40)\npygame.draw.polygon(game_display, &#039;brown&#039;,((0,200),(50,100),(100,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((100,200),(150,100),(200,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((200,200),(250,100),(300,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((300,200),(350,100),(400,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((400,200),(450,100),(500,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((500,200),(550,100),(600,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((600,200),(650,100),(700,200)))\npygame.draw.polygon(game_display, &#039;brown&#039;,((700,200),(750,100),(800,200)))\npygame.draw.rect(game_display, &#039;lightgreen&#039;, (0,200, 800, 50))\n\ndef event_handler(): \n    for event in pygame.event.get(): \n        if (event.type == QUIT): \n            pygame.quit() \n            quit() \nwhile True: \n    event_handler() \n    pygame.display.update()\n<\/pre><\/div>\n\n\n<p><strong>The results of the code above are as shown below:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"351\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Scenery_using_pygame.png\" alt=\"Scenery Using Pygame\" class=\"wp-image-14984\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Scenery_using_pygame.png 1001w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Scenery_using_pygame-300x105.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Scenery_using_pygame-768x269.png 768w\" sizes=\"auto, (max-width: 1001px) 100vw, 1001px\" \/><figcaption>Scenery Using Pygame<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, you learned some basic concepts of pygame and how to create simple shapes and objects using the same. Keep Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there fellow learner! Today you will be learning about the Python Pygame. Python Pygame is usually used to build games and other graphics. One can control all the logic and graphics. There are no worries about the background complexities related to audio and video. Getting Started with Python Pygame Before jumping to major things [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":15003,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-14950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=14950"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14950\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/15003"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=14950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=14950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=14950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}