SoFunction
Updated on 2024-11-18

pygame implementation of slider catch ball game

Make a slider catching a small ball game with pygame for your reference as follows

Let's start with a picture.

The game is very simple and very retarded, the main use of pygame drawing circles, drawing squares, random numbers, etc., you can exercise the basic mouse control, game design thinking, simple collision judgment, etc., not much to say, on the code!

Before writing, think about what parameters can be used

()
screen = .set_mode((800, 600))
# Life and score
lives = 3
score = 0
# Set the color
white = 255, 255, 255
yellow = 255, 255, 0
black = 0, 0, 0
red = 220, 50, 50
# Set the font
font = (None, 38)
.set_visible(False)
game_over = True
# Set mouse coordinates and mouse event parameters
# Mouse coordinates
mouse_x = mouse_y = 0
# Skateboard coordinates
pos_x = 300
pos_y = 580
# Spherical coordinates
ball_x = (0, 500)
ball_y = -50
# Ball radius
radius = 30
# Drop velocity
vel = 0.5

def print_text(font, x, y, text, color=white):
    imgText = (text, True, color)
    (imgText, (x, y))

Explain:

The game_over is set to True at the beginning because it stops at the beginning of the game and starts again when the mouse is clicked, this is also used to restart the game from the beginning when you die.
.set_visible(False) is to make the mouse invisible

Then comes the main part of the game

# Main loop
while True:
    for event in ():
        if  == :
            exit()
        elif  == :
            mouse_x, mouse_y = 
            move_x, move_y = 
        elif  == :
            lives = 3
            score = 0
            game_over = False

    keys = .get_pressed()
    if keys[K_ESCAPE]:
        exit()

    ((0, 0, 10))

    if game_over:
        print_text(font, 220, 300, "Press MouseButton To Start", white)
    else:
        # The ball fell to the ground
        if ball_y > 600:
            ball_y = -50
            ball_x = (0, 800)
            lives -= 1
            if lives == 0:
                game_over = True
        # The ball is caught by the slider
        elif pos_y < ball_y and pos_x < ball_x < pos_x + 120:
            score += 10
            ball_y = -50
            ball_x = (0, 800)
        # When the ball is neither on the ground nor caught, keep increasing the y-coordinate value so that the ball falls from the top.
        else:
            ball_y += vel
            ball_pos = int(ball_x), int(ball_y)
            (screen, yellow, ball_pos, radius, 0)

        # Skateboards don't draw the line
        pos_x = mouse_x
        if pos_x < 0:
            pos_x = 0
        elif pos_x > 700:
            pos_x = 700

        # Draw the skateboard and follow the mouse left and right
        (screen, white, (pos_x, 580, 100, 20), 0)
        print_text(font, 50, 0, "Score: " + str(score), red)
        print_text(font, 650, 0, "Lives:" + str(lives), red)

    ()

The basic idea is that when the ball falls to the bottom of the screen, or hits the slider, the ball is brought back to the top by assigning a value to its y-coordinate.
When the y-coordinate of the ball is greater than the y-coordinate of the slider, i.e., the ball falls to the height of the slider, and at the same time the x-coordinate of the ball is within the x-coordinate of the slider, then it is considered to be a collision, and the ball remains back on top.
The game is simple, and so is the logic.
That's the basic idea, it's the regular use of the sprite sprite class that will be used later on, as well as the more forbidding collision calculations.

This is the whole content of this article.