異常捕獲
try:
"""
需要捕獲異常的代碼
"""
except:
"""
出現(xiàn)異常會(huì)出現(xiàn)的代碼
"""
try:
'''
出現(xiàn)異常會(huì)出現(xiàn)的代碼
'''
except '''錯(cuò)誤類型''':
'''
捕獲到指定的錯(cuò)誤類型镜廉,才執(zhí)行的代碼
'''
if __name__ == '__main__':
try:
a=0
b=3
print(b/a)
except ZeroDivisionError:
print('error')
pygame操作流程
# 初始化
pygame.init()
# 創(chuàng)建窗口
screen =pygame.display.set_mode((600,400))
# 游戲循環(huán)
while True:
# 檢測事件
for event in pygame.event.get():
# 檢測窗口關(guān)閉按鈕是否被點(diǎn)擊
if event.type == pygame.QUIT:
print('game over')
exit()
# 退出游戲
# 其他操作
pygame顯示文字
pygame.init()
screen = pygame.display.set_mode((600,400))
screen.fill((230,230,39))
# 設(shè)置窗口背景顏色
# 創(chuàng)建字體對象
# 創(chuàng)建系統(tǒng)字體
# SysFont(name, size, bold=0, italic=0, constructor=None):
# name-字體名 size-字體大小 bold-加粗 italic-傾斜
# font = pygame.font.SysFont('Times',20)
# 2.根據(jù)字體創(chuàng)建顯示對象
# 創(chuàng)建自定義字體Font(字體文件路徑瓢湃,字體大小)
font = pygame.font.Font('./font/aa.ttf',50)
surface = font.render('hello開始',True,(230,39,39))
# text-要顯示的文字內(nèi)容,antialias-抗鋸齒,color-顏色,backgroud-背景
# color->計(jì)算機(jī)三原色(紅綠藍(lán)),值的范圍都是0-255
# render(self, text, antialias, color, background=None)
# 3.將內(nèi)容添加到窗口上
# blit(需要顯示的對象,顯示位置)
# 顯示的對象-->Surface類型的數(shù)據(jù)
# 顯示位置-->坐標(biāo)(x,y)
#
screen.blit(surface,(100,100))
# 4.將窗口上的內(nèi)容展示出來
# pygame.display.flip()
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
pygame顯示圖片與圖片操作
pygame.init()
screen = pygame.display.set_mode((324,310))
screen.fill((230,39,39))
font = pygame.font.Font('./font/aa.ttf',20)
surface = font.render('路飛',True,(230,39,39))
# 1.獲取圖片對象
image = pygame.image.load('./file/2.jpg')
image_size = image.get_size()
# 獲取圖片大小
print(image_size)
# 形變旭愧,transform:形變包含縮放scale辽狈、旋轉(zhuǎn)、平移
# scale1 = pygame.transform.scale(image,(200,200))
# screen.blit(scale1,(5,5))
# rotate(旋轉(zhuǎn)對象玩讳,旋轉(zhuǎn)角度)負(fù)數(shù)為順時(shí)針反向
# rotate1 = pygame.transform.rotate(image,-45)
# screen.blit(rotate1,(5,5))
# rotozoom(Surface,angle,scale)旋轉(zhuǎn)縮放,參數(shù):對象涩蜘,角度,縮放比例
# rotozoom1 = pygame.transform.rotozoom(image,0,0.5)
# screen.blit(rotozoom1,(5,5))
# 2.將圖片對象渲染到窗口上
# screen.blit(image,(5,5))
# 3.展示屏幕上
screen.blit(surface,(5,10))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
pygame基本顯示
screen = pygame.display.set_mode((400,400))
screen.fill((230,200,0))
'''
1.畫直線
line(Surface,color,start_pos,end-pos,width=1)
Surface->畫在哪里 start_pops->起點(diǎn)
'''
# 正方形
# pygame.draw.line(screen,(255,0,0),(0,0),(0,200),2)
# pygame.draw.line(screen, (255, 0, 0), (0, 0), (200, 0), 2)
# pygame.draw.line(screen, (255, 0, 0), (0, 200), (200, 200), 2)
# pygame.draw.line(screen, (255, 0, 0), (200, 200), (200, 0), 2)
# 多個(gè)點(diǎn)closed關(guān)閉
# pygame.draw.line(screen, (255, 0, 0), False,[], 2)
# 展示
# 畫曲線
# arc(Surface,color,Rect,start_angle,stop_angle,width=1)
# Rect->(x,y.width,height)矩形
# start_angle-開始角度
# 4分之一圓
# 在定義的矩形框內(nèi)畫內(nèi)切圓熏纯,開始角度與結(jié)束角度決定圓的范圍
from cmath import pi
pygame.draw.arc(screen,(0,0,0),(200,200,100,100),pi,2*pi)
pygame.draw.arc(screen, (0, 0, 0), (250, 300, 5, 5), pi, 2*pi)
pygame.draw.arc(screen, (0, 0, 0), (0, 200, 100, 100), 0, 2*pi)
pygame.draw.arc(screen, (0, 0, 0), (50, 300, 5, 5), 0, 2*pi)
# pygame.draw.arc(screen, (0, 0, 0), (200, 200, 100, 100), pi / 2, pi)
# 畫圓
# circle(位置,顏色同诫,圓心位置,半徑樟澜,width-0)
import random
pygame.draw.circle(screen,(random.randint(0,255),20,20),(100,100),50)
pygame.display.flip()
# 橢圓的畫法:矩形內(nèi)切 (0,2pi)或ellipse
pygame.draw.ellipse(screen,(0,100,0),(100,100,200,80),1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
exit()