-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTutorialPage.py
244 lines (189 loc) · 7.99 KB
/
TutorialPage.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import pygame
import os
import Pygame_Utils as utils
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
info = pygame.display.Info()
SCREEN_WIDTH = info.current_w
SCREEN_HEIGHT = info.current_h
screen = pygame.display.set_mode((SCREEN_WIDTH - 10,SCREEN_HEIGHT- 50),pygame.RESIZABLE)
pygame.display.set_caption('Learn')
INNER_SCREEN_WIDTH = SCREEN_WIDTH * 0.8
TUTORIAL_SECTION_WIDTH = INNER_SCREEN_WIDTH * 0.7
TUTORIAL_SECTION_PADDING = 80
back_btn = None
arrowU = None
arrowD = None
completedTick = None
try:
back_btn = pygame.image.load(r"Images\undo.png")
arrowU = pygame.image.load(r"Images\arrow_up.png")
arrowD = pygame.image.load(r"Images\arrow_down.png")
completedTick = pygame.image.load(r"Images\completed.png")
except:
back_btn = pygame.image.load(r"Images/undo.png")
arrowU = pygame.image.load(r"Images/arrow_up.png")
arrowD = pygame.image.load(r"Images/arrow_down.png")
completedTick = pygame.image.load(r"Images/completed.png")
exitButton = utils.Button(20,SCREEN_HEIGHT * 0.8,back_btn,0.25)
arrowButtonUp = utils.Button(SCREEN_WIDTH * 0.82,SCREEN_HEIGHT * 0.08,arrowU,0.35)
arrowButtonDown = utils.Button(SCREEN_WIDTH * 0.82,SCREEN_HEIGHT * 0.8,arrowD,0.35)
allSections = []
sectionButtons = []
def Start():
allSections.clear()
sectionButtons.clear()
running = True
numberOfSections = 7
index = 0
inTutorial = False
inPractice = False
clicked = False
buttonSelectedIndex = -1
completedSections = getUserTutorialProgress()
fontSize = 64
font = pygame.font.Font(None, fontSize)
getTutorialSections()
getSectionRects(numberOfSections)
while running:
screen.fill((219, 200, 167))
if not inTutorial:
pygame.draw.rect(screen,(206,187,155),((SCREEN_WIDTH - INNER_SCREEN_WIDTH) // 2,0,INNER_SCREEN_WIDTH,SCREEN_HEIGHT))
if arrowButtonUp.drawButton(screen):
if index - 1 >= 0:
index = index - 1
if arrowButtonDown.drawButton(screen):
if index + 7 < len(allSections):
index = index + 1
if not clicked:
buttonSelectedIndex = drawButtons(numberOfSections,index,completedSections)
if buttonSelectedIndex > -1:
clicked = True
inTutorial = True
else:
inner = pygame.draw.rect(screen,(206,187,155),((SCREEN_WIDTH - INNER_SCREEN_WIDTH) // 2,0,INNER_SCREEN_WIDTH,SCREEN_HEIGHT))
surface = font.render(allSections[buttonSelectedIndex][0], True, (0,0,0))
rect = surface.get_rect(center=(inner.midtop[0],inner.midtop[1] + 64))
screen.blit(surface, rect)
renderTextCenteredAt(allSections[buttonSelectedIndex][1],font,(0,0,0),inner.centerx,inner.centery,inner.width - 30,fontSize)
play_button = pygame.Rect((SCREEN_WIDTH - 200) // 2, SCREEN_HEIGHT * 0.85 - 50, 250, 50)
newButton = pygame.draw.rect(screen,(107, 70, 44),play_button)
surface = font.render("Practice", True, (255,255,255))
rect = surface.get_rect(center=newButton.center)
screen.blit(surface, rect)
pos = pygame.mouse.get_pos()
if newButton.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not inPractice:
inPractice = True
import GamePlayBoardPage
GamePlayBoardPage.drawBoard(allSections[buttonSelectedIndex][2])
inPractice = False
completedSections = completedSections + [allSections[buttonSelectedIndex][0]]
saveSections(completedSections)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if exitButton.drawButton(screen):
if inTutorial:
inTutorial = False
clicked = False
buttonSelectedIndex = -1
else:
running = False
pygame.display.update()
def getSectionRects(sectionsToDisplay):
tutorialSectionHeight = 100
for i in range(sectionsToDisplay):
startY = tutorialSectionHeight * i
buttonRect = pygame.Rect((SCREEN_WIDTH - TUTORIAL_SECTION_WIDTH) // 2,startY * 1.2 + TUTORIAL_SECTION_PADDING,TUTORIAL_SECTION_WIDTH,tutorialSectionHeight)
sectionButtons.append(buttonRect)
def drawButtons(sectionsToDisplay,index,completed):
font = pygame.font.Font(None, 36)
for i in range(sectionsToDisplay):
button = sectionButtons[i]
newButton = pygame.draw.rect(screen,(245,245,245),button,border_radius=10)
surface = font.render(allSections[i+index][0], True, (0,0,0))
rect = surface.get_rect(center=button.center)
screen.blit(surface, rect)
pos = pygame.mouse.get_pos()
if allSections[i+index][0] in completed:
tickRect = completedTick.get_rect()
screen.blit(completedTick,(button.right - (tickRect.width * 1.5),button.centery - (tickRect.height// 2),tickRect.width,tickRect.height))
# is hovering over button
if newButton.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
clicked = True
print(allSections[i+index][0] + " Clicked")
return i+index
return -1
def getTutorialSections():
tutorialData = None
try:
tutorialData = open(r"Tutorial\Dialogue.txt","r")
except:
tutorialData = open(r"Tutorial/Dialogue.txt","r")
for section in tutorialData.readlines():
if section.count(':') != 0:
newSection = section.split(':') # array containing [title of section, text to display, board for demo]
allSections.append([newSection[0],newSection[1],newSection[2]])
tutorialData.close()
def renderTextCenteredAt(text, font, colour, x, y, allowed_width,fontSize = 64):
#reference https://stackoverflow.com/questions/49432109/how-to-wrap-text-in-pygame-using-pygame-font-font
# first, split the text into words
words = text.split()
# now, construct lines out of these words
lines = []
while len(words) > 0:
# get as many words as will fit within allowed_width
lineWords = []
while len(words) > 0:
lineWords.append(words.pop(0))
fw, fh = font.size(' '.join(lineWords + words[:1]))
if fw > allowed_width:
break
# add a line consisting of those words
line = ' '.join(lineWords)
lines.append(line)
# now we've split our text into lines that fit into the width, actually
# render them
# we'll render each line below the last, so we need to keep track of
# the culmative height of the lines we've rendered so far
i = 0
yOffset = fontSize
centeringOffset = (len(lines) * fontSize) // 2
for line in lines:
fw, fh = font.size(line)
# (tx, ty) is the top-left of the font surface
tx = x - fw / 2
ty = y + (yOffset * i) - centeringOffset
surface = font.render(line, True, colour)
screen.blit(surface, (tx, ty))
i = i + 1
def getUserTutorialProgress():
tutorialData = None
try:
tutorialData = open(r"Data\UserTutorialProgress.txt","r")
except:
tutorialData = open(r"Data/UserTutorialProgress.txt","r")
data = tutorialData.readline()
if data == "":
return []
else:
data = data.split(",")
tutorialData.close()
return data
def saveSections(completedSections):
# the program needs to write into tutorialData every string in the array completedSections
# shouldnt return any value
tutorialData = None
try:
tutorialData = open(r"Data\UserTutorialProgress.txt","w")
except:
tutorialData = open(r"Data/UserTutorialProgress.txt","w")
#---- code here -----
data = ""
for i in completedSections:
data = data + i + ","
tutorialData.write(data)
# -------------------
tutorialData.close()