美文网首页
2018-09-04 Day12 Pygame

2018-09-04 Day12 Pygame

作者: Ftr_ | 来源:发表于2018-09-04 17:07 被阅读0次

01.Pygame基本操作

1.初始化游戏模块

    pygame.init()

2.创建游戏窗口

    """
    set_mode(窗口大小)
    窗口大小:是一个元祖,并且元祖中需要两个值分别表示宽度和高度
(单位是像素)
    """
    window=pygame.display.set_mode((400,600))

3.让游戏一直运行,直到点关闭按钮才结束

    while flag:
        # 获取当前游戏的所有时间
        for event in pygame.event.get():
            # type来判断事件的类型
            if event.type==pygame.QUIT:
                exit()#退出程序

02.显示图片

1.初始化游戏模块

pygame.init()

2.创建窗口

window = pygame.display.set_mode((600,400))
# 给窗口填充颜色

3.fill(颜色)
颜色:计算机三颜色:(红绿蓝),每个颜色对应的值的范围是0-255.可以通过改变三原色的值可以调配出不同颜色。
颜色值:是一个元祖,元祖中三个元素,分别代表红绿蓝(rgb)
(255,0,0) ---> 红色
(0,255,0) ---> 绿色
(0,0,255) ---> 蓝色
(0,0,0) ---> 黑色
(255,255,255) ---> 白色

4.显示图片
image.load(图片路径):获取本地的一张图片,返回图片对象

a.获取图片,创建图片对象

image=pygame.image.load('./files/123.png')

1).get_size():获取大小,返回值是一个元祖,有两个元素,分别是高和宽

image_width, image_height = image.get_size()

b.渲染图片(将图片画在纸上)
blit(渲染对象,位置)
位置:坐标(x,y),值的类型是元祖,元祖有两个元素分别对应x坐标y坐标

window.blit(image,(0,100))
window.blit(image,(600-image_width,400-image_width))

c.展示内容(展示图片)

pygame.display.flip()

03.形变

图片相关
1.加载图片(选图)

image = pygame.image.load('./files/123.png')

形变:
a.缩放(指定大小)
transform.scale(缩放对象,目标大小):将指定的对象缩放到指定的大小,会返回缩放后的对象

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

b.缩放(指定缩放比例)
rotozoom(Surface,angle,scale)
-Surface:旋转缩放对象
-angle:旋转角度(0-360)
-scale:缩放比例(0,-x,x),大于1放大,小于1缩小

new_image=pygame.transform.rotozoom(image,99,

rotate(Surface,angle)
-Surface:旋转对象
-angle:旋转角度

new_image=pygame.transform.rotate(image,270)

2.渲染图片

window.blit(new_image,(9,9)) #图片放置位置设定

3.展示内容

pygame.display.flip()

04.文字

显示文字
1.创建字体对象
a.创建系统的字体对象
SysFont(name,size,bold=Folse,italic=False)
-name:字体名(系统支持的字体名)
-size:字体大小
-bold:是否加粗
-italic:是否倾斜

b.创建自定义的文字对象
-Font(字体文件路劲,字体大小)
字体文件路径:ttf文件

1).创建系统文件

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

2).创建自定义文件

font = pygame.font.Font('./files/aa.ttf',30)

2.根据字体创建文字对象
render(text,antialias,color,backgrand=None)
-text:需要显示的文字(字符串)
-antialias:是否平滑(布尔)
-color:颜色
-backgrand:背景颜色

text=font.render('Ftr_2027',True,(255,255,255),(0,0,0))

3.渲染文字

window.blit(text,(139,250))

05.显示图形

1.画线段
line(Surface,color,start_pos,end_pos,width=1)
-Surface:画在哪
-color:线的颜色
-start_pos:起点
-end_pos:终点
-width:线宽

画一条水平线

pygame.draw.line(window,(255,0,0),(50,100),(200,100))

画一条垂直线

pygame.draw.line(window,(255,255,0),(50,100),(50,200),9)

2.画线段(折线)
lines(Surface,color,closed,pointlist,width=1)
-Surface:画在哪
-color:线的颜色
-closed:是否闭合(终点起点的连接)
-pointlist:点对应的列表

pygame.draw.lines(window,(0,0,0),True,[(100,200),(150,120),(99,99)])

3.画圆
circle(Surface,color, pos ,radius,width=0)
位置 颜色 圆心坐标 半径 线宽 0 ->填充

pygame.draw.circle(window,(0,0,0),(200,200),100,9)

4.画矩形
rect(Surface,color,Rect,width=0)
位置 颜色
范围(元祖,元祖中有四个元素,分别是x,y,width,height)

pygame.draw.rect(window,(0,255,0),(0,100,50,100))

5.画多边形

polygn(Surface,color,pointlist,width=0)
#pointlist: 多边形坐标点

pygame.draw.polygon(window,(0,255,255),[(350,0),(350,400),(200,200)])

6.画椭圆
ellipse(Surface,color,Rect,width=0)

pygame.draw.ellipse(window,(123,200,210),(99,150,150,100))

7.画弧线
arc(Surface,color,Rect,start_angle,stop_angele)
-start_angle:开始
-stop_angle:结束

"""
pi --- 180°  1°--- pi/180°
59° = pi/180° * 59°
"""
pygame.draw.arc(window,(200,120,10),(200,400,100,100),pi/4+pi,pi*3/4+pi,3)

06.事件

1.所有事件的处理入口就是这个for循环
2.for循环中的代码只有游戏事件发生后才会执行
3.事件的type:---决定发生的是什么事件
4.QUIT:关闭按钮被点击事件

a.鼠标事件:
MOUSEBUTTONDOWN:鼠标按下事件
MOUSEBUTTONUP:鼠标弹起事件
MOUSEMOTION:鼠标移动事件
b.键盘事件:
KEYDOWN:键盘按下
KEYUP:键盘弹起

b.事件的pos --- 鼠标事件发生的位置(坐标)
c.事件的key --- 键盘事件被按的键对应的编码值

    for event in pygame.event.get():
        #不同的事件发生后,对应type值不一样
        if event.type == pygame.QUIT:
            print('点击关闭按钮')
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 鼠标按下要做的事情就写在这儿
            print(event.pos)
            print('鼠标按下')

            pygame.display.flip()
        elif event.type == pygame.MOUSEBUTTONUP:
            print("鼠标弹起",event.pos)
        elif event.type == pygame.MOUSEMOTION:
            # print("鼠标移动")
            pygame.draw.circle(window, (randint(0, 255), randint(0, 255), \
                                        randint(0, 255)), event.pos, randint(20, 50))
            pygame.display.flip()
            pass
        elif event.type == pygame.KEYDOWN:
            print("键盘按下",event.key,chr(event.key))
        elif event.type == pygame.KEYUP:
            print("键盘弹起")

07.动图原理

import pygame
from  random import randint
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()

# 球的圆心坐标
x = 50
y = 50
r = 50
y_speed=2
x_speed=1
# 游戏循环
while True:
    pygame.time.delay(9)
    # 将之前纸上的内容覆盖
    window.fill((255,255,255))
    # 不断的画圆
    pygame.draw.circle(window,(0,0,0),(x,y),r)
    pygame.display.update()#刷新

    # 改变Y值让圆在垂直方向移动
    y+=y_speed
    if y>600-r:
        y=600-r
        y_speed=-2
    elif y<50:
        y=50
        y_speed=2
    x += x_speed
    if x > 400 - r:
        x = 400 - r
        x_speed = -2
    elif x < 50:
        x = 50
        x_speed = 2

    # 事件循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

08.按住不放原理

import pygame
# 游戏初始化
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
# pygame.display.flip()

# 1.显示一张图片
image = pygame.image.load('./files/123.png')
# 缩放
image = pygame.transform.rotozoom(image, 0, 0.5)
window.blit(image, (100, 100))
# 获取图片的宽度和高度
image_w, image_h = image.get_size()

pygame.display.flip()

# 用来存储图片是否移动
flag = False

# 保存图片的坐标
image_x, image_y = 100, 100
# 游戏循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           exit()

# 鼠标按下
    if event.type == pygame.MOUSEBUTTONDOWN:
         # 判断按下的位置是否在图片上
         m_x, m_y = event.pos
           if image_x<=m_x<=image_x+image_w and image_y<=m_y<=image_y+image_h:
              flag = True
           elif event.type == pygame.MOUSEBUTTONUP:
              flag = False
# 鼠标移动事件
# (鼠标在移动并且flag是True)
if event.type == pygame.MOUSEMOTION and flag:
   # 填充背景色,覆盖原来的内容
   window.fill((255, 255, 255))
    # 在鼠标移动的位置渲染图片
    # window.blit(image, event.pos)
    center_x, center_y = event.pos
    image_x, image_y = center_x - image_w/2, center_y-image_h/2
    window.blit(image, (image_x, image_y))
     # 更新屏幕的显示
     pygame.display.update()

相关文章

  • 2018-09-04 day12 pygame

    1.创建游戏窗口 2.图片文件操作 2.1.获取图片,创建图片对象 image.load(图片路径): 获取本地的...

  • 2018-09-04 Day12 Pygame

    01.Pygame基本操作 1.初始化游戏模块 2.创建游戏窗口 3.让游戏一直运行,直到点关闭按钮才结束 02....

  • Day12 pygame

    1.pygame基本操作: 1.导入pygame: import pygame.2.初始化:pygame init...

  • 2018-09-04-pygame

    一、pygame基本操作 import pygame——导入pygame模块 pygame.init()——初始化...

  • 2018-09-04

    2018-09-04 万千工品金秀 2018-09-04 00:21 · 字数 932 · 阅读 0 · 日记本 ...

  • Pygame入门--飞机大战案例

    Pygame的快速入门 #导入pygame模块 import pygame #游戏初始化 pygame.init(...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • Pygame-hello world

    使用pygame 模块名功能pygame.cdrom访问光驱pygame.cursors加载光标pygame.di...

  • Day12—pygame的基本操作

    1 游戏模块的创建 一个基本的游戏模块创建分为三步:初始化游戏模块;创建游戏窗口;让游戏一直运行,直到点关闭按钮才...

  • 2018-09-04 day 12 python pygame

    1.步骤

网友评论

      本文标题:2018-09-04 Day12 Pygame

      本文链接:https://www.haomeiwen.com/subject/gjfnwftx.html