一号俐、抽象類(lèi)和抽象方法
抽象類(lèi):只能被繼承不能實(shí)例化(不能創(chuàng)建對(duì)象)
抽象方法:聲明的時(shí)候不用實(shí)現(xiàn)儒老,在子類(lèi)中必須去重寫(xiě)的方法
怎么聲明抽象類(lèi):類(lèi)繼承abc模塊中的ABCMeta钥弯,繼承的時(shí)候需要加參數(shù)metaclass皆刺。 并且要通過(guò)abstractmethod來(lái)聲明抽象方法
子類(lèi)繼承一個(gè)抽象類(lèi)壶运,必須在子類(lèi)中實(shí)現(xiàn)抽象類(lèi)中所有的抽象方法
metaclass -> 元類(lèi)
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('畫(huà)圖形')
def area(self):
print('面積')
# 抽象類(lèi)不能實(shí)例化
# s1 = Shape()
c1 = Circle()
二针饥、pygame圖片顯示
import pygame
"""
display --> 屏幕相關(guān)
event --> 事件
draw --> 圖形
image --> 圖片
font --> 字體
"""
# 1.初始化游戲
pygame.init()
# 2.創(chuàng)建窗口對(duì)象
"""
set_mode(size) --> size是元祖:(長(zhǎng)厂抽,寬), 單位是像素
"""
screen = pygame.display.set_mode((600, 400))
"""
fill(顏色) --> 填充指定的顏色, 元祖(red, green, blue)
計(jì)算機(jī)使用的是計(jì)算機(jī)三原色(紅丁眼、綠筷凤、藍(lán)) --> rgb顏色, 對(duì)應(yīng)的值的范圍是0-255
紅色:(255, 0, 0)
綠色: (0, 255, 0)
白色:(255, 255, 255)
黑色:(0, 0, 0)
黃色: (255, 255, 0)
"""
screen.fill((255, 255, 255))
# 4.顯示圖片
"""
1.加載圖片
load(圖片地址) -> 返回圖片對(duì)象
"""
image = pygame.image.load('./files/luffy4.jpg')
"""
a.獲取圖片的大小
圖片.get_size() --> 返回圖片的大小,結(jié)果是元祖
"""
image_width, image_height = image.get_size()
"""
b.對(duì)圖片進(jìn)行縮放
transform.scale(圖片對(duì)象, 大小) --> 將指定的圖片縮放成指定的大小, 返回一個(gè)新的圖片對(duì)象
注意:這種縮放苞七,可能會(huì)讓圖片發(fā)生形變
"""
new_image1 = pygame.transform.scale(image, (600, 400))
"""
c.對(duì)圖片進(jìn)行縮放和旋轉(zhuǎn)(按比例縮放)
rotozoom(圖片對(duì)象, 角度, 比例)
比例:原圖的多少倍藐守,放大:大于1挪丢, 縮小:小于1
角度:0 ~ 360 (逆時(shí)針旋轉(zhuǎn))
"""
new_image2 = pygame.transform.rotozoom(image, 0, 0.5)
"""
2.渲染圖片
blit(渲染對(duì)象,渲染位置)
渲染位置 -> 元祖,(x坐標(biāo), y坐標(biāo))
"""
screen.blit(new_image2, (100, 100))
angle = 0
"""
3.展示內(nèi)容,只要想將內(nèi)容展示在屏幕上卢厂,都必須調(diào)用這個(gè)方法
"""
pygame.display.flip()
# 3.游戲循環(huán)(不斷檢測(cè)是否有事件發(fā)生)
while True:
# 不斷檢測(cè)事件的產(chǎn)生
for event in pygame.event.get():
# 不同類(lèi)型的事件乾蓬,event的type屬性不同
if event.type == pygame.QUIT:
exit() # 程序結(jié)束
三、pygame文字顯示
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.flip()
# 顯示文字
"""
1.創(chuàng)建字體對(duì)象
SysFont(字體名, 字體大小, 是否加粗=False, 是否傾斜=False) --> 創(chuàng)建系統(tǒng)字體對(duì)象
Font(字體文件路徑, 字體大小) --> 自定義字體
字體文件:后綴是.ttf文件
"""
# font = pygame.font.SysFont('宋體', 50, italic=True)
font = pygame.font.Font('./files/aa.ttf', 50)
"""
2.根據(jù)字體創(chuàng)建文字對(duì)象
字體對(duì)象.render(文字,是否抗鋸齒,顏色)
"""
text = font.render('你好足淆,python', True, (0, 255, 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()
四巢块、pyganme圖形顯示
import pygame
import random
def rand_color():
"""隨機(jī)顏色"""
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))
# 畫(huà)圖(pygame.draw)
"""
1.畫(huà)線
def line(Surface, color, start_pos, end_pos, width=1)
Surface: 窗口, 圖片, 文字對(duì)象
color:線的顏色
start_pos,end_pos: 起點(diǎn)和終點(diǎn)(坐標(biāo))
width:寬度
"""
pygame.draw.line(screen, (0, 0, 0), (50, 50), (100, 100), 5)
"""
def lines(Surface, color, closed, pointlist, width=1)
closed: 是否連接起點(diǎn)和終點(diǎn)
pointlist: 列表,列表中的元素是點(diǎn)對(duì)應(yīng)的元祖
"""
points = [(50, 100), (200, 100), (250, 200), (120, 250), (30, 160)]
pygame.draw.lines(screen, (255, 0, 0), True, points, 6)
"""
2.畫(huà)圓
def circle(Surface, color, pos, radius, width=0)
pos: 圓心位置
radius: 半徑
width: 默認(rèn)0(填充)
"""
pygame.draw.circle(screen, (255, 255, 0), (100, 200), 80, 0)
"""
def arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Rect: (x, y, w, h)
start_angle, stop_angle: 弧度(0->0, 90->pi/2, 45 -> pi/4)
"""
from math import pi
screen.fill((255, 255, 255)) # 將之前畫(huà)的全部覆蓋掉
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()
五巧号、pygame事件
1.鼠標(biāo)事件:
事件類(lèi)型:event.type
MOUSEBUTTONDOWN --> 鼠標(biāo)按下
MOUSEBUTTONUP --> 鼠標(biāo)彈起
MOUSEMOTION --> 鼠標(biāo)移動(dòng)
關(guān)鍵鼠標(biāo)的位置:event.pos
import random
def rand_color():
"""隨機(jī)顏色"""
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))
pygame.display.flip()
while True:
# 只要有事件產(chǎn)生就會(huì)進(jìn)入for循環(huán)
for event in pygame.event.get():
# 根據(jù)判斷type的值來(lái)判斷是什么事件產(chǎn)生了
if event.type == pygame.QUIT:
exit()
# =================鼠標(biāo)事件=================
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠標(biāo)按下后要做什么事情就寫(xiě)在這兒...
print('鼠標(biāo)按下:', event.pos)
pygame.draw.circle(screen, rand_color(), event.pos, random.randint(10, 40))
pygame.display.flip()
elif event.type == pygame.MOUSEBUTTONUP:
# 鼠標(biāo)按下后彈起
print('鼠標(biāo)彈起', event.pos)
elif event.type == pygame.MOUSEMOTION:
# 鼠標(biāo)移動(dòng)
print('鼠標(biāo)移動(dòng)', event.pos)
# pygame.draw.circle(screen, rand_color(), event.pos, 30)
# pygame.display.flip()
2.鍵盤(pán)事件
elif event.type == pygame.KEYDOWN:
print('按鍵按下:', event.key, chr(event.key))
elif event.type == pygame.KEYUP:
print('按鍵彈起:', event.key, chr(event.key))