day17-课后总结

pygame

1.抽象类和抽象方法

抽象类:只能被继承不能实例化(不能创建对象)
抽象方法:声明的时候不用实现,在子类中必须去重写的方法
怎么声明抽象类:类继承abc模块中的ABCMeta,继承的时候需要加参数metaclass.
并且要通过abstractmethod来声明抽象方法
子类继承一个抽象类,必须在子类中实现抽象类中所有的抽象方法

metaclass --> 元类

import abc


class Shape(metaclass=abc.ABCMeta):

    # 声明抽象方法
    @abc.abstractmethod
    def draw(self):
        pass

    @abc.abstractmethod
    def area(self):
        pass


class Circle(Shape):
    def draw(self):
        print('画画')

    def area(self):
        print('面积')


# 抽象类不能实例化
# s1 = Shape()

c1 = Circle()
c1.draw()

2.pygame图片显示

"""
display --> 屏幕相关
event --> 事件
draw --> 图形
image --> 图片
font --> 字体
"""
1.初始化游戏
pygame.init()

2.创建窗口对象
set_mode(size) - >size是元祖:(长,宽),单位时像素
"""
screen = pygame.display.set_mode((600, 400))
"""
fill(颜色)-->请填充指定的颜色,元祖(red,green,bule)
计算机使用的是计算机三原色(红、绿、蓝) -->rgb颜色,对应的值的范围时0~255

红色:(255, 0, 0)
绿色:(0, 255, 0)
白色:(255, 255, 255)
黑色:(0, 0, 0)
黄色:(255, 255, 0)

import pygame
screen.fill((255, 255, 255))

# 显示图片
"""
1.加载图片
load(图片地址) --->返回图片对象
"""
image = pygame.image.load('./files/1.jpg')
"""
a.获取图片的大小
图片.get_size()--->返回 图片的大小,结果是元祖
"""
image_width, image_height = image.get_size()
print(image.get_size())
"""
b.对图片进行缩放
transform.scale(图片对象,大小) --> 将指定的图片缩放成指定的大小,返回一个新的图片对象
注意:这种缩放,可能会让图片发生形变
"""
new_image1 = pygame.transform.scale(image, (600, 400))
"""
c.对图片进行缩放和旋转
rotozoom(图片对象, 角度, 缩放比例)
比例:原图的多少倍,放大:大于1,缩小:小于1
角度:0~360(逆时针旋转)
"""
new_image2 = pygame.transform.rotozoom(image, 0, 1)


"""
2.渲染图片
bilt(渲染对象,渲染位置)
渲染位置 -> 元祖(x坐标, y坐标)
"""
screen.blit(new_image2, (100, 100))

angle = 0

"""
3.展示内容,只要想将内容展示出来都必须调用这个方法
"""
pygame.display.flip()

# 3.游戏循环(不断检测是否有时间发生)
while True:
    # 不断检测事件的产生
    for event in pygame.event.get():
        # 不同类型的事件,event的type属性不同
        if event.type == pygame.QUIT:
            exit()  # 结束程序

3.文字的显示

1.创建文字对象
SysFont(字体名,字体大小,是否加粗=False,是否倾斜=False) -->创建系统字体对象
Font(字体文件路径,字体大小) -- >自定义字体
字体文件:后缀是.ttf文件

import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.flip()
font = pygame.font.Font('./files/aa.ttf', 50)
"""
2.根据字体创建文字对象
字体对象.render(文字内容,是否平滑,文字颜色)
"""
text = font.render('你好, python', True, (255, 0, 0))
"""
3.在窗口上的渲染文字
"""
screen.blit(text, (100, 100))

"""
4.展示在屏幕上
"""
pygame.display.flip()


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

4.图形显示

import pygame
import random


def rand_color():
    '''随机颜色'''
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255),

pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))

# 画图
"""
1.画线
def line(Surface,color,start_pos,end_pos,width=1)
Surface :窗口,图片,文字对象
color:线的颜色
start_pos,end_pos:起点和终点(坐标)
width:宽度
"""
pygame.draw.line(screen, (0, 0, 0), (50, 50), (100, 100))

"""
def lines(Surface,color,closed,pointlist,width=1)
closed:是否连接起点和终点
pointlist:列表,列表中的元素是点对应的元祖
"""
points = [(50, 100), (200, 100), (250, 200), (120, 250), (30, 160)]
pygame.draw.lines(screen, (255, 0, 0), False, points, 4)
"""
2.画圆
def circle(Surface,color,pos,radius,width=0)
pos:圆心位置
radius:半径
width:默认0(填充)
"""
pygame.draw.circle(screen, (255, 255, 0), (400, 200), 80)
"""
def arc(Surface,color,Rect,start_angle, stop_angle, width=1)
Rect:(x,y,w,h)
start_angle,stop_angle: 弧度(0->0, 90->Π/2)
"""
from math import pi
screen.fill((255, 255, 255))  # 将之前画的全部覆盖掉
pygame.draw.arc(screen, rand_color(), (100, 100, 100, 100), pi/4, pi/4*3, 4)


pygame.display.flip()


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

5.pygame事件

import pygame
import random
"""
鼠标事件:
事件类型:event.type
MOUSEBUTTONDOWN:按下鼠标
MOUSEBUTTONUP:鼠标弹起
MOUSEMOTION:鼠标移动
关心鼠标的位置:event.pos
键盘事件
"""
def rand_color():
    '''随机颜色'''
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255, 255))
pygame.display.flip()


while True:
    # 只要有事件产生就会进入for循环
    for event in pygame.event.get():
        # 根据判断type来判断是什么事件产生了
        if event.type == pygame.QUIT:
            exit()

        # ========================鼠标事件==========================
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 鼠标摁下后要做什么事情就写在这儿...
            print('鼠标摁下', event.pos)
            # pygame.draw.circle(screen, rand_color(), event.pos, random.randint(10, 40))
            # pygame.display.flip()
        elif event.type == pygame.MOUSEBUTTONUP:
            # 鼠标摁下后弹起
            print('鼠标弹起', event.pos)
        elif event.type == pygame.MOUSEMOTION:
            # 鼠标移动
            print('鼠标移动', event.pos)
            # pygame.draw.circle(screen, rand_color(), event.pos, 20)
            # pygame.display.flip()

        # ======================键盘事件==========================
        elif event.type == pygame.KEYDOWN:
            print('键盘按下', event.key, chr(event.key))
        elif event.type == pygame.KEYUP:
            print('键盘抬起', event.key, chr(event.key))

你可能感兴趣的:(day17-课后总结)