pygame3 图像

屏幕上画矩形,圆,或是画点,画曲线只是制作图形的一种方式。有时候我们还想用从别处得来的图片显示在我们的程序中。

1、获取图片
 

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

第一行代码表示将图片文件加载到内存中,以变量my_ball的形式以备调用

第二行代码表示将图片放在窗口[50, 50]的位置

当然,如果想看到效果,和前面的一样,窗口中的图像需要通过

pygame.display.flip()函数将之从缓存打印到实际的显示器上,我们才能看到。

完整代码如下:

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()

效果图:

2 让图片动起来 

想移动球,就要改变图片的位置,即改变screen.blit的坐标值,代码如下:

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()

效果图:

我们发现,球确实移动了,但左边的球还在,说明移动球需要把原来的球擦除掉。

3、动画

利用计算机做动画时,需要做到两步:

1. 在新的位置上画出图形

2. 把原来的图形擦掉

关于擦掉的讨论:

黑板上的擦掉是用黑板擦,如果是画的水彩画呢,比如蓝天上画的小鸟不想要了,想将之擦掉,只能重新画上蓝天将小鸟覆盖。计算机中的擦掉类似于水彩画的擦掉,是不能像黑板上那样擦的,只能画上物体之前的背景将想擦除的物体覆盖

我们要擦除的对象是一个球,像素为90*90的大小,可以考虑用一个rect画出白色的方框,将球覆盖,关键代码:

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

此代码在原先小球的位置画上一个白色方框,将原先的小球完全覆盖

完整代码:

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()
            

 讨论,如果背景是红色的呢,我们怎么擦除?

如果是混合着其它颜色呢?

4、让球一直动

前面球动的时候我们调用blit(my_ball, [150, 50]), 球的坐标是写死的,想让球一直动,一般我们将坐标写成变量[x, y], 然后通过修改变量的值,使得球不断动起来

关键代码:

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()

通过for循环,不断增加x坐标值,使得球不断向右移动

思考一个问题,怎样改变球的速度?

如果把循环100次增大到200次,会发生什么?

5、让球反弹

考虑一下,什么样的情况,球是碰到了右边框呢?screen.get_width()表示右边框

x + 90 > screen.get_width()即表示达到了右边框

在让球反弹时,我们想关闭程序可以吗?

如果想要随时关闭程序,应该把动画放在While循环中

关键代码:

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()

程序中 x + 90 > screen.get_width()是右边框的判断, x < 0是左边框的判断

当 x_speed = 10时,球是从右向上移动,移动到右边框,即判断x+90 > screen.get_width()时, 

x_speed = -x_speed即表示x_speed=-10了,每次x = x + x_speed即表示x = x - 10,即表示每次x坐标减少10,球向左移动,直到x<0时,判断球该反弹了, x_speed又为正10,如此循环往复。

在此过程中,由于都是在while循环中,每移动一下都会检查有没有执行exit,有的话程序退出

6、让球在整个画面中反弹

这个简单,加上对y坐标的变化y_speed即可,完整代码如下:

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、让球翻转,进到屏幕中,从另一边出来

以横向运动为例,当完全进入屏幕右边,相当于x>screen.get_width(),此时只需将x坐标重置为0即可,更精确重置为0-90

完整代码如下:

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()

猜你喜欢

转载自blog.csdn.net/luhouxiang/article/details/127828473