python—pygame(游戏开发模块)

使用pygame提供的功能之前,一般需要调用 pygame.init() 方法
pygame.init() 用来初始化大部分的pygame模块

游戏结束后,需要调用pygame.quit() 方法:
pygame.quit() 卸载所有的pygame模块

import pygame
pygame.init()
pygame.quit()

游戏中的坐标系:原点在左上角(0,0),x轴水平向右增加,y轴垂直向下增加
游戏中所有可见元素都是以矩形区域来描述的。描述矩形区域四要素(x,y, width,height)
pygame提供了一个类用于描述矩形区域pygame.Rect(特殊的类,不需要pygame.init()也可使用)

import pygame
pos = pygame.Rect(100, 500, 120, 125)
print("position:%d, %d" % (pos.x, pos.y))
print("size:%d, %d" % (pos.width, pos.height))
print("%d,%d" % pos.size)   # size是一个元组类型

创建游戏主窗口
pygame的pygame.display用于创建、管理游戏窗口
pygame.dispaly.set_mode() 初始化游戏显示窗口
set_mode(resolution=(0,0), flags=0, depth=0)
resolution执行屏幕的宽和高;flags指定附加选项,默认不需要传递;depth表示颜色的位数

pygame.display.update() 刷新屏幕内容显示

import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))

hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (200, 500))
pygame.display.update()
while True:
    pass

pygame.quit()

刷新帧率
pygame.time.Clock设置屏幕绘制速度(刷新帧率)
使用时钟对象:1、游戏初始化创建始终对象;2、游戏循环中让时钟对象调用tick(帧率)方法

clock = pygame.time.Clock()
clock.tick(60)	# 每秒60次

在游戏循环中监听事件
事件event:在游戏启动后,用户针对游戏所做的操作
监听:游戏循环中,对用户操作的捕获

for event in pygame.event.get():
    # 判断是否是退出事件
    if event.type == pygame.QUIT:
        print("游戏退出...")
        # quit卸载所有模块
        pygame.quit()
        # 终止程序
        exit()

精灵和精灵组
精灵pygame.sprite.Sprite----一个存储image和rect的对象

精灵组pygame.sprite.Group
一个精灵组可以包含多个精灵,对加入的精灵进行批处理

plane_sprits.py


import pygame

class GameSprite(pygame.sprite.Sprite):
    """飞机大战游戏精灵"""

    def __init__(self, image_name, speed=1):

        # 继承非object父类,init方法中必须调用父类init方法
        super().__init__()

        # 定义对象的属性
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):

        # 在屏幕的垂直方向上移动
        self.rect.y += self.speed
pygame_primary.py


import pygame
from plane_sprites import *

pygame.init()

screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))

hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (200, 500))

pygame.display.update()

clock = pygame.time.Clock()	# Clock对象

# 注意这里hero_rect和hero本来没有关系,但是后面pygame.blit中,hero_rect作为了hero的位置载体
hero_rect = pygame.Rect(150, 300, 102, 126)

# 创建敌机精灵
enemy = GameSprite("./images/enemy1.png")
enemy1 = GameSprite("./images/enemy1.png", 2)

# 创建敌机精灵组
enemy_group = pygame.sprite.Group(enemy, enemy1)

# 游戏循环
while True:
    # 指定刷新率
    clock.tick(60)

    # 监听事件
    for event in pygame.event.get():
        # 判断是否是退出事件
        if event.type == pygame.QUIT:
            print("游戏退出...")
            # quit卸载所有模块
            pygame.quit()
            # 终止程序
            exit()

    hero_rect.y -= 5

    if hero_rect.y <= 0:
        hero_rect.y = 700

    screen.blit(bg, (0, 0))
    screen.blit(hero, (hero_rect.x, hero_rect.y))	 # 也可以直接传入hero_rect

    # 让精灵组调用两个方法
    # updata
    enemy_group.update()
    # draw
    enemy_group.draw(screen)

    pygame.display.update()


pygame.quit()

字母雨

import random
import pygame
 
# 初始化 
panel_width = 800
panel_highly = 600
font_px = 15
 
pygame.init()
 
# 创建一个可视窗口
winSur = pygame.display.set_mode((panel_width, panel_highly))
font = pygame.font.SysFont("123.ttf", 25)
# pygame.SRCALPHA使得字母有渐变消失效果
bg_suface = pygame.Surface((panel_width, panel_highly), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 28))
winSur.fill((0, 0, 0))
 
# 字母版
letter = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c',
          'v', 'b', 'n', 'm']
texts = [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)]
 
# 按屏幕的宽计算可以在画板上放几列坐标,并生成一个列表
column = int(panel_width / font_px) 
drops = [0 for i in range(column)]
 
while True:
    # 获取事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
 
    # 暂停5秒
    pygame.time.delay(80)
 
    # 覆盖画布
    winSur.blit(bg_suface, (0, 0))
 
    for i in range(len(drops)):
        text = random.choice(texts)
 
        # 重新编辑每个坐标点的图像
        winSur.blit(text, (i * font_px, drops[i] * font_px))
 
        drops[i] += 1
        if drops[i] * 10 > panel_highly or random.random() > 0.95:
            drops[i] = 0
 
    pygame.display.update()
 
 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H4ppyD0g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值