-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (148 loc) · 4.81 KB
/
main.py
File metadata and controls
188 lines (148 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
import os
import sys
import pygame
import pygame_textinput
import shortest_path as sp
import json
from pygame.locals import *
import threading
# 초당 프레임수를 정의
TARGET_FPS = 5
clock = pygame.time.Clock()
MIDDLE = 0
LEFT = 1
RIGHT = 2
# 색 정의
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 라이브러리 및 디스플레이 초기화
pygame.init()
screenWidth = 1900#1680
screenHeight = 1024#1080
screen = pygame.display.set_mode((screenHeight, screenWidth))#, FULLSCREEN | DOUBLEBUF)
# 텍스트 좌표
textPointCurStation = (565, 100) # 현재 역 550
textPointCurStaNumber = (348, 100) # 현재 역 550
textPointPrevStation = (320, 400) # prev
textPointNextStation = (710, 400) # next
# 폰트 로딩 및 텍스트 객체 초기화
fontObj = pygame.font.Font("font/D2Coding.ttc", 80)
fontMediumObj = pygame.font.Font("font/D2Coding.ttc", 56)
fontSmallObj = pygame.font.Font("font/D2Coding.ttc", 32)
fp = open('datas/data.json', 'r')
initData = json.loads(fp.read())
fp.close()
initStation = initData["curStation"]
initLine = initData["line"]
initDirection = initData["direction"]
prevStation = '의왕'
nextStation = '화서'
direction = MIDDLE
isShown = False
def main():
# 메인 루프
global isShown
global direction
while True:
events = pygame.event.get()
screen.fill(BLACK) # 화면을 검은색으로 지운다
for event in events:
# 이벤트를 처리하는 부분
if event.type == QUIT:
pygame.quit()
sys.exit()
# 키보드 이벤트 처리
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit()
elif event.key == K_p:
direction = RIGHT
elif event.key == K_q:
direction = LEFT
elif event.key == K_m:
isShown = True
direction = sp.speak_destination(initStation, initLine, initDirection)
else:
direction = MIDDLE
################ Handle Events ###################
#start_timer()
if isShown and (direction != MIDDLE):
isShown = False
start_timer()
if direction == LEFT:
render_go_left_image()
elif direction == RIGHT:
render_go_right_image()
# custom 메소드 호출, textinput 그려준다.
# render_text(events) # 100 line
# render_arrow()
render_base_images();
message_to_screen(initStation, WHITE, textPointCurStation, 0)
message_to_screen(nextStation, WHITE, textPointNextStation, 2)
message_to_screen(prevStation, WHITE, textPointPrevStation, 2)
message_to_screen("153", WHITE, textPointCurStaNumber, 1)
pygame.display.flip() # 화면 전체를 업데이트
clock.tick(TARGET_FPS) # 프레임 수 맞추기
_image_library = {}
def get_image(path, angle):
global _image_library
image = _image_library.get(path)
if image == None:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
_image_library[path] = image
return pygame.transform.rotate(image, angle)
leftArrow = (100, 600)
leftAngle = 0
rightArrow = (709, 600)
rightAngle = 180
middleMic = (250, 600)
#posCircle = (screenHeight/2, screenWidth/2)
posBlank = (300, 50)
posStationName = (230, 220)
posCircle = (230, 320)
posTellDestination = (330, 450)
posMic = (479, 615)
def render_go_right_image():
screen.blit(get_image('images/gray.png', leftAngle), leftArrow)
screen.blit(get_image('images/red.png', rightAngle), rightArrow)
def render_go_left_image():
screen.blit(get_image('images/red.png', leftAngle), leftArrow)
screen.blit(get_image('images/gray.png', rightAngle), rightArrow)
def render_base_images():
screen.blit(get_image('images/blank_circle.png', 0), posBlank)
screen.blit(get_image('images/sta_name.png', 0), posStationName)
screen.blit(get_image('images/circle.png', 0), posCircle)
screen.blit(get_image('images/text_tell_destination.png', 0), posTellDestination)
screen.blit(get_image('images/mic.png', 0), posMic)
def text_objects(text, color, size): #size는 작을수록 크다.
if size == 0:
textSurface = fontObj.render(text, True, color)
elif size == 1:
textSurface = fontMediumObj.render(text, True, color)
else:
textSurface = fontSmallObj.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_to_screen(msg, color, p, isCurrent):
textSurf, textRect = text_objects(msg, color, isCurrent)
textRect.center = (p[0], p[1])
screen.blit(textSurf, textRect)
count = 0
def start_timer():
global count
global direction
count += 1
print(str(count)+ ":direction-" + str(direction))
timer=threading.Timer(1, start_timer)
if count < 5:
timer.start()
else :
count = 0
direction = MIDDLE
timer.cancel()
if __name__ == "__main__":
main()