Part 2: PySimpleGUI Arkanoid Rocket

Posted by


Welcome to part 2 of the PySimpleGUI tutorial series on creating an Arkanoid game! In this tutorial, we will focus on creating the paddle that the player will control to bounce the ball and prevent it from falling off the screen.

If you haven’t already, make sure you have PySimpleGUI installed on your system. You can install it by running the following command:

pip install PySimpleGUI

Now, let’s get started by first importing the necessary libraries:

import PySimpleGUI as sg
import random

Next, let’s set up the layout of our game window. We will create a window with a width and height of 600 pixels, and add a paddle at the bottom of the window. We will also set the initial position of the paddle to be in the center of the window.

layout = [
    [sg.Graph(canvas_size=(600, 400), graph_bottom_left=(0, 0), graph_top_right=(600, 400), background_color='black', key='graph')],
]

window = sg.Window('Arkanoid Part 2: Paddle', layout)

graph = window['graph']

paddle_width = 100
paddle_height = 10
paddle = graph.draw_rectangle((250, 380), (350, 390), line_color='white', fill_color='white')

Now, let’s create a function to move the paddle left and right. We will use the PySimpleGUI event loop to listen for key presses and move the paddle accordingly.

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == sg.WIN_CLOSED:
        break

    if event == 'Left:37':
        if graph.get_bounding_box(paddle)[0][0] - 10 >= 0:
            graph.move_figure(paddle, -10, 0)

    if event == 'Right:39':
        if graph.get_bounding_box(paddle)[1][0] + 10 <= 600:
            graph.move_figure(paddle, 10, 0)

Finally, let’s add a bit of styling to our game window. We will set the title bar font and the background color of the window.

window.TKroot.resizable(False, False)
window.TKroot.config(bg='black')
window.TKroot.title("Arkanoid Part 2: Paddle")
window.TKroot.attributes('-topmost', True)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Left:37':
        if graph.get_bounding_box(paddle)[0][0] - 10 >= 0:
            graph.move_figure(paddle, -10, 0)

    if event == 'Right:39':
        if graph.get_bounding_box(paddle)[1][0] + 10 <= 600:
            graph.move_figure(paddle, 10, 0)

window.close()

That’s it for part 2 of the PySimpleGUI tutorial series on creating an Arkanoid game! In this tutorial, we focused on creating the paddle that the player will control. Stay tuned for part 3 where we will add the ball and collision detection logic. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@nicko6815
3 months ago

Благодарю Вас! Очень интересно!