제작 의도
-학교폭력 가해학생과 피해학생을 떨어트려 놓기
-단 가해학생과 피해학생을 비롯한 다른 학생들 모두 눈치채지 못하게.
필요한 패키지
-pygame : 화면 상에 글자나 숫자를 원하는 위치에 띄우기 좋아서 선택
-random : 자리 배정에 필요한 랜덤 기능
-openpyxl : 자리배치도를 엑셀파일로 쓰기 위함.
초기 설정
-스크린 크기, 폰트, 타이틀, ‘교탁’ 글자 등의 위치, 색상 등을 아래와 같이 정하면 초기 설정이 완료된다.
코드 보기
# Initialization
pygame.init()
# Setting Screen
screen_width = 900
screen_height = 600
screenSize = (screen_width, screen_height)
gameScreen = pygame.display.set_mode((screen_width,screen_height))
# Setting Title
pygame.display.set_caption("Random Seat Algorithm")
# Font
font = pygame.font.SysFont("arial", 32)
titleFont = pygame.font.SysFont("malgungothic", 32, True)
tableFont = pygame.font.SysFont("malgungothic", 22)
authorFont = pygame.font.SysFont("malgungothic", 18)
# Text
titleText = titleFont.render("좌석 배정 프로그램", True, (0,0,255))
tableText = tableFont.render("교탁", True, (0,0,0))
authorText = authorFont.render("종료 후 print.xlsx 열면 자리배치도 출력가능. 제작: chamingyung@outlook.com(차민경)", True, (0,155,0))
# Position
titlePosition = (screenSize[0] // 2 - titleText.get_width() // 2, 9)
tablePosition = (screenSize[0]//2 - tableText.get_width()//2, 55)
authorPosition = (screenSize[0] // 2 - authorText.get_width() // 2 , screenSize[1] -50 ) 학급 인원수 받기
사용자에게 프로그램을 간략히 설명하고 학급 인원수를 입력받는다.
# Setting student nums, position nums
size = int(input("""
---------------------------------------------------------------------------
이 프로그램은 학급 좌석을 랜덤으로 배치하되, 두세 명의 학생의 간격을 떨어트리는 프로그램입니다.
예를 들어, 한 학급에 학폭사건에 연루된 학생이 두세 명 있으면 떨어트릴 수 있습니다.
---------------------------------------------------------------------------
학급 인원수를 입력하세요(27이상30이하)(예: 28) :"""))
numbers = list(range(1,size+1)) # 1 ~ size
[책상을 학번 순으로 배열하기]
학급 인원수에 맞추어 책상을 총 3개 분단으로 배치하려고 한다. 그러기 위해 각 책상의 위치를 positions 라는 리스트에 차곡차곡 담는다.
positions = []
# 1st Col
for i in range(5) :
for j in range(2) :
positions.append((100+j*100, 100+i*100))
# 2nd Col
for i in range(5) :
for j in range(2) :
positions.append((400+100*j, 100+i*100))
# 3rd Col
if size == 27 or size == 28 :
for i in range(4) :
for j in range(2) :
positions.append((700+100*j, 100+100*i))
elif size == 29 or size == 30 :
for i in range(5) :
for j in range(2) :
positions.append((700+100*j, 100+100*i))
[떨어트리려는 학생 번호 입력받기]
누구를 떨어트리는 기능 없이 그냥 랜덤 자리 배정을 하는 경우가 일반적이므로, 떨어트리는 기능 없는 일반 랜덤 배정 모드, 특정 학생들을 최대 3명까지 떨어트리는 모드로 구분하여 입력받는다.
떨어트리는 모드 시, 떨어트리려는 학생을 번호로 입력받아 a, b, c 변수에 할당한다.
# Selecting 3 students
mode = input ("""
---------------------------------------------------------------------------
떨어트리는 기능 없이 그냥 랜덤으로 돌리시려면 알파벳 n을,
학생을 떨어트리려면 알파벳 y를 입력하세요 (y 또는 n) : """)
if mode == 'n' :
pass
elif mode == 'y' :
sep = int(input("몇 명의 학생을 분리할까요(2 또는 3) : "))
if sep == 2 :
a = int(input("첫 번째 학생의 번호는? (예: 24) : "))
b = int(input("두 번째 학생의 번호는? (예: 25) : "))
elif sep == 3 :
a = int(input("첫 번째 학생의 번호는? (예: 24) : "))
b = int(input("두 번째 학생의 번호는? (예: 25) : "))
c = int(input("세 번째 학생의 번호는? (예: 27) : "))
[루프 얼개 짜기]
루프 얼개를 대강 아래와 같이 짠다.
running = True
while running :
# Screen Color
gameScreen.fill((255,255,255))
# Dealing Events
for event in pygame.event.get() :
# Terminating
if event.type == pygame.QUIT :
running = False
if event.type == pygame.KEYDOWN :
if mode == 'n' :
pass
elif mode == 'y' :
if sep == 2 :
pass
else : pass
if event.key == pygame.K_RETURN :
# Shuffering nums except a, b, c
# Determinating a, b, c in 1st col or 3rd col
# Calculating Index of a, b, c
# Inputting indexes of a,b,c into numbers list
# Showing title, author
gameScreen.blit(titleText, titlePosition)
gameScreen.blit(tableText, tablePosition)
gameScreen.blit(authorText, authorPosition)
# Matching nums and coordinates
# Updating Screen
pygame.display.flip()
# Terminating
pygame.quit()
작성중…..
[완성]
이제 프로그램을 실행하면 아래와 같은 화면에서 Enter를 누를 때마다 자리가 랜덤하게 섞이면서 지정 학생들의 자리가 항상 떨어지는 것을 볼 수 있다.
예를 들어, 1,2,3번 학생을 떨어트린다고 설정했다면 Enter를 누를 때마다 아래와 같이 1,2,3번 학생들의 자리는 계속 떨어진 채 배정됨을 볼 수 있다.
이 프로그램은 현재 exe파일로, 즉 오프라인용으로 제작되었다. 2025년에 streamlit을 배우고 나면 웹에서 이를 실행할 수 있도록 수정할 예정이다.
댓글 남기기