pygame

1、介绍及安装
(1)pygame——2D游戏开发工具包

(2)安装:>>> pip install pygame >>>import pygame

(3)帮助文档:https://www.pygame.org/docs/

(4)Hello World
引入相关包import pygame, sys
初始化pygame.init()
得到窗口 pygame.Surface对象pygame.display.set_mode((400,300))
游戏主循环:处理游戏事件 — 更新游戏状态 — 在屏幕上绘制
游戏事件for event in pygame.event.get(): #退出游戏事件 if event.type == QUIT: pygame.quit() sys.exit()

示例:

# 引入相关的包
import sys, pygame

# 进行初始化操作
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

# 得到初始化对象
screen = pygame.display.set_mode(size)

ball = pygame.image.load("intro_ball.gif")
ballrect = ball.get_rect()

# 游戏主循环
while 1:
    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    # 更新游戏状态
    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    #在屏幕上进行绘制
    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

2、文字及颜色
(1)游戏中的文字
在这里插入图片描述

import pygame, sys

pygame.init()

screen = pygame.display.set_mode((500,500))

red = pygame.Color(255, 0 ,0)
# 加载字体
font = pygame.font.get_fonts()
# print(font)

# 方式一:使用系统默认的字体进行加载
#font = pygame.font.SysFont('华文新魏', 40)
# 方式二:自己准备好一个字体文件ttf,放到咱们的项目里面
font = pygame.font.Font('simhei.ttf',40)
# 文字对象 得到surface对象
text = font.render('得分', True, red)
# 文字加载


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
# 在屏幕上绘制
    screen.blit(text, (20, 20))
    pygame.display.flip()

(2)游戏中的颜色:
使用RGB表示:取值范围: 0-255
RGBA表示透明色:A(alpha):0-255

线:

import sys, pygame

# 初始化pygame
pygame.init()

# 屏幕对象
screen = pygame.display.set_mode((500, 500))

# 加载图片
ball = pygame.image.load('./static/intro_ball.gif')

# 红色
red = pygame.Color(255, 20, 255)

# 游戏主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # 更新状态

    # 画线
    pygame.draw.line(screen, red, (10, 10), (200, 200), 10)

    # 画矩形
    pygame.draw.rect(screen, red, (10, 20, 200, 300), 10)

    # 画圆
    pygame.draw.circle(screen, red, (100, 100), 50, 5)

    # 绘制
    screen.blit(ball, (100, 100))
    pygame.display.flip()

3、游戏中的图片
图片加载:bg = pygame.image.load('bg.png')
在屏幕上绘制:screen.blit(bg,bg.get_rect())

4、游戏音效
在这里插入图片描述

5、动画切换
在这里插入图片描述

6、精灵及精灵组
精灵:精灵可以认为是一个个小图片,一种可以在屏幕上移动的图形对象,并且可以与其他图形对象交互。精灵图像可以是使用pygame绘制函数绘制IDE图像,也可以是原来就有的图像文件

精灵组:是一个容器,用于管理组内精灵的绘制和更新

7、碰撞检测
两个精灵之间的矩形检测:pygame.sprite.collide_rect(sprite_1,sprite_2 pygame.sprite.collide_rect_ratio(0.5)(sprite_1,sprite_2

两个精灵之间的圆形检测:pygame.sprite.collide_circle(sprite_1,sprite_2)

两个精灵之间的像素遮罩检测:pygame.sprite.collide_mask(sprite_1,sprite_2)

精灵和精灵组之间的碰撞检测:pygame.sprite.spritecollideany(sprite,sprite_group,bool)

# 1.引入相关的包
import sys, pygame

# 2. pygame进行初始化
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

# 3. 得到屏幕对象Surface
screen = pygame.display.set_mode((320, 240))


class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height, init_pos):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rect = self.image.get_rect()
        self.rect.topleft = init_pos


# 实例化精灵对象
sprite_1 = Block(pygame.Color(255, 0, 0), 50, 50, (50, 50))
sprite_2 = Block(pygame.Color(0, 255, 0), 50, 50, (90, 90))


# 4. 游戏主循环
while 1:

    # 处理游戏的事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    # 往屏幕上画精灵
    screen.blit(sprite_1.image, sprite_1.rect)
    screen.blit(sprite_2.image, sprite_2.rect)

    # 矩形检测碰撞
    rest = pygame.sprite.collide_rect(sprite_1, sprite_2)
    print('rest', rest)
    # 链式调用
    rest2 = pygame.sprite.collide_rect_ratio(0.5)(sprite_1, sprite_2)
    print('rest2', rest2)

    pygame.display.flip()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值