728x90
반응형

1. 개요
 
- 돌림판에 사용될 원 그리기


2. 코드

# -*- coding: utf-8 -*-
import sys
import pygame
import math
from pygame.locals import *

PI = 3.14159265359
R = 200
screenX = 410 
screenY = 410
Ox = screenX / 2
Oy = screenY / 2

# 초당 프레임수를 정의
TARGET_FPS = 30
clock = pygame.time.Clock()

# 색 정의
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)

# 실수를 더하기 위해서 만들어줌
def myrange(start, end, step):
    r = start
    while(r<end):
        yield r
        r += step

# 인원 수 를 입력받아서 같은 각도로 나눠줌
def auto_line(size):
  a = 360.0 / size # 1명 각도
  # cnt = 0
  tmp = 0 # 각도 누적
  x = 0.0
  y = 0.0
  
  for tmp in myrange(0, 360, a):
      # cnt = cnt + 1

      # 간단한 수학 공식으로 좌표 Get
      x = R * math.cos(math.radians(tmp))
      y = R * math.sin(math.radians(tmp))
      
      #print "[%d] tmp : %.2lf, x : %lf, y : %lf" %(cnt, tmp, x, y)
      pygame.draw.line(screen, BLACK, (Ox, Oy), (Ox+x, Oy+y), 2)


# 라이브러리 및 디스플레이 초기화
pygame.init()

num = raw_input("num : ")
screen = pygame.display.set_mode((screenX, screenY), DOUBLEBUF)
pygame.display.set_caption('Hello World!')  # 타이틀바의 텍스트를 설정

screen.fill(WHITE)
#pygame.draw.line(screen, RED, (Ox, Oy-R*10), (Ox, Oy+R*10), 1) # y
#pygame.draw.line(screen, RED, (Ox-R*10, Oy), (Ox+R*10, Oy), 1) # x

# 메인 루프
while True:
  for event in pygame.event.get():
    # 이벤트를 처리하는 부분
    if event.type == QUIT:
      pygame.quit()
      sys.exit()

  auto_line(int(num)) # 입력받은 인원 수로 원을 나눠줌
  pygame.draw.circle(screen, BLACK, (Ox, Oy), R, 2) # 외부 원
  pygame.draw.circle(screen, BLACK, (Ox, Oy), 20) # 중심 원
  pygame.display.flip()  # 화면 전체를 업데이트
  clock.tick(TARGET_FPS)  # 프레임 수 맞추기

 

생성된 돌림판 이미지 - Alt+Prtsc 으로 캡쳐 후 사용

 

 

728x90
반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python] FinanceDataReader 모듈  (0) 2021.07.31
[Python] pykrx 모듈  (0) 2021.05.27
[Python] yfinance 모듈  (0) 2021.05.27
[Python] pycryptodome 모듈  (0) 2020.09.26
[Python] 돌림판  (0) 2016.10.03
[Python] 키로거 + 스크린샷  (0) 2016.09.18
728x90
반응형

1. 개요

 - pygame 라이브러리를 사용하여 윈도우를 그림

 - 스페이스 바로 돌림판의 회전과 정지를 정함

 - 정지 시 천천히 멈춤 (회전각을 0까지 빼줌)

 

2. 코드

 
#-*- coding: utf-8 -*-
import sys
import pygame
from pygame.locals import *
import time

# 초당 프레임수를 정의
TARGET_FPS = 30
clock = pygame.time.Clock()

# 색 정의
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)

# 라이브러리 및 디스플레이 초기화
pygame.init()
screen = pygame.display.set_mode((600, 400), DOUBLEBUF) # 윈도우 크기 및 속성 설정
pygame.display.set_caption('돌림판')  # 타이틀바의 텍스트를 설정

# 이미지 파일을 로딩
img = pygame.image.load('images.png') # 돌림판 이미지
img2 = pygame.image.load('images2.jpg') # 화살표

degree = 0
flag = True 
rad = 100
stop_time = 3 # 돌림판이 멈출 때까지의 시간 (클수록 빨리 멈춤)

# 메인 루프
while True:
  for event in pygame.event.get():
    # 이벤트를 처리하는 부분
    if event.type == QUIT:
      pygame.quit()
      sys.exit()

    # 키보드 이벤트 처리
    if event.type == KEYDOWN:
      # 스페이스 -> 토글
      if event.key == 32:
          if flag == True:
              flag = False
          elif flag == False:
              flag = True
              rad = 100

  # 게임의 상태를 화면에 그려주는 부분
  screen.fill(WHITE)
  
  # 이미지 파일 회전하여 그리기
  x = 350
  y = 200
  
  rotated = pygame.transform.rotate(img, degree) # degree 만큼 회전
  rect = rotated.get_rect()
  rect.center = (x, y)
  screen.blit(rotated, rect)

  # 플래그의 상태가 True면 회전, False면 천천히 정지
  if flag == True:
      degree += rad
  elif flag == False:
      if rad > 0:
          rad -= stop_time
          degree += rad
          

  screen.blit(img2, (70, 180)) # 화살표 그리기
  pygame.display.flip()  # 화면 전체를 업데이트
  clock.tick(TARGET_FPS)  # 프레임 수 맞추기

 

3. 결과

 

 

728x90
반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python] FinanceDataReader 모듈  (0) 2021.07.31
[Python] pykrx 모듈  (0) 2021.05.27
[Python] yfinance 모듈  (0) 2021.05.27
[Python] pycryptodome 모듈  (0) 2020.09.26
[Python] 원 나누기  (0) 2016.10.03
[Python] 키로거 + 스크린샷  (0) 2016.09.18

+ Recent posts