pygame显示图片的方法_day_10 pygame文字图片图形显示

1.pygame的显示流程

1.初始化

2.创建游戏窗口

3.游戏循环

# 1.初始化pygame

pygame.init()

# 2.创建游戏窗口

# set_mode((宽度, 高度))

screen = pygame.display.set_mode((600, 400))

# 3.游戏循环

while True:

# 检测事件

for event in pygame.event.get():

# 检测窗口上的关闭按钮是否被点击

if event.type == pygame.QUIT:

# 退出游戏

print('关闭按钮被点击')

exit()

# 其它操作

2.文字显示

2.1.创建字体对象

a.创建系统字体

SysFont(name, size, bold=0, italic=0, constructor=None)

name -> 字体名

size -> 字体大小

bold -> 加粗

italic -> 倾斜

font = pygame.font.SysFont('Times', 16)

b.创建自定义字体

Font(字体文件路径,字体大小)

font = pygame.font.Font('./font/font/HYShangWeiShouShuW.ttf', 22)

2.2.根据字体去创建显示对象

render(self, text, antialias, color, background=None)

text -> 要显示的文字内容(str)

antialias -> 是否平滑

color -> 计算机三原色(红、绿、蓝),rgb颜色,值的范围0-255

(255,0,0) -> 红色 (0,0,0)-> 黑色

(255,255,255)-> 白色 (x,x,x)-> 灰色

surface = font.render('hello, python', True, (255, 0, 0))

"""

将内容写到纸上

"""

print(type(surface))

2.3.将内容添加到窗口上

blit(需要显示的对象,显示的位置)

需要显示的对象 --> Surfce类型的数据

显示位置 --> 坐标(x, y)

screen.blit(surface, (100, 100))

2.4.将窗口上的内容展示出来

pygame.display.flip()

# 游戏循环

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

3.图片显示

步骤:1.获取图片对象

2.将图片对象渲染到窗口上

3.展示在屏幕上

"""__author__=xx"""

import pygame

if __name__ == '__main__':

pygame.init()

screen = pygame.display.set_mode((600, 400))

screen.fill((255,255,255))

# 1.获取图片对象

image = pygame.image.load('./font/image/new.jpg')

"""

a.获取图片大小

get_size()

"""

image_size = image.get_size()

print(image_size)

"""

b.形变

transform:形变包含缩放、旋转和移动

缩放

scale(缩放对象,新的大小) --> 返回一个缩放后的新对象

"""

# image = pygame.transform.scale(image, (600, 400))

"""

旋转

rotate(旋转对象, 旋转角度) -- 角度是0-360对应的度数

"""

# image = pygame.transform.rotate(image, 90)

"""

旋转缩放

rotozoom(对象,旋转角度,缩放比例)

"""

image = pygame.transform.rotozoom(image, 45, 1)

# 2.将图片对象渲染到窗口上

screen.blit(image,(0, 0))

# 3.展示在屏幕上

pygame.display.flip()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

4.画图形

4.1.画直线

方法1:line(Surface, color, start_pos, end_pos, width=1)

Surface -> 画在那个地方

color -> 线的颜色

start_pos -> 起点

end_pos -> 终点

width -> 线的宽度

pygame.draw.line(screen, (255, 255, 0), (100, 100), (200, 60), 10)

方法2:lines(画线的位置,颜色,closed, 点的列表,width=1)

pygame.draw.lines(screen, (123,22,188), True, [(0, 0), (100, 100), (100, 200)])

4.2.画矩形

rect(位置, 颜色, (x, y, width, height))

pygame.draw.rect(screen,(255,255,0),(200, 100, 200,200))

4.3.画曲线

arc(Surface, color, Rect, start_angle, end_angle, width=1)

Rect -> (x, y, width, height)矩形

from math import pi

pygame.draw.arc(screen, (0, 0, 0), (200, 100, 200,200), 1.1*pi, 1.9*pi)

4.4.画圆

circle(位置, 颜色, 圆心位置, 半径,width=0)

pygame.draw.circle(screen, (22, 44, 88), (150, 150), 100, 50)

4.5.画椭圆

ellipse(位置, color, Rect, width=0)

pygame.draw.ellipse(screen, (200, 30, 100),(400, 300, 100, 50))

5.动画

原理:用下一张画布去覆盖上一张画布,改变其中的要动的物体的坐标就行了

import pygame

if __name__ == '__main__':

pygame.init()

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

screen.fill((255, 255, 255))

x = 0

y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

screen.fill((255, 255, 255))

pygame.draw.circle(screen, (255, 0, 0), (x, y), 10)

x += 1

y += 1

if x > 600:

x, y = 0, 0

pygame.display.flip()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值