Hello and welcome to Crossin’s programming classroom!

Today, I would like to talk to you about: how to make games

The theme of the game is the pinball game PONG, which is the first arcade game in history. That’s why I chose it as the topic for the first installment of my game development series.

The game engine uses a Python game library: PGZero. It encapsulates PyGame and lets you configure your game’s content logic without having to write extra routine code.

We wrote the game with it, and it only took 100 lines of code.

First, you need to install the Python environment. For those who have trouble with this step, see python666.cn, an introduction to Python.

Then you need to install the PGZero library, which can be installed from the command line via the PIP command:

pip install pgzero
Copy the code

After installation, run one sentence

pgzrun.go()
Copy the code

The door to our game world is open.

It’s still dark up there.

Set the upper-left coordinates and width of a rectangle, fill it with the specified color in the game’s draw function, and we have a rectangle.

pad_1 = Rect((20, 20), (10, 100))

def draw():
    screen.clear()
    screen.draw.filled_rect(pad_1, 'white')
Copy the code

With a few adjustments, you get a board used to block balls in the game.

Add judgment in the update function of the game, when the “up” and “down” keys on the keyboard are pressed, modify the y coordinate of the baffle, you can control the movement of the baffle in the game.

PAD_SPEED = 10

def update(dt):
    if keyboard.up:
        pad_1.y -= PAD_SPEED
    elif keyboard.down:
        pad_1.y += PAD_SPEED
Copy the code

This completes the player-controlled role of PONG: a paddle that moves up and down. Now we’re using only 10 lines of code.

Some of you may have noticed that there are two functions, one is called draw, which is responsible for the graphics in the game, and the other is called Update, which is responsible for the logical updates in the game.

We often hear that games run at 30 Frames Per Second, 60 Frames Per Second or something like that, or FPS (Frames Per Second). Draw and Update are what you do in a “frame” of a game. The higher the performance of your computer or game console, the less time it takes to compute each frame, the higher the number of frames, and the smoother the experience.

Create a type called Ball with property values including position and speed. Then, a circle is drawn with the position of the ball as the center of the circle in the drawing function. In the update function, the position of the ball in the next frame is calculated according to the displacement formula of uniform linear motion, that is, displacement = velocity x time. So you have a ball that moves.

class Ball():
    def __init__(self):
        self.pos = [300, 200]
        self.speed = [1, 1]        
    def update(self, dt):
        for i in range(2):
            self.pos[i] += self.speed[i] * dt

ball = Ball()

def draw():
    screen.clear()
    screen.draw.filled_rect(pad_1, 'white')
    screen.draw.filled_circle(ball.pos, BALL_RADIUS, 'white')
Copy the code

Set the boundary conditions so that when the ball reaches the edge of the screen, it can change the direction of its velocity. When it reaches the upper and lower edges, multiply the y-velocity component by -1. When it reaches the left and right edges, it can reset its position back to the center of the screen.

class Ball():
    ...      

    def update(self, dt):
        for i in range(2):
            self.pos[i] += self.speed[i]

        if self.pos[1] < 0 or self.pos[1] > HEIGHT:
            self.speed[1] *= -1
        if self.pos[0] < 0 or self.pos[0] > WIDTH:
            self.reset()
Copy the code

With the board, with the ball, the next step is to make them relate.

Do a collision check in the update function: If the rectangle of the board intersects the center of the ball, bounce the ball back.

def update(dt):
    ...
    
    ball.update(dt)

    if pad_1.collidepoint(ball.pos) and ball.speed[0] < 0:
        ball.speed[0] *= -1
Copy the code

At this point, the core physics rules of the game have been defined.

In the same way, create a second board on the right side of the screen, controlled by additional buttons. Then, when the ball goes beyond the left and right boundaries, each side scores.

class Ball():
    ...      

    def dead(self, side):
        scores[side] += 1
        self.reset()
Copy the code

And there you have it: a very simple, two-player version of pinball.

Of course, if you can’t find another person to play with, you can use your left hand to play with your right hand.

Or, add a bit of auto-tracking code to one side of the board: make the board move with the ball. This is also a game AI.

def auto_move_pad(dt): if ball.pos[0] > WIDTH / 2 and ball.speed[0] > 0: If pad_2.y + pad_2.height * 0.25 > ball.pos[1]: pad_2.y -= PAD_SPEED * dt if pad_2.top < 0: Top = 0 elif pad_2.y + pad_2.height * 0.75 < ball.pos[1]: pad_2.y += PAD_SPEED * dt if pad_2.y. pad_2.bottom = HEIGHTCopy the code

At this point, PONG, a pinball game with a complete core gameplay, is complete. It’s less than 100 lines of code with Spaces. Especially suitable for programming novice just contact game development partner to practice.

However, I added a little more detail to the game, which you can click on to watch. If you like, welcome to like and forward!

www.bilibili.com/video/BV1Pr…

Then I’ll come back and try more types of games, more gameplay. Try to meet the initial FLAG: make 100 games. If you want to see a certain type of game or implementation, or have any questions about implementation details, let me know in the comments, and I’ll take precedence.

The code is open source and is available through Crossin’s Programming classroom

Github.com/crossin/gam…

—-

Get more tutorials and examples,

Welcome to search and follow: Crossin programming classroom

5 minutes a day to learn programming easily.