{"id":7225,"date":"2020-09-04T10:15:56","date_gmt":"2020-09-04T08:15:56","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=7225"},"modified":"2023-09-17T20:29:00","modified_gmt":"2023-09-17T18:29:00","slug":"buttons-in-pygame","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/buttons-in-pygame\/","title":{"rendered":"How to make Buttons in pygame"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">A new version of the code here:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\n\nscreen = pygame.display.set_mode((800, 600))\nbuttons = pygame.sprite.Group()\nclass Button(pygame.sprite.Sprite):\n    ''' Create a button clickable with changing hover color'''\n\n    def __init__(self, text=\"Click\",\n                pos=(0,0), fontsize=16,\n                colors=\"white on blue\", hover_colors=\"red on green\",\n                command=lambda: print(\"No command activated for this button\")):\n\n        super().__init__()\n        self.text = text\n        self.command = command\n        self.colors = colors\n        self.original_colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n        self.fgh, self.bgh = hover_colors.split(\" on \")\n        self.font = pygame.font.SysFont(\"Arial\", fontsize)\n        self.pos = pos\n        self.create_original()\n        self.create_hover_image()\n\n    def create_original(self):\n        self.image = self.create_bg(self.text, self.fg, self.bg)\n        self.original_image = self.image.copy()\n\n\n    def create_hover_image(self):\n        self.hover_image = self.create_bg(self.text, self.fgh, self.bgh)\n        self.pressed = 1\n        buttons.add(self)\n\n\n    def create_bg(self, text, fg, bg):\n        self.text = text\n        image = self.font.render(self.text, 1, fg)\n        self.rect = image.get_rect()\n        self.rect.x, self.rect.y = self.pos\n        bgo = pygame.Surface((self.rect.w, self.rect.h))\n        bgo.fill(bg)\n        bgo.blit(image, (0,0))\n        return bgo\n\n\n    def update(self):\n        ''' CHECK IF HOVER AND IF CLICK THE BUTTON '''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            self.image = self.hover_image\n            self.check_if_click()\n        else:\n            self.image = self.original_image\n\n\n    def check_if_click(self):\n        ''' checks if you click on the button and makes the call to the action just one time'''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            if pygame.mouse.get_pressed()[0] and self.pressed == 1:\n                # print(\"Execunting code for button '\" + self.text + \"'\")\n                self.command()\n                self.pressed = 0\n            if pygame.mouse.get_pressed() == (0,0,0):\n                self.pressed = 1\n\n\nif __name__ == \"__main__\":\n# Hello, this is a snippet\n\n    pygame.init()\n\n    pygame.display.set_caption('Example of button')\n    screen = pygame.display.set_mode((1000, 800))\n    clock = pygame.time.Clock()\n\n    def window():\n        b1 = Button(\"CLICK ME\", pos=(100,100),\n            fontsize=36,\n            colors=\"red on green\",\n            hover_colors=\"green on red\",\n            command=lambda: print(\"clicked right now\"))\n\n\n    window()\n\n\n    is_running = True\n    while is_running:\n\n        for event in pygame.event.get():\n         if event.type == pygame.QUIT:\n             is_running = False\n\n        # to show buttons created\n        buttons.update()\n        buttons.draw(screen)\n\n        pygame.display.update()\n        clock.tick(60)\n\n    pygame.quit()<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"408\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33-960x408.png\" alt=\"\" class=\"wp-image-13621\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33-960x408.png 960w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33-320x136.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33-768x326.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/09\/image-33.png 1007w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you go over the button, it will change the color. If you click it it will do some action (in the example, it will just print a text on the terminal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/formazione\/pygame_widgets\">https:\/\/github.com\/formazione\/pygame_widgets<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s an update to this code, you can find it at the end of this post and also in this new post: <a href=\"https:\/\/pythonprogramming.altervista.org\/button-widget-in-pygame-updated\/\">https:\/\/pythonprogramming.altervista.org\/button-widget-in-pygame-updated\/<\/a>. I added the command to each button. The style is sick, so, go check it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Welcome to our blog about creating buttons in <strong>Pygame<\/strong>! In this post, we will be discussing the step-by-step process of creating <strong>buttons <\/strong>in Pygame. Buttons are an essential element of any user interface, and they allow users to interact with the program. Pygame provides an easy and straightforward way to create buttons for your game or application. We will cover topics such as <strong>creating a basic window<\/strong>, creating a <strong>class for the Button<\/strong>,<strong> rendering text<\/strong> to show on the button, creating a <strong>surface<\/strong> with the size of the text, giving the surface a <strong>color <\/strong>or<strong> image<\/strong>, blitting the text on the surface, creating a method to change the text of the button, blitting the button surface on the screen, and <strong>intercepting the click of the mouse<\/strong>. By the end of this post, you will have a solid understanding of how to create buttons in Pygame and implement them into your projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Github repository with the code for buttons in pygame<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click on this link to get the code to make buttons with pygame.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/formazione\/pygame_button\">https:\/\/github.com\/formazione\/pygame_button<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Repository with a quiz that uses buttons and labels in pygame<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/formazione\/pygame_quiz.git\">https:\/\/github.com\/formazione\/pygame_quiz.git<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to make a button in pygame<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So here is the steps we will follow in the process of making a button: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>create a basic <strong>window<\/strong><\/li>\n\n\n\n<li>create a <strong>class <\/strong>for the Button<\/li>\n\n\n\n<li><strong>render <\/strong>the text to show on the button<\/li>\n\n\n\n<li>create a <strong>surface <\/strong>with the size of the text<\/li>\n\n\n\n<li>give the surface a <strong>color <\/strong>(or an image as background&#8230; I can make this in another post)<\/li>\n\n\n\n<li><strong>blit <\/strong>the text on the surface<\/li>\n\n\n\n<li>create a method to <strong>change <\/strong>the text of the butto (when you click for example)<\/li>\n\n\n\n<li><strong>blit <\/strong>the button.surface on the screen<\/li>\n\n\n\n<li><strong>intercept <\/strong>click of the <strong>mouse<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-65.png\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"243\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-65.png\" alt=\"\" class=\"wp-image-9104\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-65.png 498w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-65-320x156.png 320w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/a><figcaption class=\"wp-element-caption\">Before user clicks<\/figcaption><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-66.png\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"273\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-66.png\" alt=\"\" class=\"wp-image-9105\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-66.png 511w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/03\/image-66-320x171.png 320w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/a><figcaption class=\"wp-element-caption\">After user clicks<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"live-video-coding-of-pygame-button\">Live video coding of pygame button<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Pygame's Button... fun!\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/Piqkq2r8Cq4?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>\n\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"github-repository\">Github repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/formazione\/pygame_button\">Link to github repository<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"code\">Code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The code provided is an example of how to create a button in Pygame using Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing pygame<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-32.png\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"137\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-32.png\" alt=\"\" class=\"wp-image-12610\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-32.png 445w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-32-320x99.png 320w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The first line of the code <strong>imports <\/strong>the Pygame module, which provides access to the Pygame library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second line <strong>initializes Pygame<\/strong> and sets up the Pygame environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The third line <strong>sets the screen <\/strong>size to be 500 pixels wide and 600 pixels tall. The screen variable holds a reference to the display surface, which is where all the drawing will take place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The fourth line<strong> creates a clock <\/strong>object that will be used to regulate the frame rate of the game.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The fifth line<strong> creates a font<\/strong> object using the Arial font with a size of 20 pixels.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Button class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next lines of code define a<strong> Button class<\/strong>. The Button class <strong>takes several arguments<\/strong>, including the text to display on the button, the position of the button, the font size to use, the background color of the button, and the feedback text to display when the button is clicked.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The __<strong>init__<\/strong> <strong>method <\/strong>of the Button class initializes the attributes of the Button object. It sets the position of the button, the font size, and the feedback text. It then calls the change_text method to set the initial text of the button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-31.png\"><img loading=\"lazy\" decoding=\"async\" width=\"607\" height=\"170\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-31.png\" alt=\"\" class=\"wp-image-12609\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-31.png 607w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-31-320x90.png 320w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>change_text method <\/strong>of the Button class changes the text displayed on the button when it is clicked. It takes the new text as an argument and sets the text attribute of the Button object. It then creates a new surface for the button with the new text and background color. Finally, it updates the Rect object of the Button object with the new size of the button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-30.png\"><img loading=\"lazy\" decoding=\"async\" width=\"677\" height=\"166\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-30.png\" alt=\"\" class=\"wp-image-12608\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-30.png 677w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-30-320x78.png 320w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>show method<\/strong> of the Button class<strong> blits the Button<\/strong> object&#8217;s surface onto the screen at the specified position.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"blob:https:\/\/pythonprogramming.altervista.org\/de781a50-a049-4f97-a6ac-5b1ca2377f4c\" alt=\"\" style=\"width:491px;height:62px\" width=\"491\" height=\"62\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>click method<\/strong> of the Button class handles the button clicks. It checks if the left mouse button is clicked and if the mouse is within the boundaries of the button. If both conditions are true, it changes the text of the button to the feedback text and updates the button surface with the new text and background color.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-27.png\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"137\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-27.png\" alt=\"\" class=\"wp-image-12605\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-27.png 551w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-27-320x80.png 320w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>mainloop function<\/strong> is an infinite loop that runs the game. It waits for events and handles them. It also calls the show method of the Button object to display the button on the screen. Finally, it updates the screen and regulates the frame rate of the game using the clock object.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-26.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-26.png\" alt=\"\" class=\"wp-image-12604\" style=\"width:483px;height:211px\" width=\"483\" height=\"211\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-26.png 483w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-26-320x140.png 320w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the code, the <strong>button1 object is created<\/strong> using the Button class. It is given the text &#8220;Click here,&#8221; the position (100, 100), a font size of 30 pixels, a background color of navy, and the feedback text &#8220;You clicked me.&#8221; The mainloop function is then called to start the game.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-24.png\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"194\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-24.png\" alt=\"\" class=\"wp-image-12602\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-24.png 514w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-24-320x121.png 320w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/a><figcaption class=\"wp-element-caption\">Before you ckick<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-25.png\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"253\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-25.png\" alt=\"\" class=\"wp-image-12603\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-25.png 513w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/04\/image-25-320x158.png 320w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/a><figcaption class=\"wp-element-caption\">and after<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Overall, this code provides an excellent starting point for creating buttons in Pygame using Python. It demonstrates how to create a Button class, handle button clicks, and update the button&#8217;s text and appearance.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((500, 600))\nclock = pygame.time.Clock()\nfont = pygame.font.SysFont(\"Arial\", 20)\n\n\nclass Button:\n    \"\"\"Create a button, then blit the surface in the while loop\"\"\"\n\n    def __init__(self, text,  pos, font, bg=\"black\", feedback=\"\"):\n        self.x, self.y = pos\n        self.font = pygame.font.SysFont(\"Arial\", font)\n        if feedback == \"\":\n            self.feedback = \"text\"\n        else:\n            self.feedback = feedback\n        self.change_text(text, bg)\n\n    def change_text(self, text, bg=\"black\"):\n        \"\"\"Change the text whe you click\"\"\"\n        self.text = self.font.render(text, 1, pygame.Color(\"White\"))\n        self.size = self.text.get_size()\n        self.surface = pygame.Surface(self.size)\n        self.surface.fill(bg)\n        self.surface.blit(self.text, (0, 0))\n        self.rect = pygame.Rect(self.x, self.y, self.size[0], self.size[1])\n\n    def show(self):\n        screen.blit(button1.surface, (self.x, self.y))\n\n    def click(self, event):\n        x, y = pygame.mouse.get_pos()\n        if event.type == pygame.MOUSEBUTTONDOWN:\n            if pygame.mouse.get_pressed()[0]:\n                if self.rect.collidepoint(x, y):\n                    self.change_text(self.feedback, bg=\"red\")\n\n\ndef mainloop():\n    \"\"\" The infinite loop where things happen \"\"\"\n    while True:\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                pygame.quit()\n            button1.click(event)\n        button1.show()\n        clock.tick(30)\n        pygame.display.update()\n\n\nbutton1 = Button(\n    \"Click here\",\n    (100, 100),\n    font=30,\n    bg=\"navy\",\n    feedback=\"You clicked me\")\n\nmainloop()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"another-way-to-make-a-button\">Another way to make a button<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This way is nicer and maybe easier to implement in a game.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This code defines a function <code>button<\/code> that creates a graphical button on a Pygame screen. The function takes three arguments: the <code>screen<\/code> on which to draw the button, a <code>position<\/code> tuple representing the x and y coordinates of the top-left corner of the button, and a <code>text<\/code> string to display on the button.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The button is created using Pygame drawing functions, including <code>pygame.draw.rect()<\/code> to draw a rectangle, and <code>pygame.draw.line()<\/code> to draw lines around the rectangle. The text is rendered using <code>pygame.font.SysFont()<\/code> and <code>font.render()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The function then blits the text onto the screen using <code>screen.blit()<\/code>, and returns a <code>pygame.Rect<\/code> object representing the size and position of the button on the screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>menu()<\/code> function creates two buttons using the <code>button()<\/code> function, one with the text &#8220;Quit&#8221; and one with the text &#8220;Start&#8221;. It then enters an infinite loop that handles Pygame events. If the user clicks on the &#8220;Quit&#8221; button, the program quits. If the user presses the &#8220;s&#8221; key or clicks on the &#8220;Start&#8221; button, the <code>start()<\/code> function is called.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pythonprogramming.altervista.org\/how-to-make-buttons-in-pygame\/\">How to make Buttons in Pygame<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"still-another-way-even-better\">Still another way, even better<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/05\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"432\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/05\/image-7.png\" alt=\"\" class=\"wp-image-9435\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/05\/image-7.png 601w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/05\/image-7-320x230.png 320w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption class=\"wp-element-caption\">2 buttons<\/figcaption><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\n\ndef button(screen, position, text):\n    font = pygame.font.SysFont(\"Arial\", 50)\n    text_render = font.render(text, 1, (255, 0, 0))\n    x, y, w , h = text_render.get_rect()\n    x, y = position\n    pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)\n    pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x, y + h), (x + w , y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x + w , y+h), [x + w , y], 5)\n    pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))\n    print(\"screen.blit...\", screen.blit(text_render, (x, y)))\n    return screen.blit(text_render, (x, y)) # this is a rect pygame.Rect\n\ndef start():\n    print(\"Ok, let's go\")\n\ndef menu():\n    \"\"\" This is the menu that waits you to click the s key to start \"\"\"\n    b1 = button(screen, (400, 300), \"Quit\") # this is a pygame.Rect?\n    print(type(b1))\n    b2 = button(screen, (500, 300), \"Start\")\n    while True:\n        for event in pygame.event.get():\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                if b1.collidepoint(pygame.mouse.get_pos()): # checks a collision with a pygame.Rect and the mouse pos\n                    pygame.quit()\n                elif b2.collidepoint(pygame.mouse.get_pos()):\n                    start()\n        pygame.display.update()\n    pygame.quit()\n\nmenu()\npygame.quit()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can add some come to make some actions pressing some keys too.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\n\ndef button(screen, position, text):\n    font = pygame.font.SysFont(\"Arial\", 50)\n    text_render = font.render(text, 1, (255, 0, 0))\n    x, y, w , h = text_render.get_rect()\n    x, y = position\n    pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)\n    pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x, y + h), (x + w , y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x + w , y+h), [x + w , y], 5)\n    pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))\n    print(\"screen.blit...\", screen.blit(text_render, (x, y)))\n    return screen.blit(text_render, (x, y)) # this is a rect pygame.Rect\n\ndef start():\n    print(\"Ok, let's go\")\n\ndef menu():\n    \"\"\" This is the menu that waits you to click the s key to start \"\"\"\n    b1 = button(screen, (400, 300), \"Quit\") # this is a pygame.Rect?\n    print(type(b1))\n    b2 = button(screen, (500, 300), \"Start\")\n    while True:\n        for event in pygame.event.get():\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n                possible_events = [\n                    event.key == pygame.K_s,\n                    event.key == pygame.K_RIGHT,\n                    event.key == pygame.K_UP]\n                if any(possible_events):\n                    start()\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                if b1.collidepoint(pygame.mouse.get_pos()): # checks a collision with a pygame.Rect and the mouse pos\n                    pygame.quit()\n                elif b2.collidepoint(pygame.mouse.get_pos()):\n                    start()\n        pygame.display.update()\n    pygame.quit()\n\nmenu()\npygame.quit()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"customizable-buttons\">Customizable buttons<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\n\ndef button(screen, position, text, size, colors=\"white on blue\"):\n    fg, bg = colors.split(\" on \")\n    font = pygame.font.SysFont(\"Arial\", size)\n    text_render = font.render(text, 1, fg)\n    x, y, w , h = text_render.get_rect()\n    x, y = position\n    pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)\n    pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x, y + h), (x + w , y + h), 5)\n    pygame.draw.line(screen, (50, 50, 50), (x + w , y+h), [x + w , y], 5)\n    pygame.draw.rect(screen, bg, (x, y, w , h))\n    print(screen.blit(text_render, (x, y)))\n    return screen.blit(text_render, (x, y)) \n\ndef start():\n    print(\"Ok, let's go\")\n\ndef menu():\n    \"\"\" This is the menu that waits you to click the s key to start \"\"\"\n    # b0 contains the rect coordinates of the button\n    b0 = button(screen, (10, 10), \"Here comes the buttons\", 55, \"red on yellow\")\n    b1 = button(screen, (300, 300), \"Quit me\", 50, \"red on yellow\")\n    b2 = button(screen, (500, 300), \"Start\", 50, \"white on green\")\n    while True:\n        for event in pygame.event.get():\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n                key_to_start = event.key == pygame.K_s or event.key == pygame.K_RIGHT or event.key == pygame.K_UP\n                if key_to_start:\n                    start()\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                # check when you click if the coordinates of the pointer are in the rectangle of the buttons\n                if b1.collidepoint(pygame.mouse.get_pos()):\n                    pygame.quit()\n                elif b2.collidepoint(pygame.mouse.get_pos()):\n                    start()\n        pygame.display.update()\n    pygame.quit()\n\nmenu()\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-23.png\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"432\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-23.png\" alt=\"\" class=\"wp-image-10094\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-23.png 602w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-23-320x230.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-23-321x229.png 321w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hover-effect-over-a-button\">Hover effect over a button<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With this changes in the script now the button def has become a class. Now you can make the hover effect on each button:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>define the istance of the class Button\n<ul class=\"wp-block-list\">\n<li>surface (screen)<\/li>\n\n\n\n<li>position<\/li>\n\n\n\n<li>text<\/li>\n\n\n\n<li>size<\/li>\n\n\n\n<li>color ex: &#8220;red on yellow&#8221;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>then do the if collidepoint for the hover effect and for the action (line 62 and 69)<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\n\nbuttons = pygame.sprite.Group()\nclass Button(pygame.sprite.Sprite):\n    def __init__(self, screen, position, text, size, colors=\"white on blue\"):\n        super().__init__()\n        self.colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n        self.font = pygame.font.SysFont(\"Arial\", size)\n        self.text_render = self.font.render(text, 1, self.fg)\n        self.image = self.text_render\n        self.x, self.y, self.w , self.h = self.text_render.get_rect()\n        self.x, self.y = position\n        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)\n        self.position = position\n        self.update()\n        buttons.add(self)\n\n    def update(self):\n        self.fg, self.bg = self.colors.split(\" on \")\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y), (self.x + self.w , self.y), 5)\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x, self.y + self.h), (self.x + self.w , self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x + self.w , self.y + self.h), [self.x + self.w , self.y], 5)\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))\n        # screen.blit(self.text_render, self.position)\n\n        # self.rect = screen, position, text, size, colors=\"white on blue\"screen.blit(self.text_render, (self.x, self.y))\n\n\n\ndef start():\n    print(\"Ok, let's go\")\n\n\ndef not_hover():\n    for x in buttons:\n        x.colors = \"red on yellow\"\n        x.update()\n\ndef menu():\n    \"\"\" This is the menu that waits you to click the s key to start \"\"\"\n\n\n    while True:\n        for event in pygame.event.get():\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n\n\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n                key_to_start = event.key == pygame.K_s or event.key == pygame.K_RIGHT or event.key == pygame.K_UP\n                if key_to_start:\n                    start()\n            if event.type == pygame.MOUSEMOTION:\n                # 2. put the collide check for mouse hover here for each button\n                if b0.rect.collidepoint(pygame.mouse.get_pos()):\n                    b0.colors = \"red on green\"\n                elif b1.rect.collidepoint(pygame.mouse.get_pos()):\n                    b1.colors = \"red on green\"\n                elif b2.rect.collidepoint(pygame.mouse.get_pos()):\n                    b2.colors = \"red on green\"\n                else:\n                    # this will work for every buttons going back to original color after mouse goes out\n                    not_hover()\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                # 3. here the interactions with the click of the mouse... done\n                if b0.rect.collidepoint(pygame.mouse.get_pos()):\n                    print(b0.y)\n                if b1.rect.collidepoint(pygame.mouse.get_pos()):\n                    print(b1.y)\n        buttons.update()\n        buttons.draw(screen)\n        pygame.display.update()\n    pygame.quit()\n# 1 - create the buttons with an istance of Button here...\nb0 = Button(screen, (10, 10), \"1st button (b0) at b0 = 10\", 55, \"red on yellow\")\nb1 = Button(screen, (10, 100), \"2nd button (b1) at b1 = 100\", 55, \"red on yellow\")\n# This button has no interaction (69 add...)\nb2 = Button(screen, (10, 200), \"3rd button - no action\", 55, \"red on yellow\")\nprint(b0.x)\n# follow the comments to add buttons:\n# 1 - create the buttons with an istance of Button here... line 80\n# 2. put the collide check for mouse hover here for each button.... line 62\n# 3. here the interactions with the click of the mouse... done line 69\n\nmenu()\n<\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1050\" style=\"aspect-ratio: 1680 \/ 1050;\" width=\"1680\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/00h.mp4\"><\/video><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-80.png\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"432\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-80.png\" alt=\"\" class=\"wp-image-10248\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-80.png 602w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-80-320x230.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-80-321x229.png 321w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"making-a-better-code-to-highlight-and-to-bind-the-action-to-the-click-on-the-buttons\">Making a better code to highlight and to bind the action to the click on the buttons<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can find the code here <a href=\"https:\/\/github.com\/formazione\/pygame_button.git\">https:\/\/github.com\/formazione\/pygame_button.git<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But I will show it also here:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\nclock = pygame.time.Clock()\n\nbuttons = pygame.sprite.Group()\n\nclass Button(pygame.sprite.Sprite):\n    def __init__(self, screen, position, text, size, colors=\"white on blue\"):\n        super().__init__()\n        self.colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n        self.font = pygame.font.SysFont(\"Arial\", size)\n        self.text_render = self.font.render(text, 1, self.fg)\n        self.image = self.text_render\n        self.x, self.y, self.w , self.h = self.text_render.get_rect()\n        self.x, self.y = position\n        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)\n        self.position = position\n        self.update()\n        self.pressed = 1\n        buttons.add(self)\n\n    def update(self):\n        \n        self.fg, self.bg = self.colors.split(\" on \")\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y), (self.x + self.w , self.y), 5)\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x, self.y + self.h), (self.x + self.w , self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x + self.w , self.y + self.h), [self.x + self.w , self.y], 5)\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))\n        # screen.blit(self.text_render, self.position)\n\n        # self.rect = screen, position, text, size, colors=\"white on blue\"screen.blit(self.text_render, (self.x, self.y))\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            self.colors = \"red on green\"\n        else:\n            self.colors = \"red on yellow\"\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            if pygame.mouse.get_pressed()[0] and self.pressed == 1:\n                print(self.position)\n                self.pressed = 0\n            if pygame.mouse.get_pressed() == (0,0,0):\n                self.pressed = 1\n\n\ndef start():\n    print(\"Ok, let's go\")\n\n\n\n\ndef menu():\n    \"\"\" This is the menu that waits you to click the s key to start \"\"\"\n\n\n    while True:\n        for event in pygame.event.get():\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n\n\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n                key_to_start = event.key == pygame.K_s or event.key == pygame.K_RIGHT or event.key == pygame.K_UP\n                if key_to_start:\n                    start()\n\n \n        buttons.update()\n        buttons.draw(screen)\n        clock.tick(60)\n        pygame.display.update()\n    pygame.quit()\n# 1 - create the buttons with an istance of Button here...\nb0 = Button(screen, (10, 10), \"1st button (b0) at b0 = 10\", 55, \"red on yellow\")\nb1 = Button(screen, (10, 100), \"2nd button (b1) at b1 = 100\", 55, \"red on yellow\")\n# This button has no interaction (69 add...)\nb2 = Button(screen, (10, 200), \"3rd button - no action\", 55, \"red on yellow\")\nprint(b0.x)\n# follow the comments to add buttons:\n# 1 - create the buttons with an istance of Button here... line 80\n# 2. put the collide check for mouse hover here for each button.... line 62\n# 3. here the interactions with the click of the mouse... done line 69\n\nmenu()\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-15.png\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"366\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-15.png\" alt=\"\" class=\"wp-image-11450\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-15.png 604w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-15-320x194.png 320w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It does the same things, but the code it&#8217;s easier to implement now. Still I got to fix the action to bind to the buttons. For the moment there is only a print statement as example.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"How to make buttons in pygame v.... 26\/8\/2021 hover effect\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/y3-9RUoF8ec?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>\n\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"definitive-version-with-actions-on-click\">Definitive version with Actions on click<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ok, now the basic feature are all in the class. Now we can easily add a command to each button with the argument command<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">in this function I put all the buttons<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def buttons_def():\n    b0 = Button((10, 10), \"Click me now\", 55, \"black on white\",\n        command=on_click)\n    b1 = Button((10, 100), \"Run the program\", 40, \"black on red\", command=on_run)\n\n    b2 = Button((10, 170), \"Save this file\", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_save)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a video that shows how it works<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1050\" style=\"aspect-ratio: 1680 \/ 1050;\" width=\"1680\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/000-1.mp4\"><\/video><figcaption class=\"wp-element-caption\">Video with buttons in pygame<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The code is in this github repository. You can use the main.py file or just the buttons2.py in the folder code<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/formazione\/pygame_button\">Get the code here<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code is this anyhow (the one in one file)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\nimport pygame.gfxdraw\n\n\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\nclock = pygame.time.Clock()\nbuttons = pygame.sprite.Group()\nclass Button(pygame.sprite.Sprite):\n    def __init__(self, position, text, size,\n        colors=\"white on blue\",\n        hover_colors=\"red on green\",\n        style=1, borderc=(255,255,255),\n        command=lambda: print(\"No command activated for this button\")):\n        # the hover_colors attribute needs to be fixed\n        super().__init__()\n        self.text = text\n        self.command = command\n        # --- colors ---\n        self.colors = colors\n        self.original_colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n        if hover_colors == \"red on green\":\n            self.hover_colors = f\"{self.bg} on {self.fg}\"\n        else:\n            self.hover_colors = hover_colors\n        self.style = style\n        self.borderc = borderc # for the style2\n        # font\n        self.font = pygame.font.SysFont(\"Arial\", size)\n        self.render()\n        self.x, self.y, self.w , self.h = self.text_render.get_rect()\n        self.x, self.y = position\n        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)\n        self.position = position\n        self.pressed = 1\n        buttons.add(self)\n\n    def render(self):\n        self.text_render = self.font.render(self.text, 1, self.fg)\n        self.image = self.text_render\n\n    def update(self):\n        self.fg, self.bg = self.colors.split(\" on \")\n        if self.style == 1:\n            self.draw_button1()\n        elif self.style == 2:\n            self.draw_button2()\n        self.hover()\n        self.click()\n\n    def draw_button1(self):\n        ''' draws 4 lines around the button and the background '''\n        # horizontal up\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y), (self.x + self.w , self.y), 5)\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y + self.h), 5)\n        # horizontal down\n        pygame.draw.line(screen, (50, 50, 50), (self.x, self.y + self.h), (self.x + self.w , self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x + self.w , self.y + self.h), [self.x + self.w , self.y], 5)\n        # background of the button\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))  \n\n    def draw_button2(self):\n        ''' a linear border '''\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))\n        pygame.gfxdraw.rectangle(screen, (self.x, self.y, self.w , self.h), self.borderc)\n\n    def hover(self):\n        ''' checks if the mouse is over the button and changes the color if it is true '''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            # you can change the colors when the pointer is on the button if you want\n            self.colors = self.hover_colors\n            # pygame.mouse.set_cursor(*pygame.cursors.diamond)\n        else:\n            self.colors = self.original_colors\n            \n        self.render()\n\n    def click(self):\n        ''' checks if you click on the button and makes the call to the action just one time'''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            if pygame.mouse.get_pressed()[0] and self.pressed == 1:\n                print(\"Execunting code for button '\" + self.text + \"'\")\n                self.command()\n                self.pressed = 0\n            if pygame.mouse.get_pressed() == (0,0,0):\n                self.pressed = 1\n\n\n\n\n# FUNCTIONS for the buttons on click\n# I used this convention ... on_+text of the button\n\ndef on_click():\n    print(\"Ciao bello\")\n\ndef on_run():\n    print(\"Ciao bello questo \u00e8 RUN\")\n\ndef on_save():\n    print(\"This is Save\")\n\ndef buttons_def():\n    b0 = Button((10, 10), \"Click me now\", 55, \"black on white\",\n        command=on_click)\n    b1 = Button((10, 100), \"Run the program\", 40, \"black on red\", command=on_run)\n\n    b2 = Button((10, 170), \"Save this file\", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_save)\n\n# ======================= this code is just for example, start the program from the main file\n# in the main folder, I mean, you can also use this file only, but I prefer from the main file\n# 29.8.2021\n\nif __name__ == '__main__':\n    pygame.init()\n    game_on = 0\n    def loop():\n        # BUTTONS ISTANCES\n        game_on = 1\n        buttons_def()\n        while True:\n            for event in pygame.event.get():\n                if (event.type == pygame.QUIT):\n                    game_on = 0\n                    pygame.quit()\n                if event.type == pygame.KEYDOWN:\n                    if event.key == pygame.K_ESCAPE:\n                        pygame.quit()\n                        game_on = 0\n            if game_on:\n                buttons.update()\n                buttons.draw(screen)\n            else:\n                pygame.quit()\n                sys.exit()\n            buttons.draw(screen)\n            clock.tick(60)\n            pygame.display.update()\n        pygame.quit()\n\n\n\n\n    loop()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the output<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16.png\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"555\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16.png\" alt=\"\" class=\"wp-image-11452\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16.png 782w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16-320x227.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16-768x545.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-16-321x229.png 321w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-95.png\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-95.png\" alt=\"\" class=\"wp-image-10320\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-95.png 800w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-95-320x240.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/08\/image-95-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-the-button-class-to-make-a-test-in-pygame\">Using the button class to make a test in pygame<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go here to see the latest updates of this code <a href=\"https:\/\/github.com\/formazione\/pygame_button\">https:\/\/github.com\/formazione\/pygame_button<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\nimport pygame.gfxdraw\nimport sys\nimport time\nimport random\n\n\n\npygame.init()\nscreen = pygame.display.set_mode((600, 400))\nclock = pygame.time.Clock()\nbuttons = pygame.sprite.Group()\nnum = 1\nclass Button(pygame.sprite.Sprite):\n    \n    def __init__(self, position, text, size,\n        colors=\"white on blue\",\n        hover_colors=\"red on green\",\n        style=1,\n        borderc=(255,255,255),\n        command=lambda: print(\"No command activated for this button\")):\n\n        # the hover_colors attribute needs to be fixed\n        super().__init__()\n        global num\n\n\n        self.text = text\n\n        self.command = command\n        # --- colors ---\n        self.colors = colors\n        self.original_colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n\n        if hover_colors == \"red on green\":\n            self.hover_colors = f\"{self.bg} on {self.fg}\"\n        else:\n            self.hover_colors = hover_colors\n\n        self.style = style\n        self.borderc = borderc # for the style2\n        # font\n        self.font = pygame.font.SysFont(\"Arial\", size)\n        self.render(self.text)\n        self.x, self.y, self.w , self.h = self.text_render.get_rect()\n        self.x, self.y = position\n        self.rect = pygame.Rect(self.x, self.y, 500, self.h)\n        self.position = position\n        self.pressed = 1\n        # the groups with all the buttons\n        buttons.add(self)\n\n    def render(self, text):\n        # we have a surface\n        self.text_render = self.font.render(text, 1, self.fg)\n        # memorize the surface in the image attributes\n        self.image = self.text_render\n\n    def update(self):\n        self.fg, self.bg = self.colors.split(\" on \")\n        if self.style == 1:\n            self.draw_button1()\n        elif self.style == 2:\n            self.draw_button2()\n        if self.command != None:\n            self.hover()\n            self.click()\n\n    def draw_button1(self):\n        ''' draws 4 lines around the button and the background '''\n        # horizontal up\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y), (self.x + self.w , self.y), 5)\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y + self.h), 5)\n        # horizontal down\n        pygame.draw.line(screen, (50, 50, 50), (self.x, self.y + self.h), (self.x + self.w , self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x + self.w , self.y + self.h), [self.x + self.w , self.y], 5)\n        # background of the button\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))  \n\n    def draw_button2(self):\n        ''' a linear border '''\n        pygame.draw.rect(screen, self.bg, (self.x - 50, self.y, 500 , self.h))\n        pygame.gfxdraw.rectangle(screen, (self.x - 50, self.y, 500 , self.h), self.borderc)\n\n    def check_collision(self):\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            # you can change the colors when the pointer is on the button if you want\n            self.colors = self.hover_colors\n            # pygame.mouse.set_cursor(*pygame.cursors.diamond)\n        else:\n            self.colors = self.original_colors\n            # pygame.mouse.set_cursor(*pygame.cursors.arrow)\n\n\n    def hover(self):\n        ''' checks if the mouse is over the button and changes the color if it is true '''\n        if self.style == 1:\n            self.check_collision()\n            self.render()\n        else:\n            self.check_collision()\n\n    def click(self):\n        ''' checks if you click on the button and makes the call to the action just one time'''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            if pygame.mouse.get_pressed()[0] and self.pressed == 1:\n                print(\"The answer is:'\" + self.text + \"'\")\n                self.command()\n                self.pressed = 0\n            if pygame.mouse.get_pressed() == (0,0,0):\n                self.pressed = 1\n\n\n\n\n# FUNCTIONS for the buttons on click\n# I used this convention ... on_+text of the button\n\ndef on_click():\n    print(\"Click on one answer\")\n\ndef on_run():\n    print(\"Ciao bello questo \u00e8 RUN\")\n\ndef on_save():\n    print(\"This is Save\")\n\ndef on_right():\n    print(\"Right\")\n    forward()\n\ndef on_false():\n    print(\"Wrong\")\n    forward()\n\ndef forward():\n    global qnum\n    \n    screen.fill(0)\n    if qnum &lt; len(questions):\n        time.sleep(.3)\n        qnum += 1\n        question(qnum)\n\n\n\nquestions = [\n    [\"What is Italy's Capital?\", [\"Rome\", \"Paris\", \"Tokyo\", \"Madrid\"]],\n    [\"What is France's Capital?\", [\"Paris\", \"Rome\", \"Tokyo\", \"Madrid\"]],\n    [\"What is England's Capital?\", [\"London\", \"Rome\", \"Tokyo\", \"Madrid\"]],\n]\n\n\ndef question(qnum):\n    ''' put your buttons here '''\n\n    for sprites in buttons:\n        sprites.kill()\n\n    pos = [100, 150, 200, 250]\n    random.shuffle(pos)\n    # this is a label, a button with no border does nothing: command = None\n    Button((0, 0), str(qnum-1), 20, \"white on black\",\n        hover_colors=\"blue on orange\", style=2, borderc=(0,0,0),\n        command=None)\n\n    Button((10, 10), questions[qnum-1][0], 55, \"white on black\",\n        hover_colors=\"blue on orange\", style=2, borderc=(0,0,0),\n        command=None)\n\n    # ______------_____ BUTTONS FOR ANSWERS _____------______ #\n\n    Button((10, 100), \"1. \", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=None)\n    Button((10, 150), \"2. \", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=None)\n    Button((10, 200), \"3. \", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=None)\n    Button((10, 250), \"4. \", 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=None)\n\n    Button((50, pos[0]), questions[qnum-1][1][0], 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_right)\n\n\n    Button((50, pos[1]), questions[qnum-1][1][1], 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_false)\n\n    Button((50, pos[2]), questions[qnum-1][1][2], 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_false)\n\n    Button((50, pos[3]), questions[qnum-1][1][3], 36, \"red on yellow\",\n        hover_colors=\"blue on orange\", style=2, borderc=(255,255,0),\n        command=on_false)\n\n# ======================= this code is just for example, start the program from the main file\n# in the main folder, I mean, you can also use this file only, but I prefer from the main file\n# 29.8.2021\nqnum = 1\nif __name__ == '__main__':\n    pygame.init()\n    game_on = 0\n    def loop():\n        # BUTTONS ISTANCES\n        global qnum\n\n\n        game_on = 1\n        question(qnum)\n        while True:\n            screen.fill(0)\n            for event in pygame.event.get():\n                if (event.type == pygame.QUIT):\n                    game_on = 0\n                    pygame.quit()\n                if event.type == pygame.KEYDOWN:\n                    if event.key == pygame.K_ESCAPE:\n                        pygame.quit()\n                        game_on = 0\n            if game_on:\n                buttons.update()\n                buttons.draw(screen)\n            else:\n                pygame.quit()\n                sys.exit()\n            buttons.draw(screen)\n            clock.tick(60)\n            pygame.display.update()\n        pygame.quit()\n\n\n\n\n    loop()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/replit.com\/join\/qkuojrzriv-educationalchan\">https:\/\/replit.com\/join\/qkuojrzriv-educationalchan<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/10\/image-20.png\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"404\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/10\/image-20.png\" alt=\"\" class=\"wp-image-10647\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/10\/image-20.png 604w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/10\/image-20-320x214.png 320w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can see a more mature version of quizpy here<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/replit.com\/@EducationalChan\/pyquiz\">https:\/\/replit.com\/@EducationalChan\/pyquiz<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\nimport pygame.gfxdraw\nimport sys\nimport time\nimport random\n# the Label class is this module below\nfrom label import *\n\n\npygame.init()\npygame.mixer.init()\nhit = pygame.mixer.Sound(\"sounds\/hit.wav\")\nscreen = pygame.display.set_mode((1200, 1200))\nclock = pygame.time.Clock()\n\n\n\nbuttons = pygame.sprite.Group()\nclass Button(pygame.sprite.Sprite):\n    ''' A button treated like a Sprite... and killed too '''\n    \n    def __init__(self, position, text, size,\n        colors=\"white on blue\",\n        hover_colors=\"red on green\",\n        style=\"button1\",\n        borderc=(255,255,255),\n        command=lambda: print(\"No command activated for this button\")):\n\n        # the hover_colors attribute needs to be fixed\n        super().__init__()\n        global num\n\n\n        self.text = text\n\n        self.command = command\n        # --- colors ---\n        self.colors = colors\n        self.original_colors = colors\n        self.fg, self.bg = self.colors.split(\" on \")\n\n        if hover_colors == \"red on green\":\n            self.hover_colors = f\"{self.bg} on {self.fg}\"\n        else:\n            self.hover_colors = hover_colors\n\n        self.style = style\n        self.borderc = borderc # for the style2\n        # font\n        self.font = pygame.font.SysFont(\"Arial\", size)\n        self.render(self.text)\n        self.x, self.y, self.w , self.h = self.text_render.get_rect()\n        self.x, self.y = position\n        self.rect = pygame.Rect(self.x, self.y, 500, self.h)\n        self.position = position\n        self.pressed = 1\n        # the groups with all the buttons\n        buttons.add(self)\n\n    def render(self, text):\n        # we have a surface\n        self.text_render = self.font.render(text, 1, self.fg)\n        # memorize the surface in the image attributes\n        self.image = self.text_render\n\n    def update(self):\n        self.fg, self.bg = self.colors.split(\" on \")\n        if self.style == \"button1\":\n            self.draw_button1()\n        elif self.style == \"button2\":\n            self.draw_button2()\n        if self.command != None:\n            self.hover()\n            self.click()\n\n    def draw_button1(self):\n        ''' draws 4 lines around the button and the background '''\n        # horizontal up\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y), (self.x + self.w , self.y), 5)\n        pygame.draw.line(screen, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y + self.h), 5)\n        # horizontal down\n        pygame.draw.line(screen, (50, 50, 50), (self.x, self.y + self.h), (self.x + self.w , self.y + self.h), 5)\n        pygame.draw.line(screen, (50, 50, 50), (self.x + self.w , self.y + self.h), [self.x + self.w , self.y], 5)\n        # background of the button\n        pygame.draw.rect(screen, self.bg, (self.x, self.y, self.w , self.h))  \n\n    def draw_button2(self):\n        ''' a linear border '''\n        pygame.draw.rect(screen, self.bg, (self.x - 50, self.y, 500 , self.h))\n        pygame.gfxdraw.rectangle(screen, (self.x - 50, self.y, 500 , self.h), self.borderc)\n\n    def check_collision(self):\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            # you can change the colors when the pointer is on the button if you want\n            self.colors = self.hover_colors\n            # pygame.mouse.set_cursor(*pygame.cursors.diamond)\n        else:\n            self.colors = self.original_colors\n            # pygame.mouse.set_cursor(*pygame.cursors.arrow)\n\n\n    def hover(self):\n        ''' checks if the mouse is over the button and changes the color if it is true '''\n        self.check_collision()\n\n    def click(self):\n        ''' checks if you click on the button and makes the call to the action just one time'''\n        if self.rect.collidepoint(pygame.mouse.get_pos()):\n            if pygame.mouse.get_pressed()[0] and self.pressed == 1:\n                print(\"The answer is:'\" + self.text + \"'\")\n                self.command()\n                self.pressed = 0\n\n            if pygame.mouse.get_pressed() == (0,0,0):\n                self.pressed = 1\n\n\n\n# ACTION FOR BUTTON CLICK ================\n\ndef on_click():\n    print(\"Click on one answer\")\n\ndef on_right():\n    check_score(\"right\")\n\ndef on_false():\n    ''' if there is no 'right' as arg it means it's false '''\n    check_score()\n\ndef check_score(answered=\"wrong\"):\n    ''' here we check if the answer is right '''\n    global qnum, points\n    \n    # until there are questions (before last)\n    hit.play() # click sound\n    if qnum &lt; len(questions):\n        print(qnum, len(questions))\n        if answered == \"right\":\n            time.sleep(.1) # to avoid adding more point when pressing too much\n            points += 1\n            # Show the score text\n        qnum += 1 # counter for next question in the list\n        score.change_text(str(points))\n        # Change the text of the question\n        title.change_text(questions[qnum-1][0], color=\"white\")\n        # change the question number\n        num_question.change_text(str(qnum))\n        show_question(qnum) # delete old buttons and show new\n        \n\n    # for the last question...\n    elif qnum == len(questions):\n        print(qnum, len(questions))\n        if answered == \"right\":\n            kill()\n            time.sleep(.1)\n            points +=1\n        score.change_text(\"Seu record \u00e9 de \" + str(points))\n    time.sleep(.5)\n\n\n# the first answer is right: it will be shuffled, don't worry\n# ============================================================\nquestions = [\n    [\"What is the correct file extension for Python files?\",\n      [\".py\", \".pt\", \".ppt\", \".pdf\"]],\n    [\"Which method can be used to remove any whitespace from both the beginning and the end of a string?\",\n      [\"strip()\", \"trip()\", \"trim()\", \"len()\"]],\n    [\"Which operator is used to multiply numbers?\",\n      [\"*\", \"+\", \"-\", \"\/\"]],\n]\n# =========== END OF QUESTIONS AND ANSWERS =================\n\n\n\n\n\n\ndef show_question(qnum):\n    ''' Show questions: '''\n\n    pos = [180,218,256,294] #\n    kill() # Kills the previous buttons\/sprites\n    def numbers():\n      ''' inner function: THE NUMBERS OF THE QUESTION IN ORDER 1 2 3 4 '''\n      \n      # position of the numbers\n      for n in range(4):\n        Button((10, pos[n]),\n        f\"{n+1} \",\n        36,\n        \"darkred on yellow\",\n        hover_colors=\"darkred on orange\",\n        style=\"button2\",\n        borderc=(255,255,0),\n        command=None)\n      \n    def questions_shuffler():\n      # show numbers and answers text\n      # ============== TEXT: question and answers ====================\n      comm =[on_right, on_false, on_false, on_false]\n      for n in range(4):\n        pass\n        Button(\n          (50, pos[n]),\n          questions[qnum-1][1][n],\n          36,\n          \"blue on yellow\",\n          hover_colors=\"blue on orange\",\n          style=\"button2\",\n          borderc=(255,255,0),\n          command=comm[n])\n\n    numbers()\n    random.shuffle(pos) # randomized, so that the right one is not on top\n    questions_shuffler()\n\ndef kill():\n  ''' delete buttons when go to the next question '''\n  for _ in buttons:\n      _.kill()\n\nqnum = 1\npoints = 0\n# ================= SOME LABELS ==========================\nnum_question = Label(screen, str(qnum), 0, 0)\ntitle = Label(screen, questions[qnum-1][0], 10, 10, 50, color=\"cyan\")\nscore = Label(screen, \"AV2 Programming\", 50, 335)\nwrite1 = Label(screen, \"Maria Eduarda, Joana, Rafaela, Rafael\", 50, 360, 20, color=\"white\")\n\ndef start_again():\n    pass\n\ndef loop():\n    global game_on\n\n    show_question(qnum)\n\n    while True:\n        screen.fill(0)\n        for event in pygame.event.get(): # ====== quit \/ exit\n            if (event.type == pygame.QUIT):\n                pygame.quit()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_ESCAPE:\n                    pygame.quit()\n        buttons.update() #                     update buttons\n        buttons.draw(screen)\n        show_labels()        \n        # update labels\n        if points == 3:\n          winimg = pygame.image.load(\"python_pygame.png\")\n          screen.blit(winimg, (100, 100))\n        clock.tick(60)\n        pygame.display.update()\n    pygame.quit()\n\nif __name__ == '__main__':\n    pygame.init()\n    game_on = 1\n    loop()<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-17.png\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"388\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-17.png\" alt=\"\" class=\"wp-image-11453\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-17.png 666w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/02\/image-17-320x186.png 320w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/a><figcaption class=\"wp-element-caption\">the GUI on repl.it of the code<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"even-nicer-button-with-animation\">Even nicer button with animation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This buttons are nice, they have animation. I made some changes to someone else&#8217;s code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame, sys\n\nbuttons = []\nclass Button:\n\tdef __init__(self,text,width,height,pos,elevation):\n\t\t#Core attributes \n\t\tself.pressed = False\n\t\tself.elevation = elevation\n\t\tself.dynamic_elecation = elevation\n\t\tself.original_y_pos = pos[1]\n\n\t\t# top rectangle \n\t\tself.top_rect = pygame.Rect(pos,(width,height))\n\t\tself.top_color = '#475F77'\n\n\t\t# bottom rectangle \n\t\tself.bottom_rect = pygame.Rect(pos,(width,height))\n\t\tself.bottom_color = '#354B5E'\n\t\t#text\n\t\tself.text = text\n\t\tself.text_surf = gui_font.render(text,True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\t\tbuttons.append(self)\n\n\tdef change_text(self, newtext):\n\t\tself.text_surf = gui_font.render(newtext, True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\n\tdef draw(self):\n\t\t# elevation logic \n\t\tself.top_rect.y = self.original_y_pos - self.dynamic_elecation\n\t\tself.text_rect.center = self.top_rect.center \n\n\t\tself.bottom_rect.midtop = self.top_rect.midtop\n\t\tself.bottom_rect.height = self.top_rect.height + self.dynamic_elecation\n\n\t\tpygame.draw.rect(screen,self.bottom_color, self.bottom_rect,border_radius = 12)\n\t\tpygame.draw.rect(screen,self.top_color, self.top_rect,border_radius = 12)\n\t\tscreen.blit(self.text_surf, self.text_rect)\n\t\tself.check_click()\n\n\tdef check_click(self):\n\t\tmouse_pos = pygame.mouse.get_pos()\n\t\tif self.top_rect.collidepoint(mouse_pos):\n\t\t\tself.top_color = '#D74B4B'\n\t\t\tif pygame.mouse.get_pressed()[0]:\n\t\t\t\tself.dynamic_elecation = 0\n\t\t\t\tself.pressed = True\n\t\t\t\tself.change_text(f\"{self.text}\")\n\t\t\telse:\n\t\t\t\tself.dynamic_elecation = self.elevation\n\t\t\t\tif self.pressed == True:\n\t\t\t\t\tprint('click')\n\t\t\t\t\tself.pressed = False\n\t\t\t\t\tself.change_text(self.text)\n\t\telse:\n\t\t\tself.dynamic_elecation = self.elevation\n\t\t\tself.top_color = '#475F77'\n\npygame.init()\nscreen = pygame.display.set_mode((500,500))\npygame.display.set_caption('Gui Menu')\nclock = pygame.time.Clock()\ngui_font = pygame.font.Font(None,30)\n\nbutton1 = Button('Rome',200,40,(100,200),5)\nbutton2 = Button('Milan',200,40,(100,250),5)\nbutton3 = Button('Neaples',200,40,(100,300),5)\n\n\ndef buttons_draw():\n\tfor b in buttons:\n\t\tb.draw()\n\nwhile True:\n\tfor event in pygame.event.get():\n\t\tif event.type == pygame.QUIT:\n\t\t\tpygame.quit()\n\t\t\tsys.exit()\n\n\tscreen.fill('#DCDDD8')\n\tbuttons_draw()\n\n\tpygame.display.update()\n\tclock.tick(60)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/12\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"541\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/12\/image-7.png\" alt=\"\" class=\"wp-image-11007\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/12\/image-7.png 513w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/12\/image-7-320x337.png 320w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/a><figcaption class=\"wp-element-caption\">Nice buttons with animation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"change-text-when-click\">Change text when click<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame, sys\n\nbuttons = []\nclass Button:\n\tdef __init__(self,text,translation,width,height,pos,elevation):\n\t\t#Core attributes \n\t\tself.pressed = False\n\t\tself.elevation = elevation\n\t\tself.dynamic_elecation = elevation\n\t\tself.original_y_pos = pos[1]\n\n\t\t# top rectangle \n\t\tself.top_rect = pygame.Rect(pos,(width,height))\n\t\tself.top_color = '#475F77'\n\t\tself.translation = translation\n\t\t# bottom rectangle \n\t\tself.bottom_rect = pygame.Rect(pos,(width,height))\n\t\tself.bottom_color = '#354B5E'\n\t\t#text\n\t\tself.text = text\n\t\tself.text_surf = gui_font.render(text,True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\t\tbuttons.append(self)\n\n\tdef change_text(self, newtext):\n\t\tself.text_surf = gui_font.render(newtext, True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\n\tdef draw(self):\n\t\t# elevation logic \n\t\tself.top_rect.y = self.original_y_pos - self.dynamic_elecation\n\t\tself.text_rect.center = self.top_rect.center \n\n\t\tself.bottom_rect.midtop = self.top_rect.midtop\n\t\tself.bottom_rect.height = self.top_rect.height + self.dynamic_elecation\n\n\t\tpygame.draw.rect(screen,self.bottom_color, self.bottom_rect,border_radius = 12)\n\t\tpygame.draw.rect(screen,self.top_color, self.top_rect,border_radius = 12)\n\t\tscreen.blit(self.text_surf, self.text_rect)\n\t\tself.check_click()\n\n\tdef check_click(self):\n\t\tmouse_pos = pygame.mouse.get_pos()\n\t\tif self.top_rect.collidepoint(mouse_pos):\n\t\t\tself.top_color = '#D74B4B'\n\t\t\tif pygame.mouse.get_pressed()[0]:\n\t\t\t\tself.dynamic_elecation = 0\n\t\t\t\tself.pressed = True\n\t\t\t\tself.change_text(f\"{self.translation}\")\n\t\t\telse:\n\t\t\t\tself.dynamic_elecation = self.elevation\n\t\t\t\tif self.pressed == True:\n\t\t\t\t\tprint('click')\n\t\t\t\t\tself.pressed = False\n\t\t\t\t\tself.change_text(self.text)\n\t\telse:\n\t\t\tself.dynamic_elecation = self.elevation\n\t\t\tself.top_color = '#475F77'\n\npygame.init()\nscreen = pygame.display.set_mode((500,500))\npygame.display.set_caption('Gui Menu')\nclock = pygame.time.Clock()\ngui_font = pygame.font.Font(None,30)\n\nbutton1 = Button('Rome', \"Roma\",200,40,(100,200),5)\nbutton2 = Button('Milan', \"Milano\",200,40,(100,250),5)\nbutton3 = Button('Neaples',\"Napoli\",200,40,(100,300),5)\nbutton3 = Button('Florence',\"Firenze\",200,40,(100,350),5)\nbutton3 = Button('Venice',\"Venezia\",200,40,(100,400),5)\n\n\ndef buttons_draw():\n\tfor b in buttons:\n\t\tb.draw()\n\nwhile True:\n\tfor event in pygame.event.get():\n\t\tif event.type == pygame.QUIT:\n\t\t\tpygame.quit()\n\t\t\tsys.exit()\n\n\tscreen.fill('#DCDDD8')\n\tbuttons_draw()\n\n\tpygame.display.update()\n\tclock.tick(60)<\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"640\" style=\"aspect-ratio: 640 \/ 640;\" width=\"640\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/12\/Gui-Menu-2021-12-08-23-38-10-1.mp4\"><\/video><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Make something different for every click of the different buttons<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ok, what you want to do is to make different things depending on what button you press. So here is a solution, to add another parameter to the istance of a button that refers to a function that does what you want that the button do when is clicked. Here is the code. If you do not assign a function to the button, there will be a warning, telling you how to do it.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame, sys\n\n\n\ndef help_empty_command():\n\t''' function for buttons without command argument '''\n\tprint(f\"\"\"[WARNING]:You have not assigned a function to\n\t \t\tyour button. Create a function called command2, for\n\t  \t\texample and add command2 as last parameter of the istance\n\t   \t\tof this buttonex:\n\t   \t\tbutton1 = Button('Rome',200,40,(100,200),5, command1))\"\"\")\n\nbuttons = []\nclass Button:\n\tdef __init__(self,text,width,height,pos,elevation, command=help_empty_command):\n\t\t#Core attributes \n\t\tself.pressed = False\n\t\tself.elevation = elevation\n\t\tself.dynamic_elecation = elevation\n\t\tself.original_y_pos = pos[1]\n\t\tself.command = command\n\n\t\t# top rectangle \n\t\tself.top_rect = pygame.Rect(pos,(width,height))\n\t\tself.top_color = '#475F77'\n\n\t\t# bottom rectangle \n\t\tself.bottom_rect = pygame.Rect(pos,(width,height))\n\t\tself.bottom_color = '#354B5E'\n\t\t#text\n\t\tself.text = text\n\t\tself.text_surf = gui_font.render(text,True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\t\tbuttons.append(self)\n\n\tdef change_text(self, newtext):\n\t\tself.text_surf = gui_font.render(newtext, True,'#FFFFFF')\n\t\tself.text_rect = self.text_surf.get_rect(center = self.top_rect.center)\n\n\tdef draw(self):\n\t\t# elevation logic \n\t\tself.top_rect.y = self.original_y_pos - self.dynamic_elecation\n\t\tself.text_rect.center = self.top_rect.center \n\n\t\tself.bottom_rect.midtop = self.top_rect.midtop\n\t\tself.bottom_rect.height = self.top_rect.height + self.dynamic_elecation\n\n\t\tpygame.draw.rect(screen,self.bottom_color, self.bottom_rect,border_radius = 12)\n\t\tpygame.draw.rect(screen,self.top_color, self.top_rect,border_radius = 12)\n\t\tscreen.blit(self.text_surf, self.text_rect)\n\t\tself.check_click()\n\n\tdef check_click(self):\n\t\tmouse_pos = pygame.mouse.get_pos()\n\t\tif self.top_rect.collidepoint(mouse_pos):\n\t\t\tself.top_color = '#D74B4B'\n\t\t\tif pygame.mouse.get_pressed()[0]:\n\t\t\t\tself.dynamic_elecation = 0\n\t\t\t\tself.pressed = True\n\t\t\t\tself.change_text(f\"{self.text}\")\n\t\t\telse:\n\t\t\t\tself.dynamic_elecation = self.elevation\n\t\t\t\tif self.pressed == True:\n\t\t\t\t\tself.command()\n\t\t\t\t\tself.pressed = False\n\t\t\t\t\tself.change_text(self.text)\n\t\telse:\n\t\t\tself.dynamic_elecation = self.elevation\n\t\t\tself.top_color = '#475F77'\n\n\n\ndef command1():\n\tprint(\"I am the first button command to be executed only when you press it. I ca do all the code is in the command1 function. So I can evolve, thanks to my programmer. Thanks.\")\n\n\npygame.init()\nscreen = pygame.display.set_mode((500,500))\npygame.display.set_caption('Gui Menu')\nclock = pygame.time.Clock()\ngui_font = pygame.font.Font(None,30)\n\nbutton1 = Button('Rome',200,40,(100,200),5, command1)\nbutton2 = Button('Milan',200,40,(100,250),5)\nbutton3 = Button('Neaples',200,40,(100,300),5)\n\n\ndef buttons_draw():\n\tfor b in buttons:\n\t\tb.draw()\n\nwhile True:\n\tfor event in pygame.event.get():\n\t\tif event.type == pygame.QUIT:\n\t\t\tpygame.quit()\n\t\t\tsys.exit()\n\n\tscreen.fill('#DCDDD8')\n\tbuttons_draw()\n\n\tpygame.display.update()\n\tclock.tick(60)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we can say that the button widget is fully usable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><!-- se vuoi mettere un testo scorrevole\r\n[hoops name=\"typeWriterGen\"]\r\n\r\npoi metti un id diverso per ogni testo nella stessa pagina\r\n\r\n<div id=\"div01\">\r\n<script>\r\n\r\ntypeWriterGen(\"div01\",\"Esempio di testo scorrevole\");\r\n<\/script>\r\n\r\n-->\r\n<style>\r\n.avatar {\r\n  vertical-align: middle;\r\n  width: 100px;\r\n  height: 100px;\r\n  border-radius: 50%;\r\n}\r\n<\/style>\r\n\r\n<hr>\r\n\r\n<!-- NEWSLETTER LINK -->\r\n<a href=\"https:\/\/docs.google.com\/forms\/d\/e\/1FAIpQLSf7TniIPCWHDzCSGh2dYZaCwDvi9yLKS5ovFdKuK1sdfOvwEg\/viewform\">\r\n<img decoding=\"async\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/08\/image-13.png\" class=\"avatar\">\r\nSubscribe to the <b>newsletter<\/b> for updates<\/a><br>\r\n\r\n<!-- TKINTER TEMPLATE LINK -->\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-templates\/\">\r\n<img decoding=\"async\" src=\"https:\/\/i0.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/07\/image-26.png\" class=\"avatar\">\r\nTkinter templates<\/a><br>\r\n\r\n<!-- MY AVATAR PUT A LINK TO YOUTUBE CHANNEL-->\r\n<iframe loading=\"lazy\" frameborder=\"0\" src=\"https:\/\/itch.io\/embed\/711828\" width=\"552\" height=\"167\"><a href=\"https:\/\/pythonprogrammi.itch.io\/pysnake\">PySnake by PythonProgrammi<\/a><\/iframe>\r\n<br>\r\n<style>\r\n.avatar {\r\n  vertical-align: middle;\r\n  width: 100px;\r\n  height: 100px;\r\n  border-radius: 50%;\r\n}\r\n<\/style>\r\n\r\n\r\n<a href=\"https:\/\/www.youtube.com\/channel\/UCzbxq5e9gLiY-je2-br1rvg\">\r\n\t<img decoding=\"async\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/10\/avatar64x64.png\" alt=\"Avatar\" class=\"avatar\">\r\n\t My youtube channel<\/a><br>\r\n\r\n<br>\r\n\r\nTwitter: <a href=\"https:\/\/twitter.com\/pythonprogrammi\">@pythonprogrammi - python_pygame<\/a>\r\n<h3>Claude's Games<\/h3>\r\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/random-daily-game-1-arkanoid\/\">Arkanoid<\/a><br>\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/platform-2d-with-pygame-made-with-claude\/\">Platform 2d<\/a><\/p> <!-- videogames made with claude -->\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/artifacts-games-day-1-memory-game\/\">1. Memory game<\/a>\r\n<h4>Videos<\/h4>\r\n<a href=\"https:\/\/youtu.be\/ciLjWWw5pLY\">Speech recognition game<\/a>\r\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><\/p>\n","protected":false},"excerpt":{"rendered":"How to make a button in pygame.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/buttons-in-pygame\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":7226,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[191],"tags":[],"class_list":["post-7225","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pygame"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"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\/7225","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=7225"}],"version-history":[{"count":9,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/7225\/revisions"}],"predecessor-version":[{"id":13625,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/7225\/revisions\/13625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/7226"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=7225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=7225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=7225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}