Python__模块(界面-小游戏)__pygame(代码实践)

参考代码

加载字体

import sys
import pygame

# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
screen = pygame.display.set_mode((400, 400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption("demo")
# 引入字体类型
f = pygame.font.Font("C:/Users/Administrator/Desktop/NotoSansSC-Regular.otf", 50)
""" 
    生成文本信息, 
    第一个参数:文本内容 
    第二个参数:是否平滑 
    第三个参数:字体颜色 
    第四个参数:背景颜色 
     
"""
text = f.render("Test Area", True, (255, 255, 0), (0, 0, 0))
# 获得显示对象的rect区域坐标
textRect = text.get_rect()
# 设置显示对象居中
textRect.center = (200, 200)
# 将准备好的文本信息,绘制到主屏幕Screen上
screen.blit(text, textRect)
# 固定代码段,实现点击'X'号退出界面的功能,几乎所有Pygame都使用该段代码
while True:
    # 循环获取事件,监听事件状态
    for event in pygame.event.get():
        # 判断用户是否点击了'X'关闭按钮,并执行if代码段
        if event.type == pygame.QUIT:
            # 卸载所有模块
            pygame.quit()

            sys.exit()
    # 更新屏幕内容
    pygame.display.flip()

"""
注释:
pygame.init() 必填项:作用是自动检测pygame软件包是否正常可用...
创建Surface对象:Pygame种最重要的组成部分
               可以理解成一张白纸,通过这张纸可以做许多的事情,比如,纸上添加文字,填充颜色,添加图片...

"""

图像绘制

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("demo2")
screen.fill("white")
# 创建一个50*50的图像,并优化显示
face = pygame.Surface((50, 50), flags=pygame.HWSURFACE)
# 填充颜色
face.fill(color="blue")
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(face, (100, 100))
    # 更新屏幕内容
    pygame.display.flip()

图片加载

import pygame
from pygame.locals import * # 引入pygame中的所有常量

pygame.init()
screen = pygame.display.set_mode((500, 250))
pygame.display.set_caption("demo3")
# 加载一张图片
img = pygame.image.load("C:/Users/Administrator/Desktop/1.jpg").convert()
# rect(let,top,width,height) 指定图片上某个区域
# special_flags功能标志位,指定颜色混合模式,默认为0用纯色填充
img.fill((0, 0, 255), rect=(50, 50, 400, 200), special_flags=0)
# 图片偏移量x,y
img.scroll(50, 50)
# 无限循环,让窗口停留
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        # 将图像放置在主屏幕上
        screen.blit(img, (0, 0))
        pygame.display.update()

图片操作

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 250))
pygame.display.set_caption("demo4")
img = pygame.image.load("C:/Users/Administrator/Desktop/2.jpg").convert()
# 图片放大
img = pygame.transform.scale(img, (300, 250))
# 图片旋转45度
img1 = pygame.transform.rotate(img, -90)
# 旋转0度,将图像缩小0.5倍
img2 = pygame.transform.rotozoom(img1, 0, 0.5)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        screen.blit(img2, (0, 0))
        pygame.display.update()

图片(创建子图)

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 300))
pygame.display.set_caption("demo7")
img = pygame.image.load("C:/Users/Administrator/Desktop/bg2.jpg")
r1 = pygame.Rect(50, 50, 100, 100)
# 在原图的基础上创建一个新的子图
imgChild = img.subsurface(r1)
r2 = imgChild.get_rect()
# 输出的矩形大小为 100*100
print(r2)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    # 在屏幕上希纳是子图的区域
    screen.blit(imgChild, r2)
    pygame.display.update()

 画图

import pygame
from math import pi

pygame.init()
size = (500, 450)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("demo9")

# 设置一个控制主循环的变量
done = False
# 创建时钟对象
clock = pygame.time.Clock()
while not done:
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True  # 若检测到关闭窗口
    # 绘制一条宽度为3的红色对角线
    pygame.draw.line(screen, (255, 0, 0), [0, 0], (500, 450), 3)
    # 绘制多条蓝色的直线(连续直线,非抗锯齿),False表示首尾不相连
    pygame.draw.lines(
        screen, (0, 0, 255), False, [[0, 80], [50, 90], [200, 80], [200, 30]], 1
    )
    # 绘制一个灰色的矩形区域,以灰色填充区域
    pygame.draw.rect(screen, (155, 155, 155), (75, 10, 50, 20), 0)
    # 绘制一条线框宽度为2的矩形区域
    pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20], 2)
    # 绘制一个椭圆形,其线宽为2
    pygame.draw.ellipse(screen, (255, 0, 0), (225, 10, 50, 20), 2)
    # 绘制一条实心的红色椭圆形
    pygame.draw.ellipse(screen, (255, 0, 0), (300, 10, 50, 20))
    # 绘制一条绿色边框(宽度为2)三角形
    pygame.draw.polygon(screen, (100, 200, 45), [[100, 100], [0, 200], [200, 200]], 2)
    # 绘制一条蓝色实心的圆形,其中[60,250]表示圆心的位置,40为半径,width默认为0
    pygame.draw.circle(screen, (0, 0, 255), [60, 250], 40)
    # 绘制一个圆弧,其中0表示弧线的开始位置,pi/2表示弧线的结束位置,2表示线宽
    pygame.draw.arc(screen, (255, 10, 0), (210, 75, 150, 125), 0, pi / 2, 2)
    # 刷新显示屏幕
    pygame.display.flip()
# 点击关闭,退出pygame程序
pygame.quit()

时间延迟

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("demo5")
# 获取以毫秒为单位的时间
t = pygame.time.get_ticks()  # 该事件指的是从pygame初始化后开始计算
t1 = pygame.time.wait(3000)
print(t1)
# 暂停t1时间后,加载图片
img = pygame.image.load("C:/Users/Administrator/Desktop/bg1.jpg")
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.blit(img, (0, 0))
    pygame.display.update()

FPS帧率调节

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("demo6")
t = pygame.time.get_ticks()
t1 = pygame.time.delay(3000)

img = pygame.image.load("C:/Users/Administrator/Desktop/bg1.jpg")
# 创建时钟对象(控制游戏中的FPS)
clock = pygame.time.Clock()

# 注释:变量test 个人练习测试,仅用于查看效果
test = 0
while True:
    # 通过时钟对象,指定循环帧率,每秒循环60次
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    test += 1
    screen.blit(img, (test, 0))
    if test > 300:
        test = 0
    pygame.display.update()

event事件

import pygame
import sys

pygame.init()
size = width, height = 600, 400
print(size)
bg = (255, 255, 255)
img = pygame.image.load("C:/Users/Administrator/Desktop/bg1.jpg")
# 获取图像的位置
position = img.get_rect()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("demo8")

while True:
    # 设置初始值
    site = [0, 0]
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # 图像移动 KEYDOWN键盘按下事件
        # 通过key属性对应按键
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                site[1] -= 8
            if event.key == pygame.K_DOWN:
                site[1] += 8
            if event.key == pygame.K_LEFT:
                site[0] -= 8
            if event.key == pygame.K_RIGHT:
                site[0] += 8
    # 移动图像
    position = position.move(site)
    # 填充背景
    screen.fill(bg)
    screen.blit(img, position)
    pygame.display.flip()

猜你喜欢

转载自blog.csdn.net/werdasooooo/article/details/134914497