Pygame

作者: 蘑菇plus | 来源:发表于2018-07-27 17:33 被阅读0次

文件操作

1.模块
导入模块(自定义模块,第三方模块,系统其他模块)
import 模块
使用方式:模块.内容
from 模块 import 内容--->内容

被导入的模块中,除了声明在if__name__=='main':判断中的内容以外的其他的全聚德

2.文件操作
打开文件--操作文件--关闭文件
open()操作文件 close()
---with open()as文件:
操作文件

open(打开文件路径,打开方式-->'r',编码方式--->二进制文件不能设置)
注意:一般只有文本文件设置编码方式
文本文件-->'r'/'w','a'
二进制文件-->'rb'/'wb'
以读的方式打开文件,如果文件不存在会报错,以写的方式打开文件,如果文件不存在,会在指定路径下创建该文件

read()-->读文件中所有内容
write()-->将内容添加到文件中
3.json文件(文本文件)
json支持的数据类型和对应的字面量
字符串(string)--->""
数字(number)--->10/12.9
布尔--->true/false
数组(array)--->[12,"a",true]
字典(object)--->{"name":""dandan}
null--->null(空)

import json
json.load(json文件对象)--->读,读出来的数据的类型和json的数据类型一样(最外层的数据类型)
json.dump(需要写入文件的数据,json文件对象)--->写,python中的基本数据类型,可以直接写入json文件

4.异常捕获
try:
需要捕获异常的代码(就是一段代码出现异常,不让程序崩溃)
except:
只要try后面的代码出现异常,都会执行这段代码,并且程序不会崩溃

try:
    print([1,2,4][5])
except:
    print("出现异常")

try:
    需要捕获异常的代码块
except 异常的类型:
    出现指定异常后才会执行

try--except执行过程:先执行try后面的代码块,只要出现异常就使用except去捕获,
如果能捕获到,就直接进入except中,执行里面的代码块,执行完成后,在执行后面的其他语句。
如果捕获不到,就会直接报错,如果try后面的代码中,没有异常,那么执行完代码块中的内容直接
执行后面的其他语句(异常语句里面,有一条执行,就不会再执行后面的语句)

想要同时捕获多个异常:

except(错误类型1,错误类型2):


try:
    代码块1
except 错误类型1:
    代码块2
except 错误类型2:
    代码块3
    
    

try:
    代码块1
except:
    代码块2
finally:    
    代码块3
代码块3是在代码块1中没有出现异常,和代码块1中出现异常被捕获到都会执行

raise:抛出异常

总结:1.异常捕获不是什么都要用,只有在程序员清楚会出现异常,并且想要自己来
处理异常,而不是让程序崩溃的情况下才异常捕获
2.使用异常捕获的时候,不能让except直接捕获所有的异常,而是捕获特定的异常

while True:
    try:
        number=int(input("请输入数字:"))
        print(number)
        break
    except ValueError:
        print("数据格式有误!")

pygame

1.初始化pygame

#1.初始化pygame
pygame.init()
#2.创建游戏窗口
screen=pygame.display.set_mode((600,400))
#3.游戏循环(死循环)
while True:
    # 检测事件
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            #退出游戏
            print('点击按钮关闭')
            exit()

2.显示文字

import pygame
if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    #设置窗口背景颜色
    screen.fill((255,255,255))
    """
    1.创建系统字体
    def SysFont(name, size, bold=0, italic=0, constructor=None):
    name-->字体名
    size-->字体大小
    bold-->加粗
    italic-->倾斜
    """
    # font=pygame.font.SysFont('  Times',100)
    """
    创建自定义字体
    Font(字体文件路径,字体大小)
    """
    font=pygame.font.Font('./font/aa.ttf',22)
    #2.根据字体去创建显示对象(文字)
    """
    render(self, text, antialias, color, background=None):
    text-->要显示的文字内容(str)
    antialias-->是否平滑
    color-->计算机三原色(红,绿,蓝),RGB颜色,范围都是0-255
    (255,0,0)-->红色
    (0,255,0)-->绿色
    (0,0,255)-->蓝色
    (0,0,0)-->黑色
    (255,255,255)--白色
    (x,x,x)--灰色(当三原色数值相同,为灰色,数值越接近0,灰色越深)
    """
    surface=font.render('dandan',True,(0,255,0))
    3.#将内容添加到窗口上
    """
    blit(需要显示的对象,显示位置)
    需要显示的对象-->surface类型的数据
    显示位置-->坐标(x,y)
    """
    screen.blit(surface,(150,100))
    #4.将窗口上的内容展示出来(将画有文字的纸贴出来)
    pygame.display.flip()

    #游戏循环
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

显示图片

import pygame
if __name__ == '__main__':
    pass
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    #获取图片对象
    image=pygame.image.load('./ppl.png')
    """
    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,360)
    """
    def rotozoom(旋转对象,旋转角度,缩放比例)
    """
    image=pygame.transform.rotozoom(image,60,0.4)
    #将图片对象渲染到窗口上
    screen.blit(image,(0,0))
    #3.展示在屏幕上
    pygame.display.flip()



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

显示图形

"""__author__ = 余婷"""
import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600, 400))
    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), (78, 59), (100, 100), 2)
    pygame.draw.line(screen, (0, 255, 0), (0, 0), (130, 100), 2)

    """
    lines(画线的位置, 颜色, closed, 点的列表, width=1)
    """
    pygame.draw.lines(screen, (0, 0, 255), True, [(10, 10), (200, 50), (100, 100)])


    """
    画矩形
    rect(位置,颜色,(x,y,width,height))
    """
    pygame.draw.rect(screen,(255,255,0),(0,0,200,200),2)

    """
    2.画曲线
    arc(Surface, color, Rect, start_angle, stop_angle, width=1)
    Rect -> (x, y, width, height)矩形
    start_angle
    stop_angle
    """
    from math import pi
    pygame.draw.arc(screen, (0, 0, 0), (0, 0, 100, 200), pi+pi/4, pi*2-pi/4)


    """
    3.画圆
    circle(位置, 颜色, 圆心位置, 半径, width=0)
    """
    import random
    pygame.draw.circle(screen,\
            (random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
            (400,200),100)

    """
    画椭圆
    ellipse(Surface, color, Rect, width=0)
    """
    pygame.draw.ellipse(screen, (0, 100, 0), (100, 300, 200, 80), 1)



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

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

动画原理

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600, 400))
    screen.fill((255, 255, 255))
    x = 0
    y = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        x += 1
        y += 1
        screen.fill((255,255,255))
        pygame.draw.circle(screen,(255,0,0),(x,y),80)
        pygame.display.flip()

相关文章

  • 2018-09-04-pygame

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

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

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

  • Day_10 异常与pygame

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

  • Pygame-hello world

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

  • pygame - alphabet

    pygame install pygame install[https://www.pygame.org/wiki...

  • Day12 pygame

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

  • Day-18正则表达式2

    pygame游戏基本框架的创建 pygame中图片的显示 字体的显示 图形 Pygame Pygame有很多的模块...

  • day11-pygame笔记

    1pygame事件 import pygame pygame.display.set_caption('游戏事件'...

  • Python——Pygame模块

    学习资料: Pygame官网 pygame系列 PyGame - Python Wiki 用Python和Pyga...

  • pygame简介

    简介 关于Pygame的基本信息,pygame是什么,谁会被Pygame吸引,并且在哪里找到它。 Pygame是被...

网友评论

      本文标题:Pygame

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