Lecture 다각형 그리기
Lecture
• Views 54
• Comments 0
• Last Updated at 3 weeks ago
- 도형그리기
polygon
pygame.draw.polygon(surface, color, pointlist, width=1)
- surface : 도형을 그릴 창
- color : 색
- pointlist : 선들의 [ 시작점, 끝 점 ] 의 리스트
- width : 선의 굵기, 0이거나 없으면 닫힌 도형안을 채워준다.
import pygame
pygame.init()
screen = pygame.display.set_mode((500,400))
clock = pygame.time.Clock()
#p_img = pygame.image.load('tiger.bmp')
#p_img = pygame.transform.scale(p_img, (100,100))
white = (255,255,255)
black = (0,0,0)
running = True
while running:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.draw.polygon(screen,black,[[50,50],[100,300],[150,350],[200,200],[180,30]],5)
#screen.blit(p_img, (x,y))
pygame.display.flip()
#pygame.display.update()
pygame.quit()
draw triangle
import pygame
pygame.init( )
#색설정
BLACK= ( 0, 0, 0)
WHITE= (255, 255, 255)
#화먼설정
size = [400, 300]
screen = pygame.display.set_mode(size)
pygame.display.set_caption(”다각형 그리기!!!”)
#메인루프
running = True
clock = pygame.time.Clock( )
while running:
clock.tick(10)
for event in pygame.event.get( ):
if event.type == pygame.QUIT: # 〈닫기〉 버튼을 클력하면
running = False # 루프 탈출을 위한 변수 설정
screen.fill(WH ITE)
#삼각형그리기
pygame.draw.polygon(screen, BLACK, [[100, 50], [50, 125], [150, 125]], 5)
pygame.draw.polygon(screen, BLACK, [[250, 50], [200, 125], [300, 125]])
pygame.display.flip( )
pygame.quit( )
import pygame
from pygame.locals import *
from random import *
pygame.init( )
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption(”마우스로 클릭하여 다각형 그리기∼”)
screen.fill((255,255,255))
#클릭한 점의 좌표 목록 저장
points_list = []
running = True
clock= pygame.time.Clock( )
while running:
clock.tick(1)
for event in pygame.event.get( ):
if event.type == QUIT:
running = False
# 마우스를 클릭하면 점의 좌표를 points_list에 저장
if event.type == MOUSEBUTTONDOWN:
points_list.append(event.pos)
#다각형의채우기 색상은 랜덤하게
random_FillColor = (randint(0, 255), randint(0,255), randint(0, 255))
# 마우스로 클럭한 점을 검정색 원으로 표시
for point in points_list:
pygame.draw.circle(screen, (0, 0, 0), point, 3)
# 점의 좌표 개수가 3개 이상이면 다각형을 그림
if len(points_list) >= 3:
pygame.draw.polygon(screen, random_FillColor, points_list)
pygame.display.update( )
pygame.quit( )
Login to write a comment.