{"id":1028,"date":"2018-08-09T09:25:51","date_gmt":"2018-08-09T07:25:51","guid":{"rendered":"http:\/\/pythonprogramming.altervista.org\/?p=1028"},"modified":"2019-08-21T10:09:13","modified_gmt":"2019-08-21T08:09:13","slug":"pygame-and-mouse-events","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/pygame-and-mouse-events\/","title":{"rendered":"Pygame and mouse events"},"content":{"rendered":"<h2>MOUSEBUTTONDOWM &#8211; MOUSEBUTTONUP<\/h2>\n<p>Let&#8217;s see how to check when the mouse button is pressed. Not only we will check the mouse button on the left, but the middle one, the right one and also the when mousewheel will be pushed up or down.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/cdn.iconscout.com\/icon\/free\/png-256\/computer-mouse-12-667864.png\" alt=\"Risultati immagini per mouse icon png\" \/><\/p>\n<h2>Initialize the window<\/h2>\n<p>First<\/p>\n<ul>\n<li>we start pygame with pygame.init(),<\/li>\n<li>init the font<\/li>\n<li>create a font object in Comin Sans MS, 14 of size<\/li>\n<li>then we create the clock object to control the frame rate<\/li>\n<li>we create the SURFACE object (screen)<\/li>\n<li>we give the window a title with set_caption method of display<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import pygame\r\n\r\n# Initialize pygame and the screen\r\npygame.init()\r\npygame.font.init()\r\nmyfont = pygame.font.SysFont('Comic Sans MS', 14)\r\nclock = pygame.time.Clock() # frame rate\r\nscreen = pygame.display.set_mode((400, 400)) \r\npygame.display.set_caption('Mouse events')<\/pre>\n<p>Then we create a little function to render the text<\/p>\n<pre class=\"lang:default decode:true \">def update_text(text, color=(0, 255, 255)):\r\n    text = text.encode()\r\n    mf = myfont.render(text, True, color)\r\n    screen.blit(mf, (10, 10))<\/pre>\n<p>In the main function we start with some variable for colors, text etc.<\/p>\n<pre class=\"lang:default decode:true \">def main():\r\n    c = 0\r\n    tc = (c,c,c)\r\n    text = \"'http:\/\/pythonprogramming.altervista.org'\"\r\n    color = (255,255,255)\r\n    update_text(text, color=color)\r\n    loop = 1\r\n    CORAL = (128,0,0)\r\n    DARKGREEN = (0,255,128)\r\n    COLOR = CORAL<\/pre>\n<p>The interesting part is the loop where we get the input of the user and we show different text for every event<\/p>\n<pre class=\"lang:default decode:true \">    while loop:\r\n        screen.fill(COLOR)\r\n        clock.tick(60)\r\n        events = pygame.event.get()\r\n        update_text(text, color=color)\r\n        for event in events:\r\n            if event.type == pygame.QUIT:\r\n                loop = 0\r\n            elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:\r\n                if event.type == pygame.MOUSEBUTTONDOWN:\r\n                    action, color = \"pressed\", (0,255, 255)\r\n                elif event.type == pygame.MOUSEBUTTONUP:\r\n                    action, color = \"released\", (255, 64, 64)\r\n                if event.button == 4:\r\n                    print(\"MOUSEWHEEL UP\")\r\n                    action = \"MOUSEWHEEL UP\"\r\n                    COLOR = CORAL\r\n                if event.button == 5:\r\n                    print(\"MOUSEWHEEL DOWN\")\r\n                    action = \"MOUSEWHEEL DOWN\"\r\n                    COLOR = DARKGREEN\r\n\r\n\r\n                text = f'button {event.button} {action} in the position {event.pos}'\r\n                print(text)\r\n        pygame.display.update()\r\n    pygame.quit()<\/pre>\n<p>This is the whole code:<\/p>\n<pre class=\"lang:default decode:true\">import pygame\r\n\r\n# Initialize pygame and the screen\r\npygame.init()\r\npygame.font.init()\r\nmyfont = pygame.font.SysFont('Comic Sans MS', 14)\r\nclock = pygame.time.Clock() # frame rate\r\nscreen = pygame.display.set_mode((400, 400)) \r\npygame.display.set_caption('Mouse events')\r\n\r\ndef update_text(text, color=(0, 255, 255)):\r\n    text = text.encode()\r\n    mf = myfont.render(text, True, color)\r\n    screen.blit(mf, (10, 10))\r\n\r\n\r\ndef main():\r\n    c = 0\r\n    tc = (c,c,c)\r\n    text = \"'http:\/\/pythonprogramming.altervista.org'\"\r\n    color = (255,255,255)\r\n    update_text(text, color=color)\r\n    loop = 1\r\n    CORAL = (128,0,0)\r\n    DARKGREEN = (0,255,128)\r\n    COLOR = CORAL\r\n    while loop:\r\n        screen.fill(COLOR)\r\n        clock.tick(60)\r\n        events = pygame.event.get()\r\n        update_text(text, color=color)\r\n        for event in events:\r\n            if event.type == pygame.QUIT:\r\n                loop = 0\r\n            elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:\r\n                if event.type == pygame.MOUSEBUTTONDOWN:\r\n                    action, color = \"pressed\", (0,255, 255)\r\n                elif event.type == pygame.MOUSEBUTTONUP:\r\n                    action, color = \"released\", (255, 64, 64)\r\n                if event.button == 4:\r\n                    print(\"MOUSEWHEEL UP\")\r\n                    action = \"MOUSEWHEEL UP\"\r\n                    COLOR = CORAL\r\n                if event.button == 5:\r\n                    print(\"MOUSEWHEEL DOWN\")\r\n                    action = \"MOUSEWHEEL DOWN\"\r\n                    COLOR = DARKGREEN\r\n\r\n\r\n                text = f'button {event.button} {action} in the position {event.pos}'\r\n                print(text)\r\n        pygame.display.update()\r\n    pygame.quit()\r\n\r\nmain()<\/pre>\n<p>This is how the window appears together with the messages on the console.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/mouse2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2919\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/mouse2.png\" alt=\"\" width=\"861\" height=\"463\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/mouse2.png 861w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/mouse2-320x172.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/mouse2-768x413.png 768w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\" \/><\/a><\/p>\n<p>In this video you see the window in action:<\/p>\n<div style=\"width: 747px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1028-1\" width=\"747\" height=\"420\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/output2.mp4?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/output2.mp4\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/output2.mp4<\/a><\/video><\/div>\n<p>To be continued&#8230;<\/p>\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":"How to get the mouse button events with pygame\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/pygame-and-mouse-events\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3090,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[191],"tags":[136,229,194],"class_list":["post-1028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pygame","tag-events","tag-mouse","tag-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\/1028","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=1028"}],"version-history":[{"count":7,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"predecessor-version":[{"id":2921,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1028\/revisions\/2921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3090"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}