1. Pygame的基本使用
首先我们需要先安装它
pip install pygame
import time
# 导入pygame的包
import pygame
# 初始化pygame,让计算机硬件做准备
pygame.init()
# 创建一个窗口对象,并指定宽 500 和高 700
pygame.display.set_mode([500, 700])
# 导入磁盘上的图片资源文件,返回一个图片对象
image = pygame.image.load("res/app.ico")
# 设置窗口显式的图标
pygame.display.set_icon(image)
# 设置窗口名称
pygame.display.set_caption("飞机大战v1.0")
time.sleep(5)
这里绘制了游戏的框框和标题 , 最后让程序等待5秒
2.pygame的图片基本处理
# 导入pygame的包
import pygame
import time
# 初始化pygame,让计算机硬件做准备
pygame.init()
# 初始化窗口对象的宽高属性
WINDOW_WIDTH = 512
WINDOW_HEIGHT = 768
# 创建一个窗口对象,并指定宽和高,返回一个窗口对象
window = pygame.display.set_mode([WINDOW_WIDTH, WINDOW_HEIGHT])
# 加载图片资源文件,返回图片对象
# 背景图
bg_image = pygame.image.load("images/bg.jpg")
# 飞机图片
plane_image = pygame.image.load("images/plane.png")
# 绘制图片,将图片的坐标原点放到窗口的指定位置,参数1:图片对象;参数2:窗口的坐标(窗口左上角为原点(0,0))
# 先绘制背景图,再绘制飞机图,否则背景图会覆盖飞机图,导致不显示飞机图
window.blit(bg_image, (0, 0))
window.blit(plane_image, (100, 200))
# 刷新窗口,让图片加载到窗口中
pygame.display.update()
time.sleep(3)
这里绘制游戏的框框,并把游戏背景和小飞机 放在上面
- blit 用户绘制图片对象 参数1:图片对象;参数2:窗口的坐标(窗口左上角为原点(0,0))
- pygame.display.update() 刷新窗口,让图片加载到窗口中 ,如果没有这句上面绘制的东西将不会出现在窗口中
3.pygame的图片移动处理 -- rect矩形对象
# 导入pygame的包
import pygame
import time
# 初始化pygame,让计算机硬件做准备
pygame.init()
# 初始化窗口对象的宽高属性
WINDOW_WIDTH = 512
WINDOW_HEIGHT = 500
window = pygame.display.set_mode([WINDOW_WIDTH, WINDOW_HEIGHT])
bg_image = pygame.image.load("images/bg.jpg")
window.blit(bg_image, (0, 0))
plane_image = pygame.image.load("images/plane.png")
"""
创建这个图片的矩形对象,这个对象其实就是一个元组
(图片原点在x轴坐标, 图片原点在y轴坐标, 图片的宽, 图片的高)
默认x,y轴的坐标都是0
"""
plane_rect = plane_image.get_rect()
# print(plane_rect) # <rect(0, 0, 120, 78)>
"""
通过矩形对象的 move_ip(x, y) 这个方法,可以将矩形对象相对于当前位置移动指定的偏移量
if 按下键盘上键:
plane_rect.move_ip(0, -偏移量)
if 按下键盘下键:
plane_rect.move_ip(0, +偏移量)
if 按下键盘左键:
plane_rect.move_ip(-偏移量, 0)
if 按下键盘右键:
plane_rect.move_ip(+偏移量, 0)
"""
# 将飞机图片移动到屏幕中间
plane_rect.move_ip(WINDOW_WIDTH / 2 - plane_rect[2] / 2, WINDOW_HEIGHT / 2 - plane_rect[3] / 2)
# print(plane_rect) # <rect(196, 345, 120, 78)>
# 根据矩形对象设置飞机图片位置
window.blit(plane_image, (plane_rect[0], plane_rect[1]))
# 设置速度
speed=-1
# 动态让飞机图片每次向上偏移1
while True:
#plane_rect.move_ip(0,-1)
if plane_rect.top < 0 or plane_rect.top > WINDOW_HEIGHT:
speed=+1
if plane_rect.bottom < 0 or plane_rect.bottom > WINDOW_HEIGHT:
speed=-1
plane_rect.move_ip(0,speed)
window.blit(bg_image, (0, 0))
window.blit(plane_image, (plane_rect[0], plane_rect[1]))
# 刷新窗口,让图片加载到窗口中
pygame.display.update()
前面的代码和上面2步差不多。
这里引入了矩形对象。
因为图片本身并不能移动,通过矩形对象的移动,然后获取它的坐标
然后再在变化了的坐标上绘制图片blit
这里的关键函数是 move_ip(x, y) 这个方法,可以将矩形对象相对于当前位置移动指定的偏移量 (并不是移动到指定的坐标,而是在原来的坐标基础上加上去)
4、pygame的事件处理
# 导入pygame的包
import pygame
import time
# 初始化pygame,让计算机硬件做准备
pygame.init()
# 初始化窗口对象的宽高属性
WINDOW_WIDTH = 512
WINDOW_HEIGHT = 500
window = pygame.display.set_mode([WINDOW_WIDTH, WINDOW_HEIGHT])
bg_image = pygame.image.load("images/bg.jpg")
window.blit(bg_image, (0, 0))
plane_image = pygame.image.load("images/plane.png")
"""
创建这个图片的矩形对象,这个对象其实就是一个元组
(图片原点在x轴坐标, 图片原点在y轴坐标, 图片的宽, 图片的高)
默认x,y轴的坐标都是0
"""
plane_rect = plane_image.get_rect()
# print(plane_rect) # <rect(0, 0, 120, 78)>
"""
通过矩形对象的 move_ip(x, y) 这个方法,可以将矩形对象相对于当前位置移动指定的偏移量
if 按下键盘上键:
plane_rect.move_ip(0, -偏移量)
if 按下键盘下键:
plane_rect.move_ip(0, +偏移量)
if 按下键盘左键:
plane_rect.move_ip(-偏移量, 0)
if 按下键盘右键:
plane_rect.move_ip(+偏移量, 0)
"""
# 将飞机图片移动到屏幕中间
plane_rect.move_ip(WINDOW_WIDTH / 2 - plane_rect[2] / 2, WINDOW_HEIGHT / 2 - plane_rect[3] / 2)
# print(plane_rect) # <rect(196, 345, 120, 78)>
# 根据矩形对象设置飞机图片位置
window.blit(plane_image, (plane_rect[0], plane_rect[1]))
# 设置速度
speed=-1
# 动态让飞机图片每次向上偏移1
while True:
#plane_rect.move_ip(0,-1)
if plane_rect.top < 0 or plane_rect.top > WINDOW_HEIGHT:
speed=+1
if plane_rect.bottom < 0 or plane_rect.bottom > WINDOW_HEIGHT:
speed=-1
plane_rect.move_ip(0,speed)
window.blit(bg_image, (0, 0))
window.blit(plane_image, (plane_rect[0], plane_rect[1]))
# 刷新窗口,让图片加载到窗口中
pygame.display.update()
4.1 evenList = pygame.event.get() 获取当前发生的事件, 打印一下会发现是一个list 数据类型
[<Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57})>, <Event(2-KeyDown {'unicode': '', 'key': 273, 'mod': 0, 'scancode': 72})>, <Event(2-KeyDown {'unicode':
'', 'key': 276, 'mod': 0, 'scancode': 75})>]
我们通过for循环,得出当前发生事件,然后作出处理。
4.2 pressed_key = pygame.key.get_pressed() 获取键盘中的连续按键, 返回一组布尔值的元组。 如果是被一直按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表示上键一直被按着
5参考
https://blog.youkuaiyun.com/wingzhezhe/article/details/79136620