Lecture 사각형 그리기

Lecture • Views 111 • Comments 0 • Last Updated at 3 weeks ago  
  • 도형그리기
  • 사각형 그리기

사각형 그리기

pygame.draw.rect(surface, color, rect, width=0)
  • surface : 도형을 그릴 창
  • color : 색
  • rect : [ x, y, width, height ] 의 리스트 혹은 튜플
  • width : 선의 굵기, 0이면 닫힌 도형안을 채워준다.
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.rect(screen,black,[10, 10, 100, 150], 10)
    pygame.display.flip()
    #pygame.display.update()
pygame.quit()
import pygame
pygame.init( )

#RGB 형식으로 색 설정
BLUE = (0, 0, 255)
RED= (255, 0, 0)
WHITE = (255, 255, 255)

#화면크기설정
size = [500, 300]
screen = pygame.display.set_mode(size)
pygame.display.set_caption(” 사각형 그리기 ”)

# 〈닫기〉 버튼을 클릭할 때까지 반복하는 루프
running = True
clock= pygame.time.Clock( )

while running:
    # 1초에 10번루프 반복
    clock.tick(10)
    for event in pygame.event.get( ):
        if event.type == pygame.QUIT: 
            runnig = False  # 루프를 빠져나갈 수 있도록 변수 설정

    #흰색으로배경그리기
    screen.fill(WHITE)
    
    #사각형그리기 채워지지않은사각형
    pygame.draw.rect(screen, BLUE, [50, 50, 100, 100], 3)
    # 사각형그리기 - 채워진사각형
    pygame.draw.rect(screen, RED, [200, 50, 100, 100] )
    pygame.display.flip( )
pygame.quit( )

사각형 색 바꾸기

import pygame
from pygame.locals import *
from random import *

pygame.init( )
WHITE = (255, 255, 255)
WIDTH = 680
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
running = True
clock = pygame.time.Clock( )

while running:
    clock. tick(1)
    for event in pygame.event.get( ):
        if event.type == QUIT:
            running = False
    for count in range(30):
        random_color = (randint(0,255), randint(0,255), randint(0,255))
        pygame.draw.rect(screen, random_color, [50, 50, WIDTH - 100, HEIGHT - 100])
    pygame.display.update( )
pygame.quit( )
import pygame
from pygame.locals import *
from random import *

pygame.init( )
WHITE = (255, 255, 255)
WIDTH = 680
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
running = True
clock = pygame.time.Clock( )

raw = 2
column = 2
d_width = (WIDTH - 100)//raw
d_height = (HEIGHT - 100)//column

while running:
    clock. tick(1)
    for event in pygame.event.get( ):
        if event.type == QUIT:
            running = False
    for i in range(raw):
        for j in range(column):
            random_color = (randint(0,255), randint(0,255), randint(0,255))
            pygame.draw.rect(screen, random_color, [50+(i*d_width), 50+(j*d_height), d_width, d_height])
    pygame.display.update( )
pygame.quit( )

import pygame
from pygame.locals import *
from random import *

pygame.init( )
WHITE = (255, 255, 255)
WIDTH = 680
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
running = True
clock = pygame.time.Clock( )

raw = 5
column = 5
d_width = (WIDTH - 100)//raw
d_height = (HEIGHT - 100)//column
random_col = []
for i in range(raw):
    for j in range(column):
        random_col.append((255,255,255))
        
while running:
    clock. tick(10)
    for event in pygame.event.get( ):
        if event.type == QUIT:
            running = False
            
    random_col.pop()
    random_col.insert(0, (randint(0,255), randint(0,255), randint(0,255)))

    for i in range(raw):
        for j in range(column):
            pygame.draw.rect(screen, random_col[i*(column)+j], [50+(i*d_width), 50+(j*d_height), d_width, d_height])
    pygame.display.update( )
pygame.quit( )
previous article
next article
Comments
Feel free to ask a question, answer or comment, etc.