Pygame(三)畫圓
作業代碼
- 畫一個矩形:要求,左上角在(100,100), 寬為200, 高為60,邊框顏色為藍色
作業代碼:
def homework01():
'''畫一個矩形'''
'''
要求:
1 畫一個矩形:要求,左上角在(100,100), 寬為200, 高為60,邊框顏色為藍色
'''
pygame.init()
screen = pygame.display.set_mode((800, 600))
rect = pygame.Rect(100,100, 200, 60)
pygame.draw.rect(screen, (0, 0, 255), rect, 1)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
- 用矩形畫一個10層的高樓
要求: - 層高:50
- 層寬:逐層遞減,最寬(1層)200, 每層減8
- 顏色:灰色(190, 190, 190)
作業代碼
def homework02():
'''畫一個矩形'''
'''
要求:用矩形畫一個10層的高樓
1. 層高:50
2. 層寬:逐層遞減,最寬(1層)200, 每層減8
3. 顏色:灰色(190, 190, 190)
'''
pygame.init()
screen = pygame.display.set_mode((800, 600))
for i in range(10):
rect = pygame.Rect(300+4*i, 500-50*i, 200-8 * i, 50)
pygame.draw.rect(screen, (190, 190, 190), rect, 1)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()

前情提要

本課提要

內容詳解
畫圓
pygame.draw.circle(Surface, color, pos, radius, width=0)
引數說明:
- Surface : 同上節課,一個Surface型別物件, 一般screen
- color: 線的顏色
- pos:圓心坐標
- radius: 半徑
- width:線寬
說明:- 當設定為0或者不設定時,畫出來的是實心的圓
- 線寬是算在半徑之內的,即當半徑是5的時候,如果設定width = 5,畫出來的是一個實心圓
示例代碼
def drawcircle():
'''函式說明'''
'''
畫一個圓心在中心,半徑=高一半的紅色圓
'''
pygame.init()
screen = pygame.display.set_mode((800, 600))
print(dir(pygame.Surface))
x = screen.get_width()/2
y = screen.get_height()/2
radius = screen.get_height()/2
pygame.draw.circle(screen, (255,0,0), (x,y), radius, width = 1)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
要點說明:
這里用到了兩個Surface物件的方法函式,一個是get_width(),一個是get_height()
顧名思議
get_width:獲取的是螢屏的寬度
get_height:獲取的是螢屏的高度
要想同時獲取寬度與高度,可以用get_size()來實作,回傳的是一個二元組
示例2:奧運五環
def draw_a5():
'''函式說明'''
'''
畫奧云五環
畫法說明:
圓環內圈半徑為單位1,外圈半徑為1.2;相鄰圓環圓心水平距bai離為2.6;兩排圓環圓心垂直距離為1.1
顏色第一行為藍 黑 紅
第二行為 黃 綠
'''
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255,255,255)) # 背景色設定為白色
# 畫第一個圓,顏色藍色:(0,0,255)
pos = x1, y1= 275, 200 # 圓心
r = 50 # 半徑
c = (0, 0, 255) # 藍色
pygame.draw.circle(screen, c, pos, r + 10, width = 10)
# 畫第二個圓, 圓心在第一個圓往右2.5半徑
pos = x2, y2 = x1 + 2.6 * r, y1
c = (0,0,0) # 黑色
pygame.draw.circle(screen, c, pos, r + 10, width = 10)
# 畫第三個圓, 圓心再次往右2.5半徑
pos =x3, y3 = x2 + 2.6 * r, y1
c = (255, 0, 0) # 紅色
pygame.draw.circle(screen, c, pos, r + 10, width=10)
# 第四個圓, 圓心在第一與第二個圓圓心中間向下
pos = x1 + 1.3 * r, y1 + 1.1 * r
c = (255, 255, 0)
pygame.draw.circle(screen, c, pos, r + 10, width=10)
# 第五個圓,圓心在第二與第三個圓的中間向下
pos = x2 + 1.3 * r,y1 + 1.1 * r
c = (0, 255, 0)
pygame.draw.circle(screen, c, pos, r + 10, width=10)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
注意:
- 因為線寬是算在半徑內的.因此,畫一個內圓是50, 外圓是60,所以實際畫的時候要畫半徑為60的圓.
- 背景色默認為黑色,會影響第二個圓環的顯示,所以將背景色設定為白色,用到了screen.fill()方法.引數為RGB元組

后記:
- 矩形與圓是兩個基本的圖形.因此,我們要熟練掌握使用方法.
2.無論是畫矩形,還是畫圓,還是后面的其他圖形,畫出來的東西會回傳一個矩形物件.以用來后續的操作.比如移動什么的.
練習:
- 畫一個邊長為100的正方形,線寬為1, 顏色為藍色.再畫一個內切圓,實心紅色
- 畫一個半徑為200的圓,再畫一個內接正方形
備注:計算根號2取1.41
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243844.html
標籤:python
上一篇:用Python寫一個植物大戰僵尸
