python基础:pygame模块

python模块语法:

import pygame

pygame.init() # 初始化


print('游戏代码')


pygame.quit()  # 卸载模块,释放资源

rect 用法
描述绘制的窗口的位置,包括坐标,宽度。高度等
练习:绘制一个窗口,并且描述该窗口的位置

import pygame

pygame.init() # 初始化

# 100表示距离x轴原点的位置,
# 200表示距离x轴原点的位置
# 125 矩形的宽度
# 300 矩形的长度
hero_rect=pygame.Rect(100,200,125,300)

print('英雄矩形的x={},y={}'.format(hero_rect.x,hero_rect.y))
print('英雄矩形的width={},height={}'.format(hero_rect.width,hero_rect.height))
print('英雄矩形的中心线center={}'.format(hero_rect.center))
print('英雄底部bottom={}'.format(hero_rect.bottom))
print('英雄的右部{}'.format(hero_rect.right))
print('大小{}'.format(hero_rect.size))

pygame.quit()  # 卸载模块,释放资源import pygame

运行结果:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
英雄矩形的x=100,y=200
英雄矩形的width=125,height=300
英雄矩形的中心线center=(162, 350)
英雄底部bottom=500
英雄的右部225
大小(125, 300)

创建一个游戏窗口

import pygame
pygame.init()

# 1.创建游戏窗口
# 元组中的320表示宽度,568表示高度
pygame.display.set_mode((320,568))

# 2.游戏循环
while True:
    pass

pygame.quit()

此程序中需要加入死循环,使游戏窗口循环建立,否则窗口会在执行后立刻关闭。但是此种方法无法实现窗口正常的关闭
运行结果;
python基础:pygame模块

绘制背景图以及其他图片

import pygame
pygame.init()
# 使用变量screen接收返回值,代表整个窗口对象。
screen=pygame.display.set_mode((320,568))
# 1.加载背景图片
bg_img=pygame.image.load('background_1.png')
# 2.将背景图片加载到窗口中,(0,0)表示背景图片放到原点位置
screen.blit(bg_img,(0,0))
# 3.更新
pygame.display.update()
# 4.加载英雄飞机
hero_image=pygame.image.load('me.gif')
# 5.将英雄飞机绘制到窗口上
screen.blit(hero_image,(127,460))
# 6.更新窗口
pygame.display.update()
while True:
    pass
pygame.quit()

运行结果;
python基础:pygame模块

pygame模块中的时间设置,也就是游戏时钟设置,设置游戏窗口中图片移动的快慢

import pygame
pygame.init()

screen=pygame.display.set_mode((320,568))
bg_img=pygame.image.load('background_1.png')
screen.blit(bg_img,(0,0))
pygame.display.update()
hero_image=pygame.image.load('me.gif')
screen.blit(hero_image,(127,460))
pygame.display.update()

# 创建游戏时钟

clock=pygame.time.Clock()
while True:
    clock.tick(60)  # 每秒钟刷新60次

pygame.quit()

为了使游戏窗口中的图片移动,我们需要改变对应图片的坐标值
例如改变插入的飞机的图片的坐标值,使得飞机可以上下移动
这里需要注意:pygame模块中的坐标值,x轴往右为正,y轴往下为正

import pygame

pygame.init()

screen = pygame.display.set_mode((320, 568))
bg_img = pygame.image.load('background_1.png')
screen.blit(bg_img, (0, 0))
hero_image = pygame.image.load('me.gif')
screen.blit(hero_image, (127, 460))
pygame.display.update()

# 创建游戏时钟

clock = pygame.time.Clock()
# 1.定义英雄飞机的rect
hero_rect=pygame.rect.Rect(127,460,66,80)

while True:
    clock.tick(600)  # 每秒钟刷新60次
    # 2.修改英雄飞机的y轴值
    hero_rect.y-=1
    # 6.让飞机从底部飞进
    if hero_rect.bottom <=0:
        hero_rect.y=568
    # 5.解决飞机背景重影
    screen.blit(bg_img,(0,0))
    # 3.重新绘制
    screen.blit(hero_image,(127,hero_rect.y))
    # 4.窗口更新
    pygame.display.update()

pygame.quit()

此程序实现的是飞机图片y轴坐标的递减,也就是向上移动,最终实现的效果是飞机自动向上移动,直到越出游戏窗口

为了记录游戏中图片移动时的位置,需要用到事件监听
pygame.event.get()

import pygame

pygame.init()

screen = pygame.display.set_mode((320, 568))
bg_img = pygame.image.load('background_1.png')
screen.blit(bg_img, (0, 0))
hero_image = pygame.image.load('me.gif')
screen.blit(hero_image, (127, 460))
pygame.display.update()

# 创建游戏时钟

clock = pygame.time.Clock()
# 1.定义英雄飞机的rect
hero_rect=pygame.rect.Rect(127,460,66,80)

while True:
    clock.tick(60)  # 每秒钟刷新60次
    # 获取所有的事件
    event_lst=pygame.event.get()
    # 捕获窗口退出事件
    for event in event_lst:
        print(event)
        print(event.type)
        if event.type==pygame.QUIT:
            pygame.quit()  # 卸载
            exit()  # 退出系统

    hero_rect.y-=1
    # 6.让飞机从底部飞进
    if hero_rect.bottom <=0:
        hero_rect.y=568
    # 5.解决飞机背景重影
    screen.blit(bg_img,(0,0))
    # 3.重新绘制
    screen.blit(hero_image,(127,hero_rect.y))
    # 4.窗口更新
    pygame.display.update()

pygame.quit()

运行结果:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
<Event(17-VideoExpose {})>
17
<Event(16-VideoResize {'size': (320, 568), 'w': 320, 'h': 568})>
16
<Event(1-ActiveEvent {'gain': 0, 'state': 1})>
1
<Event(4-MouseMotion {'pos': (319, 334), 'rel': (320, 335), 'buttons': (0, 0, 0)})>
4
<Event(1-ActiveEvent {'gain': 1, 'state': 1})>
1
........

运行结果是飞机图片在向上运动过程中对应的部分路径的坐标