1.抽象類和抽象方法
抽象類:只能被繼承懊烤,不能實(shí)例化(不能創(chuàng)建對象)
抽象方法:聲明的時候不用實(shí)現(xiàn)梯醒。在子類中必須去重寫的方法
怎么聲明抽象類:類繼承abc模塊中的ABCMeta,繼承的時候需要加參數(shù)metaclass
并且要通過abstractmethod來聲明抽象方法
子類繼承一個抽象類后必須在子類中實(shí)現(xiàn)抽象類中所有的抽象方法
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('面積')
抽象類不能實(shí)例化
s1 = Shape()
c1 = Circle()
2.pygame圖片顯示
import pygame
display --> 屏幕相關(guān)
event --> 事件
draw --> 圖形
image --> 圖片
font --> 字體
1.初始化游戲
pygame.init()
2.創(chuàng)建窗口對象
set_mode(size) --> size是元祖:(長,寬), 單位是像素
screen = pygame.display.set_mode((600, 400))
fill(顏色) --> 填充指定的顏色腌紧, 元祖(red, green, blue)
計(jì)算機(jī)使用的是計(jì)算機(jī)三原色(紅茸习、綠、藍(lán)) --> rgb顏色, 對應(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(圖片地址) -> 返回圖片對象
image = pygame.image.load('./files/GH.jpg')
a.獲取圖片的大小
圖片.get_size() --> 返回圖片的大小壁肋,結(jié)果是元祖
image_width, image_height = image.get_size()
b.對圖片進(jìn)行縮放
transform.scale(圖片對象, 大小) --> 將指定的圖片縮放成指定的大小, 返回一個新的圖片對象
注意:這種縮放号胚,可能會讓圖片發(fā)生形變
new_image1 = pygame.transform.scale(image, (600, 400))
c.對圖片進(jìn)行縮放和旋轉(zhuǎn)(按比例縮放)
rotozoom(圖片對象, 角度, 比例)
比例:原圖的多少倍,放大:大于1浸遗, 縮忻ㄐ病:小于1
角度:0 ~ 360 (逆時針旋轉(zhuǎn))
new_image2 = pygame.transform.rotozoom(image, 0, 0.5)
2.渲染圖片
blit(渲染對象,渲染位置)
渲染位置 -> 元祖,(x坐標(biāo), y坐標(biāo))
screen.blit(new_image2, (100, 100))
angle = 0
3.展示內(nèi)容,只要想將內(nèi)容展示在屏幕上,都必須調(diào)用這個方法
pygame.display.flip()
4.游戲循環(huán)(不斷檢測是否有事件發(fā)生)
while True:
不斷檢測事件的產(chǎn)生
for event in pygame.event.get():
不同類型的事件跛锌,event的type屬性不同
if event.type == pygame.QUIT:
exit() 程序結(jié)束
3.pygame文字顯示
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.flip()
顯示文字
1.創(chuàng)建字體對象
SysFont(字體名, 字體大小, 是否加粗=False, 是否傾斜=False) --> 創(chuàng)建系統(tǒng)字體對象
Font(字體文件路徑, 字體大小) --> 自定義字體
字體文件:后綴是.ttf文件
font = pygame.font.SysFont('宋體', 50, italic=True)
font = pygame.font.Font('./files/aa.ttf', 50)
2.根據(jù)字體創(chuàng)建文字對象
字體對象.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()
4.pygame圖形顯示
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))
畫圖(pygame.draw)
1.畫線
def line(Surface, color, start_pos, end_pos, width=1)
Surface: 窗口, 圖片, 文字對象
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)對應(yīng)的元祖
points = [(50, 100), (200, 100), (250, 200), (120, 250), (30, 160)]
pygame.draw.lines(screen, (255, 0, 0), True, points, 6)
2.畫圓
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)) # 將之前畫的全部覆蓋掉
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
1.鼠標(biāo)事件:
事件類型:event.type
MOUSEBUTTONDOWN --> 鼠標(biāo)按下
MOUSEBUTTONUP --> 鼠標(biāo)彈起
MOUSEMOTION --> 鼠標(biāo)移動
關(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)生就會進(jìn)入for循環(huán)
for event in pygame.event.get():
根據(jù)判斷type的值來判斷是什么事件產(chǎn)生了
if event.type == pygame.QUIT:
exit()
=================鼠標(biāo)事件=================
elif event.type == pygame.MOUSEBUTTONDOWN:
鼠標(biāo)按下后要做什么事情就寫在這兒...
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)移動
print('鼠標(biāo)移動', event.pos)
pygame.draw.circle(screen, rand_color(), event.pos, 30)
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))