Как создать кнопку в pygame

от admin

How to make buttons in python/pygame?

I’m making a game in pygame and on the first screen I want there to be buttons that you can press to (i) start the game, (ii) load a new screen with instructions, and (iii) exit the program.

I’ve found this code online for button making, but I don’t really understand it (I’m not that good at object oriented programming). If I could get some explanation as to what it’s doing that would be great. Also, when I use it and try to open a file on my computer using the file path, I get the error sh: filepath :Permission denied, which I don’t know how to solve.

Thank you to anyone who can help me.

6 Answers 6

I don’t have a code example for you, but how I would do it is to:

  1. Make a Button class, with the text to go on the button as a constructor argument
    1. Create a PyGame surface, either of an image or filled Rect
    2. Render text on it with the Font.Render stuff in Pygame

    That is similar to what your example is doing, although different still.

    Another good way to create buttons on pygame (in Python) is by installing the package called pygame_widgets ( pip3 install pygame_widgets ).

    The ‘code’ you have found online is not that good. All you need to make a button is this. Put this near the beginning of your code:

    Put the following in your game loop. Also somewhere in your game loop:

    Also put this in your game loop wherever you have done for event in pygame.event.get

    So, buttonify loads the image that will be on the button. This image must be a .jpg file or any other PICTURE file in the same directory as the code. Picture is its name. The name must have .jpg or anything else after it and the name must be in quotation marks. The coords parameter in Buttonify is the top-right coordinate on your screen or window that opens from pygame. The surface is this thing:

    So it the function makes something called ‘image’ which is a pygame surface, it puts a rectangle around it called ‘imagerect’ (to set it at a location and for the second parameter when blitting,) and then it sets it’s location, and blits it on the second to last last line.

    The next bit of code makes ‘Image’ a tuple of both ‘image’ and ‘imagerect.’

    Creating Buttons in Pygame

    report this ad Ezoic report this ad Ezoic report this ad

    This tutorial explains how to create Buttons in Python Pygame.

    Pygame doesn’t actually have some kind of button widget that you may expect to see in a GUI library like Tkinter or PyQt5. However, using a combination of various features, we can put together something just as good.

    Creating Buttons

    The first thing to do is to create the actual appearance of the button. This includes the text, color and shape of the button. Luckily we can achieve all of this within a single line by using the draw.rect() function.

    It takes as first argument, the window that we created (displaysurface), followed by the color (which we created previously as a tuple). Lastly we create a list with 4 values in the following order, x-coordinate , y-coordinate , width and height .

    You can place this line of code anywhere, inside the game loop or out of it. Keeping within the game loop will render it with every iteration of the loop. Whereas keeping it out will render it only once (saving resources). It’s upto you where to keep it, based off your requirements.

    Adding Text to Buttons

    Keep in mind that the above code just creates a colored rectangle which barely qualifies as a button. In order to complete the button, we should add some text to it first.

    If you have any trouble understanding the way we created text here, check out the Pygame Fonts and Text tutorial.

    With the addition of these two lines right after the draw.rect() code, you can render the text “LOAD” over the button. (You can move the text render function out of the game loop as it only needs to generate once)

    This what the button we created looks like.

    Making Buttons Interactable

    What we’re going to do is return the position of the mouse in every cycle of the game and determine if the mouse has been pressed (clicked) or not.

    The above code is a line that must be added into the game loop to return the updated position of the mouse on every frame.

    Using the mouse position, we will determine whether it is hovering over the button we created or not. If it is, we will also check to see if the mouse has been clicked. If both statements are true, we will activate the function that button is meant to be linked to.

    (In this case, the code which will execute when the button is clicked, is in the load() function of an event handler object)

    You may find this confusing, so go back to look where we drew our button. Then come back here and look at the coordinates range given here. You should realize that the button falls within this range.

    Note: It’s important to remember the logic behind the creating of these buttons in pygame. It’s not that clicking that text or colored box will activate the function. It’s the act of clicking that specific area on the screen that does it. Even if you were to remove the text and color, that area of the screen would still be clickable.

    However, we can change this behavior slightly by instead comparing collisions between the mouse cursor and the button rect. In theory it’s the same concept, but it’s a easier to manage and scale up for many buttons.

    This marks the end of the Creating Buttons in Pygame tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below,

    Ezoic

    report this ad

    Создание игр на Python 3 и Pygame: Часть 4

    Это четвёртая из пяти частей туториала, посвящённого созданию игр с помощью Python 3 и Pygame. В третьей части мы углубились в сердце Breakout и узнали, как обрабатывать события, познакомились с основным классом Breakout и увидели, как перемещать разные игровые объекты.

    В этой части мы узнаем, как распознавать коллизии и что случается, когда мяч ударяется об разные объекты: ракетку, кирпичи, стены, потолок и пол. Наконец, мы рассмотрим важную тему пользовательского интерфейса и в частности то, как создать меню из собственных кнопок.

    Распознавание коллизий

    В играх объекты сталкиваются друг с другом, и Breakout не является исключением. В основном с объектами сталкивается мяч. В методе handle_ball_collisions() есть встроенная функция под названием intersect() , которая используется для проверки того, ударился ли мяч об объект, и того, где он столкнулся с объектом. Она возвращает ‘left’, ‘right’, ‘top’, ‘bottom’ или None, если мяч не столкнулся с объектом.

    Столкновение мяча с ракеткой

    Когда мяч стукается об ракетку, он отскакивает. Если он ударяется о верхнюю часть ракетки, то отражается обратно вверх, но сохраняет тот же компонент горизонтальной скорости.

    Но если он ударяется о боковую часть ракетки, то отскакивает в противоположную сторону (влево или вправо) и продолжает движение вниз, пока не столкнётся с полом. В коде используется функция intersect() .

    Столкновение с полом

    Когда ракетка пропускает мяч на пути вниз (или мяч ударяется об ракетку сбоку), то мяч продолжает падать и затем ударяется об пол. В этот момент игрок теряет жизнь и мяч создаётся заново, чтобы игра могла продолжаться. Игра завершается, когда у игрока заканчиваются жизни.

    Столкновение с потолком и стенами

    Когда мяч ударяется об стены или потолок, он просто отскакивает от них.

    Столкновение с кирпичами

    Когда мяч ударяется об кирпич, это является основным событием игры Breakout — кирпич исчезает, игрок получает очко, мяч отражается назад и происходят ещё несколько событий (звуковой эффект, а иногда и спецэффект), которые я рассмотрю позже.

    Чтобы определить, что мяч ударился об кирпич, код проверят, пересекается ли какой-нибудь из кирпичей с мячом:

    Программирование игрового меню

    В большинстве игр есть какой-нибудь UI. В Breakout есть простое меню с двумя кнопками, ‘PLAY’ и ‘QUIT’. Меню отображается в начале игры и пропадает, когда игрок нажимает на ‘PLAY’. Давайте посмотрим, как реализуются кнопки и меню, а также как они интегрируются в игру.

    Создание кнопок

    В Pygame нет встроенной библиотеки UI. Есть сторонние расширения, но для меню я решил создать свои кнопки. Кнопка — это игровой объект, имеющий три состояния: нормальное, выделенное и нажатое. Нормальное состояние — это когда мышь не находится над кнопкой, а выделенное состояние — когда мышь находится над кнопкой, но левая кнопка мыши ещё не нажата. Нажатое состояние — это когда мышь находится над кнопкой и игрок нажал на левую кнопку мыши.

    Кнопка реализуется как прямоугольник с фоновым цветом и текст, отображаемый поверх него. Также кнопка получает функцию on_click (по умолчанию являющуюся пустой лямбда-функцией), которая вызывается при нажатии кнопки.

    Кнопка обрабатывает собственные события мыши и изменяет своё внутреннее состояние на основании этих событий. Когда кнопка находится в нажатом состоянии и получает событие MOUSEBUTTONUP , это означает, что игрок нажал на кнопку, и вызывается функция on_click() .

    Свойство back_color , используемое для отрисовки фонового прямоугольника, всегда возвращает цвет, соответствующий текущему состоянию кнопки, чтобы игроку было ясно, что кнопка активна:

    Создание меню

    Функция create_menu() создаёт меню с двумя кнопками с текстом ‘PLAY’ и ‘QUIT’. Она имеет две встроенные функции, on_play() и on_quit() , которые она передаёт соответствующей кнопке. Каждая кнопка добавляется в список objects (для отрисовки), а также в поле menu_buttons .

    При нажатии кнопки PLAY вызывается функция on_play() , удаляющая кнопки из списка objects , чтобы они больше не отрисовывались. Кроме того, значения булевых полей, которые запускают начало игры — is_game_running и start_level — становятся равными True.

    При нажатии кнопки QUIT is_game_running принимает значение False (фактически ставя игру на паузу), а game_over присваивается значение True, что приводит к срабатыванию последовательности завершения игры.

    Отображение и сокрытие игрового меню

    Отображение и сокрытие меню выполняются неявным образом. Когда кнопки находятся в списке objects , меню видимо; когда они удаляются, оно скрывается. Всё очень просто.

    Можно создать встроенное меню с собственной поверхностью, которое рендерит свои подкомпоненты (кнопки и другие объекты), а затем просто добавлять/удалять эти компоненты меню, но для такого простого меню это не требуется.

    Подводим итог

    В этой части мы рассмотрели распознавание коллизий и то, что происходит, когда мяч сталкивается с разными объектами: ракеткой, кирпичами, стенами, полом и потолком. Также мы создали меню с собственными кнопками, которое можно скрывать и отображать по команде.

    В последней части серии мы рассмотрим завершение игры, отслеживание очков и жизней, звуковые эффекты и музыку.

    Затем мы разработаем сложную систему спецэффектов, добавляющих в игру немного специй. Наконец, мы обсудим дальнейшее развитие и возможные улучшения.

    PyGame Buttons and Mouse Movement

    In this section we will look at how you can utilise the mouse in your games and create buttons to craft more intuitive interfaces and game interactions.

    Getting Set Up

    Tom, our heroic character, standingBefore we begin, let’s create a new file (call it mouse.py) and copy in the template code from the previous section

    Once you’ve saved your file with the template code in it, run the file to make sure you’ve copied it in ok.

    We will also need some images to play with. Create a directory called images in the same directory as your Python file and place these images into this directory. Right click and download these images.

      — our heroic hero.

    (Right click and «Save image as . «, or similar)

    It is possible to access and load the images from anywhere on the system but if you place them in the same location as the Python (preferably in a folder called «images» to keep things neat) file it makes things a little easier. It also makes things easier when you want to package up and share your game later on.

    We will place Tom in the middle of the window so that we can manipulate him via the mouse. Place the following code near the beginning of the main function :

    • looping = True
    • tom = pygame.image.load(‘images/tom_standing.png’).convert_alpha()
    • tom = pygame.transform.scale(tom, (50, 95))
    • tomX = WINDOW_WIDTH // 2 — 25
    • tomY = WINDOW_HEIGHT //2 — 48

    And then render Tom :

    • WINDOW.fill(BACKGROUND)
    • WINDOW.blit(tom, (tomX, tomY))
    • pygame.display.update()

    Tom in a windowIf you save and run the program you should get a window with Tom in the middle.

    Where is the cursor?

    Let’s start by figuring out the location of the mouse within the window. We can do this rather easily.

    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • mouse = pygame.mouse.get_pos()

    And now we will print the values out so we can see what they look like :

    • # Processing
    • print (mouse)

    This will print out a constant stream of values to the screen. Move the mouse around a bit and observe how the values change. See what happens if you move the cursor outside the window as well. Once you are done, you may comment out this line.

    The values are in what is called a tuple which is essentially a list (or array) in which the items cannot be changed once they are set. You access the items using numerical index (starting from 0) just the same as a list.

    If we wanted to print the values a bit more nicely we could replace the line we just put in with the following :

    • # Processing
    • print (‘X coordinate : ‘, mouse[0], ‘ Y coordinate : ‘, mouse[1])

    Reacting to Mouse Movement

    Now that we can identify the location of the cursor we can add some interactivity. Let’s make Tom chase the cursor.

    • # Processing
    • # print (‘X coordinate : ‘, mouse[0], ‘ Y coordinate : ‘, mouse[1])
    • if tomX mouse[0] :
    • tomX = tomX — 1
    • if tomY mouse[1] :
    • tomY = tomY — 1

    What we have done is comment out the line printing the coordinates (now that we know that is works we don’t need it any more). Then we added four if statements to check if Tom is above, below, to the left, or to the right of the cursor and to update his coordinates accordingly.

    Tom chasing the cursorTom now chases the cursor but there is a small flaw. If you move the mouse outside the window, Tom will keep moving to whereever that last location was. It would be nice if Tom stopped moving when there wasn’t a cursor to chase. We do this by checking if the mouse is focused on the window or not.

    • # Processing
    • # print (‘X coordinate : ‘, mouse[0], ‘ Y coordinate : ‘, mouse[1])
    • if pygame.mouse.get_focused() == 1 :
    • if tomX mouse[0] :
    • tomX = tomX — 1
    • if tomY mouse[1] :
    • tomY = tomY — 1

    Mouse Clicks

    Detecting mouse clicks is just as easy. Reacting to mouse clicks is a little bit trickier. It is possible to detect and respond to both the mouse button being pressed and the mouse button being released.

    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    • print (‘mouse button down’)
    • if event.type == pygame.MOUSEBUTTONUP and event.button == 1 :
    • print (‘mouse button up’)
    • mouse = pygame.mouse.get_pos()

    With this method we can detect both when the mouse button has been clicked and when it has been released. This can be useful to show when the button has been pressed down but only act when it has been released. Alternatively, we may want to perform an action for as long as the button is pressed and only stop when it is released.

    You’ll notice that at the end of the if statements we have event.button == 1. This indicates that it is the left mouse button we are looking at. If you change this to 2 it will instead look at the middle button (usually the scroll wheel) and 3 will make it look at the right mouse button.

    It is also possible to get the state of all buttons using the command pygame.mouse.get_pressed() however this will only tell us if the button is up or down at the time of calling. It doesn’t give us as fine grained an indication of the state of the buttons.

    • tom = pygame.transform.scale(tom, (50, 95))
    • tomX = WINDOW_WIDTH // 2 — 25
    • tomY = WINDOW_HEIGHT //2 — 48
    • mouseDown = False

    And change the statement within the if statement for the MOUSEBUTTONDOWN event so that instead of printing a message it sets the variable mouseDown to True. Also do the opposite when MOUSEBUTTONUP event is fired.

    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    • mouseDown = True
    • if event.type == pygame.MOUSEBUTTONUP and event.button == 1 :
    • mouseDown = False
    • mouse = pygame.mouse.get_pos()

    This first bit creates a variable that stores if the button is currently down or not. Next we will add a condition to the if statement around the movement code for Tom so that it only runs when this variable is False.

    • # Processing
    • # print (‘X coordinate : ‘, mouse[0], ‘ Y coordinate : ‘, mouse[1])
    • if pygame.mouse.get_focused() == 1 and mouseDown == False :
    • if tomX mouse[0] :
    • tomX = tomX — 1
    • if tomY mouse[1] :
    • tomY = tomY — 1

    If you save and run your code now you should see that Tom will chase your cursor but stop whenever you have your left mouse button pressed. Now le’t code in the next step which is when the button is released.

    • tom = pygame.transform.scale(tom, (50, 95))
    • tomX = WINDOW_WIDTH // 2 — 25
    • tomY = WINDOW_HEIGHT //2 — 48
    • mouseDown = False
    • mouseReleased = False
    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    • mouseDown = True
    • mouseReleased = False
    • if event.type == pygame.MOUSEBUTTONUP and event.button == 1 :
    • mouseDown = False
    • mouseReleased = True
    • mouse = pygame.mouse.get_pos()

    And another if statement to act upon when the mouse button is released.

    • if tomX mouse[0] :
    • tomX = tomX — 1
    • if tomY mouse[1] :
    • tomY = tomY — 1
    • if mouseReleased == True :
    • tomX = WINDOW_WIDTH // 2 — 25
    • tomY = WINDOW_HEIGHT //2 — 48
    • mouseReleased = False

    Now if you save and run your code, when you release the mouse button Tom should reset back to the middle of the window.

    Buttons

    Being able to detect mouse clicks gives us the final bit we need in order to make buttons within our games. I will demonstrate a simple working button here but utilising your knowledge of drawing and images you can easily take this further and create more elaborate buttons.

    • import pygame, sys, random
    • from pygame.locals import *
    • pygame.init()
    • # Colours
    • BACKGROUND = (255, 255, 255)
    • BUTTON_NORMAL = (255, 100, 100)
    • BUTTON_HOVER = (100, 255, 100)
    • BUTTON_CLICKED = (100, 100, 255)
    • # Game Setup
    • FPS = 60
    • fpsClock = pygame.time.Clock()
    • WINDOW_WIDTH = 400
    • WINDOW_HEIGHT = 300
    • WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    • pygame.display.set_caption(‘My Game!’)
    • # The main function that controls the game
    • def main () :
    • looping = True
    • button = pygame.Rect(50, 50, 200, 100)
    • mouseClicked = False
    • # The main game loop
    • while looping :
    • state = ‘normal’
    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • if event.type == pygame.MOUSEBUTTONDOWN :
    • mouseClicked = True
    • if event.type == pygame.MOUSEBUTTONUP :
    • mouseClicked = False
    • mouse = pygame.mouse.get_pos()
    • # Processing
    • # This section will be built out later
    • if button.collidepoint(mouse) and mouseClicked :
    • state = ‘clicked’
    • elif button.collidepoint(mouse) :
    • state = ‘hover’
    • # Render elements of the game
    • WINDOW.fill(BACKGROUND)
    • if state == ‘hover’ :
    • pygame.draw.rect(WINDOW, BUTTON_HOVER, button)
    • elif state == ‘clicked’ :
    • pygame.draw.rect(WINDOW, BUTTON_CLICKED, button)
    • else :
    • pygame.draw.rect(WINDOW, BUTTON_NORMAL, button)
    • pygame.display.update()
    • fpsClock.tick(FPS)
    • main()

    The button is just a rectangle that changes colour based upon the state (normal, hover, click) but it illustrates the mechanism.

    In Action

    Now let’s put this all together into a simple game. The game is going to either show the text ‘Left’ or ‘Right’ randomly on the screen and you have to react as fast as you can to click the opposite button.

    • import pygame, sys, random
    • from pygame.locals import *
    • pygame.init()
    • # Colours
    • BACKGROUND = (255, 255, 255)
    • BUTTON_NORMAL = (255, 100, 100)
    • BUTTON_HOVER = (100, 255, 100)
    • BUTTON_CLICKED = (100, 100, 255)
    • TEXTCOLOUR = (0, 0, 0)
    • # Game Setup
    • FPS = 60
    • fpsClock = pygame.time.Clock()
    • WINDOW_WIDTH = 400
    • WINDOW_HEIGHT = 300
    • WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    • pygame.display.set_caption(‘My Game!’)
    • # Fonts
    • fontObj = pygame.font.Font(None, 32)
    • startText = fontObj.render(‘Start’, True, TEXTCOLOUR, None)
    • leftText = fontObj.render(‘Left’, True, TEXTCOLOUR, None)
    • rightText = fontObj.render(‘Right’, True, TEXTCOLOUR, None)
    • # The main function that controls the game
    • def main () :
    • looping = True
    • gameState = ‘menu’
    • toHit = random.randint(0,1) # 0 = Left, 1 = Right
    • timer = 0
    • timeTaken = 0
    • timeToWait = 0
    • startButton = pygame.Rect(150, 40, 100, 50)
    • leftButton = pygame.Rect(50, 190, 100, 50)
    • rightButton = pygame.Rect(250, 190, 100, 50)
    • mouseClicked = False
    • # The main game loop
    • while looping :
    • startState = ‘normal’
    • leftState = ‘normal’
    • rightState = ‘normal’
    • # Get inputs
    • for event in pygame.event.get() :
    • if event.type == QUIT :
    • pygame.quit()
    • sys.exit()
    • if event.type == pygame.MOUSEBUTTONDOWN :
    • mouseClicked = True
    • if event.type == pygame.MOUSEBUTTONUP :
    • mouseClicked = False
    • mouse = pygame.mouse.get_pos()
    • # Processing
    • # This section will be built out later
    • if startButton.collidepoint(mouse) and mouseClicked :
    • startState = ‘clicked’
    • gameState = ‘inGame’
    • timer = time.time() + random.randint(2, 5)
    • elif startButton.collidepoint(mouse) :
    • startState = ‘hover’
    • if leftButton.collidepoint(mouse) and mouseClicked :
    • leftState = ‘clicked’
    • if gameState == ‘inGame’ and toHit == 0 and time.time() > timer:
    • timeTaken = time.time() — timer
    • print (f»Time Taken : «)
    • toHit = random.randint(0,1) # 0 = Left, 1 = Right
    • timer = time.time() + random.randint (2, 5)
    • elif leftButton.collidepoint(mouse) :
    • leftState = ‘hover’
    • if rightButton.collidepoint(mouse) and mouseClicked :
    • rightState = ‘clicked’
    • if gameState == ‘inGame’ and toHit == 1 and time.time() > timer:
    • timeTaken = time.time() — timer
    • print (f»Time Taken : «)
    • toHit = random.randint(0,1) # 0 = Left, 1 = Right
    • timer = time.time() + random.randint (2, 5)
    • elif rightButton.collidepoint(mouse) :
    • rightState = ‘hover’
    • # Render elements of the game
    • WINDOW.fill(BACKGROUND)
    • if startState == ‘hover’ and gameState == ‘menu’ :
    • pygame.draw.rect(WINDOW, BUTTON_HOVER, startButton)
    • WINDOW.blit(startText, (175, 55))
    • elif startState == ‘clicked’ and gameState == ‘menu’ :
    • pygame.draw.rect(WINDOW, BUTTON_CLICKED, startButton)
    • WINDOW.blit(startText, (175, 55))
    • elif startState == ‘normal’ and gameState == ‘menu’ :
    • pygame.draw.rect(WINDOW, BUTTON_NORMAL, startButton)
    • WINDOW.blit(startText, (175, 55))
    • if gameState == ‘inGame’ and time.time() > timer:
    • if toHit == 0 :
    • WINDOW.blit(leftText, (175, 65))
    • else :
    • WINDOW.blit(rightText, (175, 65))
    • if leftState == ‘hover’ :
    • pygame.draw.rect(WINDOW, BUTTON_HOVER, leftButton)
    • WINDOW.blit(leftText, (80, 205))
    • elif leftState == ‘clicked’ :
    • pygame.draw.rect(WINDOW, BUTTON_CLICKED, leftButton)
    • WINDOW.blit(leftText, (80, 205))
    • elif leftState == ‘normal’ :
    • pygame.draw.rect(WINDOW, BUTTON_NORMAL, leftButton)
    • WINDOW.blit(leftText, (80, 205))
    • if rightState == ‘hover’ :
    • pygame.draw.rect(WINDOW, BUTTON_HOVER, rightButton)
    • WINDOW.blit(rightText, (270, 205))
    • elif rightState == ‘clicked’ :
    • pygame.draw.rect(WINDOW, BUTTON_CLICKED, rightButton)
    • WINDOW.blit(rightText, (270, 205))
    • elif rightState == ‘normal’ :
    • pygame.draw.rect(WINDOW, BUTTON_NORMAL, rightButton)
    • WINDOW.blit(rightText, (270, 205))
    • pygame.display.update()
    • fpsClock.tick(FPS)
    • main()

    If you save and run the game you should have a simple reaction timer game.

    There is a lot of duplication of similar code in this program for the different buttons etc. Ideally functions and arrays should be used to make the code shorter and more efficient (as well as modular which makes it easier to modify and expand). I haven’t done so here simply to make it easier for newer coders to follow but a good challenge would be to refactor this code using functions.

    Activities

    Even though our program is fairly basic we can still tinker with a few elements to make sure we understand them.

    Have a go at the following :

    1. Can you improve the look of the buttons?

    2. Can you indicate the speed of reaction visually instead of printing it to the terminal?

    3. Can you make it so that the player has 10 goes then the start button reappears and an average reaction time is printed?

    4. Can you enhance the game by adding ‘Up’ and ‘Down’ to the available options?

    Читать:
    Как скачать ориджин на windows 10

Похожие статьи