python project pinball game 3

Okay, let’s go ahead and make our pinball game! Yesterday we completed the most basic window and the most basic ball, so today we will add the racket responsible for interacting with the player!

First, let’s conceive the basic functions and functions of the racket. The racket, as the name suggests, is the platform responsible for bouncing the ball. It is also responsible for the interactive function with the player. How to interact? There are several methods:

1. Direction key control.

2. Mouse coordinate control.

Each of the above methods has its pros and cons, but I think using mouse control is more humane. So we first face the easier mouse controls for beginners.

Obtaining coordinates in pygame is relatively simple. We only need to refer to the "Escape Key Detection" in Part 1. But before that, we need to draw a racket.

We create a new file and enter these codes to generate a rectangle:

import pygame

pygame.init()
screen=pygame.display.set_mode([640,480])
screen.fill([0,0,0])
pygame.draw.rect(screen,[255,255,255],[250,150,300,200],0)
pygame.display.update()
running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False

pygame.quit()

Looks familiar, right?

That's right, it just adds a filled rectangle to the basic structure. We can make the rectangle appear where we want it to appear by changing the coordinates on line 6.

So how do you look at these coordinates? it's actually really easy. Its format is this: <

Guess you like

Origin blog.csdn.net/CSP_J/article/details/128779916
Recommended