import pygame, sys from pygame.locals import * pygame.init() # set up the windowDISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32) pygame.display.set_caption('Drawing') # set up the colorsBLACK = ( 0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) # draw on the surface objectDISPLAYSURF.fill(WHITE) pygame.draw.polygon(DISPLAYSURF, GREEN, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106))) pygame.draw.line(DISPLAYSURF, BLUE, (60, 60), (120, 60), 4) pygame.draw.line(DISPLAYSURF, BLUE, (120, 60), (60, 120)) pygame.draw.line(DISPLAYSURF, BLUE, (60, 120), (120, 120), 4) pygame.draw.circle(DISPLAYSURF, BLUE, (300, 50), 20, 0) pygame.draw.ellipse(DISPLAYSURF, RED, (300, 250, 40, 80), 1) pygame.draw.rect(DISPLAYSURF, RED, (200, 150, 100, 50)) pixObj = pygame.PixelArray(DISPLAYSURF) pixObj[480][380] = BLACK pixObj[482][382] = BLACK pixObj[484][384] = BLACK pixObj[486][386] = BLACK pixObj[488][388] = BLACK del pixObj while True: # main game loopfor event in pygame.event.get():if event.type == QUIT:pygame.quit() sys.exit() pygame.display.update()
fill(color) : color 색깔로 화면 전체를 채운다.
pygame.draw.polygon(surface, color, pointlist, width) : 오각형을 그린다. 어떤 표면 위에 어떤 색으로, 어떤 점에서 찍을 지 인자를 전달해주면 된다. width는 옵션으로, 인자를 전달하면 오각형의 외곽선을 나타내며, 인자를 전달하지 않으면 내부가 color로 채워진다.
pygame.draw.line(surface, color, start_point, end_point, width) : 선분을 그린다. polygon 함수와 유사하며, 시작점과 끝점을 인자로 받는다.
pygame.draw.lines(surface, color, closed, pointlist, width) : 선분들을 그린다. pointlist가 선분들의 한 점이 되며, closed가 True인 경우 맨 마지막 점과 처음 점을 잇는 선분이 그려지고, False일 경우 그렇지 않다.
pygame.draw.circle(surface, color, center_point, radius, width) : 원을 그린다. 중심과 반지름을 인자로 받는다.
pygame.draw.ellipse(surface, color, bounding_rectangle, width) : 타원을 그린다. bounding_rectangle에 rectangle_tuple을 전달하면 그 직사각형에 내접하는 타원을 그린다.
* rectangle_tuple : ( x pos, y pos, width, height )
pygame.draw.rect(surface, color, rectangle_tuple, width) : 직사각형을 그린다.
pygame.PixelArray(surface)를 이용해 surface의 픽셀을 변경시킬 수 있다. 수행하려는 작업이 끝나면 del을 이용해 지워서 unlock을 해 줘야 한다. 그렇지 않을 경우, blit()을 이용해 변경된 픽셀을 변경하려 할 때 오류가 발생한다.
댓글 없음:
댓글 쓰기