pygame3 image

Drawing rectangles, circles, or dots on the screen, drawing curves is just one way to make graphics. Sometimes we also want to use pictures from other places to display in our programs.

1. Get pictures
 

my_ball = pygame.image.load("beach_ball.png")
screen.blit(my_ball, [50, 50])

The first line of code means to load the image file into the memory and prepare it for calling in the form of variable my_ball

The second line of code indicates to place the picture at the position of window [50, 50]

Of course, if you want to see the effect, as before, the image in the window needs to pass

The pygame.display.flip() function prints it from the cache to the actual display so we can see it.

The complete code is as follows:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")
screen.blit(my_ball, [50, 50])
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

Renderings:

2 Make the picture move 

If you want to move the ball, you need to change the position of the picture, that is, change the coordinate value of screen.blit. The code is as follows:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")
screen.blit(my_ball, [50, 50])
pygame.display.flip()
pygame.time.delay(2000)     # 等待2秒
screen.blit(my_ball, [150, 50]) # 向右移动100像素
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

Renderings:

We found that the ball has indeed moved, but the ball on the left is still there, indicating that moving the ball needs to erase the original ball.

3. Animation

When using a computer to do animation, you need to do two steps:

1. Draw the shape at the new position

2. Erase the original graphics

Discussion about erasing:

The eraser on the blackboard is done with a blackboard eraser. If it is a watercolor painting, for example, the bird painted on the blue sky is no longer wanted. If you want to erase it, you can only repaint the blue sky to cover the bird. The erasing in the computer is similar to the erasing of watercolor painting. It cannot be erased like on a blackboard. It can only be painted on the background before the object to cover the object to be erased.

The object we want to erase is a ball with a size of 90*90 pixels. We can consider using a rect to draw a white box and cover the ball. The key code:

pygame.draw(screen, [255, 255, 255], [50, 50, 90, 90], 0)

This code draws a white box at the position of the original ball, completely covering the original ball

Full code:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")
screen.blit(my_ball, [50, 50])
pygame.display.flip()
pygame.time.delay(2000)
screen.blit(my_ball, [150, 50])
pygame.draw.rect(screen, [255, 255, 255], [50, 50, 90, 90], 0)  # 将原先的小球用白色的方框覆盖
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            

 Discussion, if the background is red, how do we erase it?

What if it is mixed with other colors?

4. Keep the ball moving

When the ball is moving, we call blit(my_ball, [150, 50]). The coordinates of the ball are hard-coded. If we want the ball to keep moving, we usually write the coordinates as variables [x, y], and then modify the value of the variable , making the ball move continuously

key code:

x = 50
y = 50
for looper in range(1, 100):
    pygame.time.delay(20)
    pygame.draw.rect(screen, [255, 255, 255], [x, y, 90, 90], 0)
    x = x + 5
    screen.blit(my_ball, [x, y])
    pygame.display.flip()

Through the for loop, the x coordinate value is continuously increased, so that the ball continues to move to the right

Think about a question, how to change the speed of the ball?

What happens if you increase the loop from 100 times to 200 times?

5. Let the ball bounce

Think about it, what kind of situation does the ball hit the right border? screen.get_width() indicates the right border

x + 90 > screen.get_width() means that the right border has been reached

Is it okay if we want to close the program while bouncing the ball?

If you want to close the program at any time, you should put the animation in the While loop

key code:

x = 50
y = 50
x_speed = 10
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    pygame.time.delay(20)
    pygame.draw.rect(screen, [255, 255, 255], [x, y, 90, 90], 0)
    x = x + x_speed
    if x + 90 > screen.get_width() or x < 0:
        x_speed = -x_speed
    screen.blit(my_ball, [x, y])
    pygame.display.flip()

In the program, x + 90 > screen.get_width() is the judgment of the right border, and x < 0 is the judgment of the left border

When x_speed = 10, the ball moves from the right up to the right border, that is, when judging x+90 > screen.get_width(), 

x_speed = -x_speed means that x_speed=-10, each time x = x + x_speed means x = x - 10, which means that each time the x coordinate is reduced by 10, the ball moves to the left until x<0, it is judged that the ball should After rebounding, x_speed is positive 10 again, and so on.

In this process, since they are all in the while loop, every time you move, you will check whether there is an exit, and if so, the program exits

6. Let the ball bounce across the screen

This is simple, just add the change y_speed to the y coordinate, the complete code is as follows:

import pygame, sys

pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")
x = 50
y = 50
x_speed = 10
y_speed = 10
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255, 255, 255], [x, y, 90, 90], 0)
    x = x + x_speed
    y = y + y_speed
    if x + 90 > screen.get_width() or x < 0:
        x_speed = -x_speed
    if y + 90 > screen.get_height() or y < 0:
        y_speed = -y_speed
    screen.blit(my_ball, [x, y])
    pygame.display.flip()

7. Make the ball flip, go into the screen and come out the other side

Taking horizontal movement as an example, when it completely enters the right side of the screen, it is equivalent to x>screen.get_width(). At this time, you only need to reset the x coordinate to 0, and reset it to 0-90 more precisely.

The complete code is as follows:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")
x = 50
y = 50
x_speed = 5
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255, 255, 255], [x, y, 90, 90], 0)
    x = x + x_speed
    if x > screen.get_width():
        x = 0 - 90
    screen.blit(my_ball, [x, y])
    pygame.display.flip()

Guess you like

Origin blog.csdn.net/luhouxiang/article/details/127828473