강의노트 선 그리기
강의노트
• 조회수 91
• 댓글 0
• 수정 1주 전
- 도형그리기
line
pygame.draw.line(surface, color, start, end, width=1)
- surface : 도형을 그릴 창
- color : 색
- start : 선 시작 점
- end : 선 끝 점
- width : 선의 굵기
import pygame
from pygame.locals import *
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
left_key, right_key, up_key, down_key = False, False, False, False
while running:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.draw.line(screen,black,[50,100],[350,300],5)
#screen.blit(p_img, (x,y))
pygame.display.update()
pygame.quit()
import pygame
pygame.init( )
BLACK= ( 0, 0, 0)
WHITE= (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
#화면설정
size = [600, 300]
screen = pygame.display.set_mode(size)
pygame .display.set_ caption('선 그러기')
#메인루프
running = True
clock = pygame.time.Clock()
while running:
clock.tick(10)
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#width 값이 홀수인 경우
pygame.draw.line(screen, GREEN, [0, 50], [200,150], 29)
pygame.draw.line(screen, BLUE, [0, 50], [200, 150], 1)
#width 값이 짝수인 경우
pygame.draw.line(screen, GREEN, [200, 50], [400, 150], 30)
pygame.draw.line(screen, BLUE, [200, 50], [400, 150], 29)
pygame.draw.line(screen, GREEN, [400, 50], [500,250], 30)
pygame.draw.line(screen, BLUE, [400, 50], [500, 250], 29)
pygame.display.flip()
pygame.qu1t()
마우스 클릭하여 선그리기
import pygame
#from pygame.locals import*
from sys import e×it
from random import *
pygame.init()
BLACK= ( 0, 0, 0)
BLUE = ( 0, 0, 255)
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption('마우스로 클릭하여 선 그리기')
screen.fill((255, 255, 255))
clock= pygame.time.Clock()
line_points = []
running = True
while running:
clock.tick(30)
for event in pygame.event.get( ):
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
line_points.append(event.pos)
for point in line_points:
pygame.draw.circle(screen, BLACK, point, 5)
if len(line_points)) > 0 and (len(line_points) % 2 == 0):
pygame.draw.line(screen, BLUE, line_points[len(line_points)-2], line_points[len(line_points)-1], 2)
pygame.display.update()
pygame.display.quit()
import pygame
pygame.init( )
#색 설정
BLACK= (0, 0, 0)
WHITE= (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
#화면설정
size = [600, 300]
screen = pygame.display.set_mode(size)
pygame.display.set_caption(”선 그러기∼∼∼”)
#메인루프
running = True
clock = pygame.time.Clock( )
while running:
clock.tick(10)
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT: # 〈닫기〉 버튼을 클릭하면
running = False # 루프 탈출을 위해 변수 값 설정
#width 값이 홀수인 경우
pygame.draw.line(screen, GREEN, [0, 50], [200,150], 29)
pygame.draw.line(screen, BLUE, [0, 50], [200, 150], 1)
#width 값이 짝수인 경우
pygame.draw.line(screen, GREEN, [200, 50], [400, 150], 30)
pygame.draw.line(screen , BLUE, [200, 50], [400, 150], 29)
pygame.draw.line(screen, GREEN, [400, 50], [500,250], 30)
pygame.draw.line(screen, BLUE, [400, 50], [500, 250], 29)
pygame.display.flip( )
pygame.quit( )
import pygame
from pygame.locals import *
from random import *
pygame.init( )
BLACK= ( 0, 0, 0)
BLUE = ( 0, 0, 255)
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption(”마우스로 클릭하여 선 그리기”)
screen.fill((255, 255, 255))
clock= pygame.time.Clock( )
line_points = []
running = True
while running:
for event in pygame.event.get( ):
if event.type== QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
line_points.append(event.pos)
for point in line_points:
pygame.draw.circle(screen, BLACK, point, 5)
if len(line_points) > 0 and (len(line_points) % 2 == 0):
pygame.draw.line(screen, BLUE, line_points[len(line_points)-2], line_points[len(line_points)-1], 2)
pygame.display.update()
clock.tick(30)
pygame.quit()
lines
pygame.draw.lines(surface, color, closed, pointlist, width=1)
- surface : 도형을 그릴 창
- color : 색
- closed : 선들이 닫힌 도형을 만들면 True, 아니면 False
- pointlist : 점들의 리스트 [[ x1, y1] , [x2, y2], ... ]
- width : 선의 굵기
\# pg_draw_lines.py
import pygame
pygame.init()
screen = pygame.display.set_mode((500,400))
clock = pygame.time.Clock()
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.lines(screen,black,False,[[50,100],[150,100],[200,250],[250,150]],5)
#screen.blit(p_img, (x,y))
pygame.display.flip()
#pygame.display.update()
pygame.quit()
여러 개의 선을 그린다.
import pygame
from pygame.locals import *
pygame.init( )
#색설정
BLACK= ( 0, 0, 0)
WHITE= (255, 255, 255)
BLUE = ( 0, 0, 255)
lines_points = []
#화면설정
size = [500, 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
if event.type == MOUSEBUTTONDOWN # 마우스를 클록/하먼
lines_points.append(event.pos) # 점 목록 추가
screen.fill(WHITE)
for point in lines_points:
pygame.draw.circle(screen, BLACK, point, 5)
if len(lines_points) >= 2: #선그리기
pygame.draw.lines(screen, BLUE, False, lines_points, 4)
pygame.display.flip( )
pygame.quit( )
마우스를 클릭하면 선을 그린다.
import pygame
pygame.init()
#색설정
BLACK= ( 0, 0, 0)
WHITE= (255, 255, 255)
BLUE = ( 0, 0, 255)
lines_points = []
#화면설정
size = (500, 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
if event.type == pygame.MOUSEBUTTONDOWN:
lines_points.append(event.pos)
screen.fill(WHITE)
for point in lines_points:
pygame.draw.circle(screen, BLACK, point, 5)
if len(lines_points) >= 2:
pygame.draw.lines(screen, BLUE, False, lines_points, 4)
pygame.display.flip()
pygame.quit()
aaline (anti-aliased)
부드러운 선
pygame.draw.aaline(surface, color, start, end, blend = True)
- surface : 도형을 그릴 창
- color : 색
- start : 시작 점
- end : 끝 점
- blend : True, pygame 2.2부터는 True만 존재한다.
부드러운 선은 선두께가 1로 고정되어 있다.
import pygame
import numpy as np
pygame.init()
screen = pygame.display.set_mode((500,400))
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
pi = np.pi
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.line(screen,black,(50,50),(300,300))
pygame.draw.aaline(screen,black,(60,40),(350,250))
pygame.display.update()
pygame.quit()
aalines
pygame.draw.aalines(surface, color, closed, [start, end] )
- surface : 도형을 그릴 창
- color : 색
- closed :
- start : 시작 점
- end : 끝 점
import pygame
import numpy as np
pygame.init()
screen = pygame.display.set_mode((500,400))
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
pi = np.pi
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.line(screen,black,(50,50),(300,300))
pygame.draw.aalines(screen,black,True, [(60,40),(350,250)])
pygame.display.update()
pygame.quit()
로그인 하면 댓글을 쓸 수 있습니다.