{"id":2780,"date":"2019-08-09T18:48:10","date_gmt":"2019-08-09T16:48:10","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2780"},"modified":"2019-08-09T19:16:58","modified_gmt":"2019-08-09T17:16:58","slug":"starting-with-pygame","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/starting-with-pygame\/","title":{"rendered":"Starting with Pygame"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>If you want to learn how to do a game with python you have to learn how to use the module pygame. We will take a tour through some basic stuffs.<\/p>\n<p>In this first example we will set a screen of 400&#215;300 dimensions, with a loop in wich you will draw circles as you move the mouse in the window.<\/p>\n<p>You start with<\/p>\n<pre class=\"lang:default decode:true \">pygame init()<\/pre>\n<p>then you define the screen and give the window a title<\/p>\n<pre class=\"lang:default decode:true\">screen = pygame.display.set_mode((400,300))\r\npygame.display.set_caption('A bit Racey')<\/pre>\n<p>To control the frame rate:<\/p>\n<pre class=\"lang:default decode:true \">clock = pygame.time.Clock()<\/pre>\n<p>then you start the infinite loop with:<\/p>\n<pre class=\"lang:default decode:true\">while loop:<\/pre>\n<p>It will listen to every event that happens:<\/p>\n<pre class=\"lang:default decode:true \">for event in pygame.event.get():<\/pre>\n<p>&nbsp;<\/p>\n<p>To be able to close the windows properly:<\/p>\n<pre class=\"lang:default decode:true\">if event.type == pygame.QUIT:\r\n    loop = False<\/pre>\n<p>Then you get the position of the mouse to make the circle related to this position:<\/p>\n<pre class=\"lang:default decode:true \">pos = pygame.mouse.get_pos()<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/drawcircles.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2786 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/drawcircles.png\" alt=\"\" width=\"363\" height=\"361\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/drawcircles.png 363w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/drawcircles-150x150.png 150w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/drawcircles-320x318.png 320w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/a><\/p>\n<p>So, you draw the circle with screen (the display object), the color, the center coordinates (that are the position of the mouse that we get above) and the radius and the width.<\/p>\n<pre class=\"lang:default decode:true \">pygame.draw.circle(screen, (0,0,255), pos, 15, 1)<\/pre>\n<p>Then you update the screen<\/p>\n<pre class=\"lang:default decode:true \">pygame.display.update()<\/pre>\n<p>and you make it do it for 60 frames for second<\/p>\n<pre class=\"lang:default decode:true \">clock.tick(60)<\/pre>\n<p>When you click the quit button, the\u00a0 window will close:<\/p>\n<pre class=\"lang:default decode:true \">pygame.quit()<\/pre>\n<h2>The entire code to make this pygame app<\/h2>\n<p>Here are two images of this pygame app in wich you can &#8220;draw&#8221; on the window. You move the mouse and a circle is drawn making these shapes you can see in the images below. With some little changes you could:<\/p>\n<ul>\n<li>draw only when you press the mouse<\/li>\n<li>change color when you press the keyboard<\/li>\n<li>change the dimentions of the &#8216;brush&#8217;<\/li>\n<li>change the shape of the brush<\/li>\n<li>ecc.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2788\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1.png\" alt=\"\" width=\"358\" height=\"257\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1.png 602w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1-320x230.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1-321x229.png 321w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/> <\/a><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1b.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2789\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1b.png\" alt=\"\" width=\"312\" height=\"258\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1b.png 402w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/game1b-320x264.png 320w\" sizes=\"auto, (max-width: 312px) 100vw, 312px\" \/><\/a><\/p>\n<pre class=\"lang:default decode:true\">import pygame\r\n\r\npygame.init()\r\nscreen = pygame.display.set_mode((400,300))\r\npygame.display.set_caption('A bit Racey')\r\nclock = pygame.time.Clock()\r\n\r\nloop = True\r\nwhile loop:\r\n\r\n    for event in pygame.event.get():\r\n        if event.type == pygame.QUIT:\r\n            loop = False\r\n    pos = pygame.mouse.get_pos()\r\n    pygame.draw.circle(screen, (0,0,255), pos, 15, 1)\r\n    pygame.display.update()\r\n    clock.tick(60)\r\n\r\n\r\npygame.quit()\r\n# quit()<\/pre>\n<p>You can make the mouse invisible if you want:<\/p>\n<div style=\"background:gold\">\r\n<blockquote>pygame.mouse.<code class=\"descname\">set_visible<\/code><span class=\"sig-paren\">(<\/span><span class=\"sig-paren\">)<\/span>\r\n<div class=\"line-block\">\r\n<div class=\"line\"><span class=\"summaryline\">hide or show the mouse cursor<\/span><\/div>\r\n<div class=\"line\"><span class=\"signature\">set_visible(bool) -&gt; bool<\/span><\/div>\r\n<\/div>\r\nIf the bool argument is true, the mouse cursor will be visible. This will return the previous visible state of the cursor.<\/blockquote>\r\n<\/div>\n<h2>The video about the first tutorial for pygame and python<\/h2>\n<p><iframe loading=\"lazy\" title=\"Pygame tutorial n.1\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/jdUyhVfqnfI?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Maybe in the next video we will see how to make the changes I wrote about above.<\/p>\n<p>To be continued<\/p>\n\n<h3>Pygame's Platform Game<\/h3>\r\n\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-5-sounds-and-mixer\/\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/01\/climbercover.png?w=557&ssl=1\"\/ width=\"50%\"><\/a>\r\n<script>\r\nvar title = \"Platform Pygame\";\r\n\t\tvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-animation-of-a-sprite-v-1-3\/\",\"Animation 1.3\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-sprite-animation-v-2-better-coding-test-it-checking-fps-on-the-screen\/\",\"Animation 1.2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-how-to-display-the-frame-rate-fps-on-the-screen\/\",\"Display Frame rate\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-sprite-animation-update\/\",\"Animation 1.1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platformer-1\/\",\"Pygame Platform Game 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-platform-game-2\/\",\"Pygame Platform 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-3-recap-cheatsheet\/\",\"Pygame PLatform 3 - recap and some Cheat Sheet\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-4-background-and-stuffs\/\",\"Pygame Platform 4 - Background & organizing code\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-5-sounds-and-mixer\/\",\"Pygame Platform 5 - Sounds\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/platform-game-in-detail-part-1\/\",\"Game in detail part 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/map-maker-1-2\/\", \"Map maker 1.2\"]\r\n\t\t];\r\n\t\t<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\r\n<h3>Other Pygame's posts<\/h3>\r\n\r\n<script>\r\nvar title = \"Pygame's Posts\"\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platformer-1\/\",\"Platform game 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/make-a-platform-game-with-pygame-dafluffypotato\/\",\"DaFluffyPotato Platform Tutorials\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-and-classic-arcade-games-pong\/\",\"Pong Game Full\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-draws-in-colors-app-to-draw-with-pygame\/\",\"PyGameGIF 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-draw-app-with-animation\/\",\"PyGameGIF 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pydraw-2-0-app-to-draw-gif\/\",\"PyDraw 2.0\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-drawing-2\/\",\"Draw with Pygame\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/animation-with-pygame\",\"Sprite animation 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/animation-on-pygame-2-free-characters-and-more-actions\/\",\"Sprite animation 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/starting-with-pygame\/\",\"Starting movements with Pygame\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-3-move-sprite\/\", \"Move a Sprite\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-4-fonts\/\",\"Text and Fonts\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-animate-a-sprite\/\", \"Animate a sprite\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-and-mouse-events\/\",\"Mouse events\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pgp-aka-pygamepresentation-project\/\",\"Pygame presentation\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/moving-the-player-in-pygame-with-key-get_pressed\/\",\"How to use key.get_pressed()\"]\r\n]\r\n<\/script>\r\n\r\n\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"Some simple examples to start using the pygame library to play with Python programming language\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/starting-with-pygame\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2784,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,191],"tags":[],"class_list":["post-2780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-pygame"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2780"}],"version-history":[{"count":10,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2780\/revisions"}],"predecessor-version":[{"id":2794,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2780\/revisions\/2794"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2784"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}