2018-07-27pygame简单应用

2018-07-30  本文已影响0人  卡塔心里只有学习

一、pygame导入及基本工作框架

import pygame
    if __name__ == '__main__':
# 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()

这是一个最简单的pygame的工作框架,在pygame中实现各种事件都需要这个工作框架,也就是说每一个.py文件都需要此工作框架

二、pygame显示文字

import pygame
if __name__ == '__main__':

pygame.init()
screen = pygame.display.set_mode((600, 400))
# 设置背景颜色
screen.fill((255, 255, 255))
# 1.创建字体
# SysFont(name, size, bold=False, italic=False, constructor=None) 系统字体
# name 字体名字
# size 字体大小
# blod 加粗
# italic 倾斜

font = pygame.font.SysFont('宋体', 80)
"""
创建自定义字体
Font(字体文件路径,字体大小)
"""
font = pygame.font.Font('./font/aa.ttf', 50)
# 2.根据字体去创建显示对象(文字)
# render(self, text, antialias, color, background=None)
# text 要显示的文字内容(str)
# color 计算机三原色(红、绿、蓝)RGB颜色,值的范围都是0~255
#       (255,0,0)红色(0,255,0)绿色(0,0,255)蓝色(0,0,0)黑色(255,255,255)白色(X,X,X)灰色
# antialias 是否平滑

surface = font.render('你好 Python', True, (0, 255, 0))
"""
3.将内容添加到窗口上(画到纸上)
blit(需要显示的对象,显示位置)
需要显示的对象 --> Surface类型的数据
显示位置 --> 坐标(x,y)
"""
screen.blit(surface, (100, 100))

# 4.将窗口上的内容展示出来(将画有文字的纸贴出来)
pygame.display.flip()
# 游戏循环

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

三、pygame中显示图片

import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 1000))
screen.fill((255, 255, 255))

# 1.获取图片对象
image = pygame.image.load('./image/12.jpg')

"""
a.获取图片大小
"""
image_size = image.get_size()
print(image_size)

"""
b.图片形变
transform:形变包括缩放,旋转,平移

scale(缩放对象,新的大小) --> 返回一个缩放后的新对象
rotate(旋转对象,旋转角度)-- 角度是0-360对应的度数
rotozoom(旋转对象,角度旋转,缩放比例)
"""
pygame.transform.scale(image, (500, 600))
pygame.transform.rotate(image, 90)
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()

四、pygame中显示图形

import pygame
if __name__ == '__main__':
pygame.init()

screen = pygame.display.set_mode((1000, 1000))
screen.fill((255, 255, 255))

"""
1.画直线
line(Surface, color, start_pos, end_pos, width=1)
Surface 画在哪个地方
color 线的颜色
start_pos 起点
end_pos 终点
width 线的宽度
"""
# pygame.draw.line(screen, (255, 0, 0), (77, 77), (777, 777), 3)

"""
lines(Surface, color, closed, pointlist, width=1)
画的位置,颜色,是否连接起点和终点,点的列表,线的宽度
"""

# pygame.draw.lines(screen, (255, 255, 0), True, [(77, 77), (233, 421), (789, 963), (555, 555), 10])

"""
2.画矩形
Rect(位置,颜色,坐标,宽度)
"""

pygame.draw.rect(screen, (0, 0, 255), (77, 77, 600, 700), 0)
"""
3.画曲线
arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Rect --> (x, y, width, height)矩形
start_angle 
"""
from math import pi
pygame.draw.arc(screen, (0, 0, 0), (77, 77, 600, 700), pi/2, pi, 5)

"""
4.画圆
circle(Surface, color, pos, radius, width=0)
位置,颜色,圆心,半径,宽度
"""
import random

pygame.draw.circle(screen, (random.randint(0, 255), random.randint(0, 255),\
                            random.randint(0, 255)), (500, 500), 300, 5)

# 将内容展示在屏幕上
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
上一篇 下一篇

猜你喜欢

热点阅读