Python3:pygame模块的简单的使用(加载图片并让图片动起来)

1.前言

由于前面学习了使用pygame的简单操作,现在学习当前的pygame怎么加载图片,并实现动画效果

2.首先在当前的界面中加载一个图片

使用的图片为:
在这里插入图片描述

# 使用当前的pygame加载图片

import pygame, sys

pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 205])
img = pygame.image.load("noavatar_small.gif")
screen.blit(img, [50, 50])
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

结果:
在这里插入图片描述

1.当前的加载图片需要使用pygame.image.load("noavatar_small.gif"),加载图片

2.将图片映射到界面上:screen.blit(img, [50, 50])

3.简单的让图片动起来

# 使用当前的pygame加载图片

import pygame, sys

pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 205])
img = pygame.image.load("noavatar_small.gif")
screen.blit(img, [50, 50])
pygame.display.flip()
pygame.time.delay(2000)
screen.blit(img, [150, 50])
pygame.draw.rect(screen, [255, 255, 205], [50, 50, 90, 90], 0)
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

结果:
在这里插入图片描述

1.当前的移动就是将原来的图片位置复制到移动的位置,然后将旧位置的图片变成背景颜色即可!

2.还是通过screen.blit(img, [150, 50])复制图片到新的位置,通过pygame.draw.rect将原来的位置的图片变为背景颜色

3.pygame.time.delay(20)就是表示动画刷新的时间

4.让图片循环的动起来(左右动起来)

# 使用当前的pygame加载图片

import pygame, sys

pygame.init()
screen_x = 640
screen_y = 480
init_x = 50
init_y = 50
screen = pygame.display.set_mode([screen_x, screen_y])
screen.fill([255, 255, 205])
img = pygame.image.load("noavatar_small.gif")
img_width = 48
img_height = 48
screen.blit(img, [init_x, init_y])
pygame.display.flip()
# flag_reverse = True
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, 205], [init_x, init_y, 90, 90], 0)
    # if init_x >= screen_x - img_width:
    #     flag_reverse = False
    # elif init_x <= 0:
    #     flag_reverse = True
    # if flag_reverse:
    #     init_x = init_x + 5
    # else:
    #     init_x = init_x - 5
    init_x = init_x + x_speed
    # init_y = init_y + y_speed
    if init_x > screen.get_width() - img_width or init_x < 0:
        x_speed = -x_speed
    screen.blit(img, [init_x, init_y])
    pygame.display.flip()

图片:
在这里插入图片描述
这里的动画就是判断边界即可

5.让当前的图片动起来2

# 使用当前的pygame加载图片

import pygame, sys

pygame.init()
screen_x = 640
screen_y = 480
init_x = 50
init_y = 50
screen = pygame.display.set_mode([screen_x, screen_y])
screen.fill([255, 255, 205])
img = pygame.image.load("noavatar_small.gif")
img_width = 48
img_height = 48
screen.blit(img, [init_x, init_y])
pygame.display.flip()
x_speed = 5
y_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, 205], [init_x, init_y, 90, 90], 0)

    init_x = init_x + x_speed
    init_y = init_y + y_speed
    if init_x > screen.get_width() - img_width or init_x < 0:
        x_speed = -x_speed
    if init_y > screen.get_height() - img_height or init_y < 0:
         y_speed = -y_speed
    screen.blit(img, [init_x, init_y])
    pygame.display.flip()

在这里插入图片描述

6.创建障碍物

# 使用当前的pygame加载图片
# 让图片在指定的墙上反弹
import pygame, sys

pygame.init()
screen_x = 640
screen_y = 480
init_x = 50
init_y = 50
screen = pygame.display.set_mode([screen_x, screen_y])
screen.fill([255, 255, 205])
img = pygame.image.load("noavatar_small.gif")
img_width = 48
img_height = 48
screen.blit(img, [init_x, init_y])
# 创建一个 指定的墙
# pygame.draw.rect(screen, [0, 0, 0], [200, 200, 10, 200], 1)
pygame.display.flip()
flag_reverse = True
x_speed = 5
y_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, 205], [init_x, init_y, 90, 90], 0)
    init_x = init_x + x_speed
    init_y = init_y + y_speed
    if init_x > screen.get_width() - img_width or init_x < 0:
        x_speed = -x_speed
    if init_y > screen.get_height() - img_height or init_y < 0:
        y_speed = -y_speed

    if (init_x >= 320 - img_width and init_x <= 325) \
            and (init_y >= 80 - img_width and init_y <= 100):
        x_speed = -x_speed
        y_speed = -y_speed

    screen.blit(img, [init_x, init_y])
    # 创建一个 指定的墙
    pygame.draw.rect(screen, [0, 0, 0], [320, 80, 5, 20], 1)
    pygame.display.flip()

注意这里使用的是集合的并集!
结果:
在这里插入图片描述

7.总结

1.在任何使用的时候都首先需要初始化当前的pygame:pygame.init()

2.在导入动画的时候需要使用:pygame.image.load()加载本地的图片

3.在需要在pygame.display.set_mode()设置当前的显示界面的大小

4.通过screen.fill()填充画布的背景颜色

5.通过pygame.draw.XXX方式画任何图形

6.如果想让指定的位置显示这个图片:screen.blit(img,[x,y])画这个图片在指定的位置

7.动画就是将原来的图片复制到新的位置,然后将原来的图片擦除(就是附上同样的背景颜色),需要pygame.time.delay(20),和pygame.draw.rect(screen)

  • 8
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值