ai-creator

[파이썬 간단한 게임 만들기] 0. 기본기 - 도형그리기 본문

유치한 게임

[파이썬 간단한 게임 만들기] 0. 기본기 - 도형그리기

ai-creator 2021. 3. 6. 08:26
반응형

1. 기본 게임판

import pygame # 1. pygame 선언
import random

pygame.init() # 2. pygame 초기화

# 3. pygame에 사용되는 전역변수 선언

WHITE = (255, 255, 255)
size = [400, 400]
screen = pygame.display.set_mode(size)

done = False
clock = pygame.time.Clock()

# 4. pygame 무한루프
def runGame():
    global done
    while not done:
        clock.tick(10)
        screen.fill(WHITE)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done=True

        ############################
        # 여기에 도형을 그리세요
        ############################
        
        
        pygame.display.update()

runGame()
pygame.quit()

2. 도형 그리기

www.pygame.org/docs/ref/draw.html

1) 사각형

<코드-1>

# where = screen, color=(0, 0, 255), rect = (x,y,width,height)
pygame.draw.rect(screen, (0, 0, 255), (0, 0, 200, 100))

<결과-1>

<코드-2>

# where = screen, color=(0, 0, 255), rect = (x,y,width,height), width = 굵기
pygame.draw.rect(screen, (0, 0, 255), (0, 0, 200, 100), 1)

<결과-2> 

2) 원

<코드>

# where = screen, color=(0,0,255), 중심좌표(x,y)=(100,200), radius(반지름)=30
pygame.draw.circle(screen, (0,0,255), (100, 200), 30)

<결과>

<코드>

pygame.draw.circle(screen, (0,0,255), (100, 200), 30, 1)
pygame.draw.circle(screen, (0,0,255), (160, 200), 30, 2)
pygame.draw.circle(screen, (0,0,255), (220, 200), 30, 3)

<결과>

 

3) 선

<구현>

pygame.draw.line(screen, (255, 0, 0), (0, 0), (200, 0))
pygame.draw.line(screen, (255, 0, 0), (0, 20), (200, 20), 3)
pygame.draw.line(screen, (255, 0, 0), (0, 40), (200, 40), 6)

<결과>

 

3. 격자무늬 그리기

3x3의 격자무늬를 만들어보세요.

 

쉬운버전

더보기
#가로선
pygame.draw.line(screen, (255, 0, 0), (0, 0), (60, 0), 3)
pygame.draw.line(screen, (255, 0, 0), (0, 20), (60, 20), 3)
pygame.draw.line(screen, (255, 0, 0), (0, 40), (60, 40), 3)
pygame.draw.line(screen, (255, 0, 0), (0, 60), (60, 60), 3)

#세로선
pygame.draw.line(screen, (255, 0, 0), (0, 0), (0, 60), 3)
pygame.draw.line(screen, (255, 0, 0), (20, 0), (20, 60), 3)
pygame.draw.line(screen, (255, 0, 0), (40, 0), (40, 60), 3)
pygame.draw.line(screen, (255, 0, 0), (60, 0), (60, 60), 3)

for문 버전

더보기
num_of_lines=3
gap = 20
# 가로선
for y_idx in range(num_of_lines+1):
	y_pos = y_idx * gap
	pygame.draw.line(screen, (255, 0, 0), (0, y_pos), (gap*num_of_lines, y_pos), 3)

# 세로선
for x_idx in range(num_of_lines+1):
	x_pos =(x_idx) * gap
	pygame.draw.line(screen, (255, 0, 0), (x_pos, 0), (x_pos, gap*num_of_lines), 3)

4. 신호등 만들기

더보기
pygame.draw.circle(screen, (255,0,0), (100, 100), 30)
pygame.draw.circle(screen, (255,255, 0), (100, 160), 30)
pygame.draw.circle(screen, (0,255,0), (100, 220), 30)
pygame.draw.rect(screen, (0, 0, 0), (40, 40, 120, 240), 1)
pygame.draw.line(screen, (0, 0, 0), (100, 280), (100, 380), 1)

 

5. 과제

금일 수업에서 격자무늬를 그리는 방법을 배워보았습니다.

gird값을 설정할 수 있도록 프로그래밍 해보세요.

gird=(3,4)면 3x4의 격자가

gird=(2,5)면 2x5의 격자를 그릴 수 있는 프로그램입니다.

더보기
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import pygame  # 1. pygame 선언
import random

pygame.init()  # 2. pygame 초기화

# 3. pygame에 사용되는 전역변수 선언
WHITE = (255, 255, 255)
RED = (255, 0, 0)

size = [400, 400]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()

offs = 30   # 격자 크기
thick = 3   # 선굵기

def drawGrid(m, n):
    '''
    :param m: 격자무늬 행 개수
    :param n: 격자무늬 열 개수
    '''
    x = 0
    y = 0
    for j in range(0, m+2):
        pygame.draw.line(screen, RED, (x, y), (x + n * offs, y), thick)
        y = j * offs
    x = 0
    y = 0
    for i in range(0, n+2):
        pygame.draw.line(screen, RED, (x, y), (x, y + m * offs), thick)
        x = i * offs

# 4. pygame 무한루프
def runGame():
    global done
    while not done:
        clock.tick(10)
        screen.fill(WHITE)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        ############################
        # 여기에 도형을 그리세요
        ############################
        drawGrid(3, 4)
        #drawGrid(2, 5)

        pygame.display.update()

runGame()
pygame.quit()

 

ㅁ Reference

- 그림 : https://bournetocode.com/projects/9-CS-pyGame/pages/1_Lesson.html

 

반응형
Comments