(Python3)- PyGame 基本參數學習-5 (*直線、圓弧畫圖*)

(Python3)- PyGame 基本參數學習-5 (*直線、圓弧畫圖*)
1、繪制線條
使用pygame.draw.line()函數來繪制直線,必須提供線條起點和終點
import pygame,sys #導入Pygame
from pygame import * #導入Pygame中的所有常量
pygame.init() #初始化Pygame
screen = pygame.display.set_mode((600,500)) #創建窗口
pygame.display.set_caption("畫直線")
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,80,0))
#畫直線
color = 100,255,200
width = 8
pygame.draw.line(screen,color,(100,100),(500,400),width)
pygame.display.update()
2、繪制弧線
弧線是圓的一部分,使用pygame.draw.arc()函數來繪制
要將角度轉換弧度,使用math.radians()函數
import math,pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("畫弧線")
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,0,200))
#畫弧線
color = 255,0,255
position = 200,150,200,200
start_angle = math.radians(0)
end_angle = math.radians(180)
width = 8
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.display.update()
3、繪制圓
主要是使用 pygame.draw.circle()
import pygame,sys #導入Pygame
from pygame import * #導入Pygame中的所有常量
pygame.init() #初始化Pygame
screen = pygame.display.set_mode((600,500)) #創建窗口
pygame.display.set_caption("畫圓")
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,0,200))
#畫圓
color = 255,255,0
position = 300,250
radius = 100
width = 10
pygame.draw.circle(screen,color,position,radius,width)
pygame.display.update()
4、繪制矩形
通過多個參數來調用pygame.draw.rect()函數,在while循環之外的記錄矩形的位置,并且創建一對速度變量。
import pygame,sys #導入Pygame
from pygame import * #導入Pygame中的所有常量
pygame.init() #初始化Pygame
screen = pygame.display.set_mode((600,500)) #創建窗口
pygame.display.set_caption("畫矩形")
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,0,200))
#移動矩形
pos_x += vel_x
pos_y += vel_y
#讓矩形在屏幕中
if pos_x > 500 or pos_x < 0:
vel_x = -vel_x
if pos_y > 400 or pos_y < 0:
vel_y = -vel_y
#畫矩形
color = 255,255,0
width = 0
pos = pos_x,pos_y,100,100
pygame.draw.rect(screen,color,pos,width)
pygame.display.update()
免責聲明:
1.本影像檔案皆從網上搜集轉載,不承擔任何技術及版權問題。
2.如有下載連結僅供寬頻測試研究用途,請下載後在24小時內刪除,請勿用於商業。
3.若侵犯了您的合法權益,請來信通知我們,我們會及時刪除,給您帶來的不便,深表歉意。