-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvolume.py
More file actions
194 lines (152 loc) · 8.01 KB
/
Copy pathvolume.py
File metadata and controls
194 lines (152 loc) · 8.01 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
189
190
191
192
193
194
"""
volume.py - module defining classes displaying and handling volume menu
---------------------------------------------------------------------------
Copyright 2014 Alexandre Lopes <aalopes@ovi.com>
This file is part of CometZ.
CometZ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CometZ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CometZ. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
"""
from constants import *
import pygame
import pickle
class Volume():
"""
Class for generating volume menu based on
a background image and the current volume
It takes as input a background image and a dictionary containing the
current volume
We will assume that the only way the volume can be changed throughout the game
is through the class methods
If one needs to change the text size or color, one can make use of the
class attributes
"""
# Constructor
def __init__(self,backg,volume):
# Input
# items - list of strings (menu elements)
# backg - background
# volume - dictionary of the form {"sound": soundValue, "music": musicValue }
self.items = ["Back", "Sound Volume", "Music Volume"]
self.backg = backg
self.musicVol = volume["music"]
self.soundVol = volume["sound"]
self.state = len(self.items)-1 # currently selected menu item
# in this case there are only 3 states
# Default values
self.fontType = "Arial" # font type
self.fontSizeNSelec = 40 # font size for non selected items
self.fontSizeSelec = 50 # font size for selected items
self.spread = 80 # how far apart the lines are
self.fontColor = WHITE # text color
def up(self):
"""
moving up on the menu
"""
self.state += 1
# if we go way too up
if self.state > len(self.items) - 1:
self.state = len(self.items) - 1 # simply stay at the top item (we could also loop around...)
def down(self):
"""
moving down on the menu
"""
self.state -= 1
# if we go way too down
if self.state < 0:
self.state = 0 # simply stay at the bottom item (we could also loop around...)
def left(self):
"""
moving "left" on the menu
"""
if self.state == 2: # if we have the music volume selected
self.musicVol += -10 # increment
if self.musicVol < 0: # if it exceeds the minimum
self.musicVol = 0
# save new volume to file
pickle.dump( {"sound": self.soundVol,
"music": self.musicVol},
open( "volume.p", "wb" ) )
return self.musicVol/100. # return value so we can change volume in the game
elif self.state == 1: # if we have the sound volume selected
self.soundVol += -10 # increment
if self.soundVol < 0: # if it exceeds the minimum
self.soundVol = 0
# save new volume to file
pickle.dump( {"sound": self.soundVol,
"music": self.musicVol},
open( "volume.p", "wb" ) )
return self.soundVol/100. # return value so we can change volume in the game
def right(self):
"""
moving "right" on the menu
"""
if self.state == 2: # if we have the music volume selected
self.musicVol += 10 # increment
if self.musicVol > 100: # if it exceeds the maximum
self.musicVol = 100
# save new volume to file
pickle.dump( {"sound": self.soundVol,
"music": self.musicVol},
open( "volume.p", "wb" ) )
return self.musicVol/100. # return value so we can change volume in the game
elif self.state == 1: # if we have the sound volume selected
self.soundVol += 10 # increment
if self.soundVol > 100: # if it exceeds the maximum
self.soundVol = 100
# save new volume to file
pickle.dump( {"sound": self.soundVol,
"music": self.musicVol},
open( "volume.p", "wb" ) )
return self.soundVol/100. # return value so we can change volume in the game
def press(self):
"""
whenever the selected item is pressed
"""
return self.state
def draw(self,screen):
"""
drawing the menu
"""
# erase screen
screen.fill(BLACK)
# draw background
screen.blit(self.backg.image,(self.backg.x,self.backg.y))
# drawing text
fontNSelected = pygame.font.SysFont(self.fontType, self.fontSizeNSelec)
fontSelected = pygame.font.SysFont(self.fontType, self.fontSizeSelec)
textList = [] # we could actually initialize this,
# since we know its size, but this won't impact performance
# generate all menu items
for item in self.items:
textList.append( fontNSelected.render(item, True, self.fontColor) )
# selected item gets a different font
textList[self.state] = fontSelected.render(self.items[self.state], True, self.fontColor)
# generate volume values text (in percentage)
if self.state == 2: # music volume selected
txtMusVol = fontSelected.render( str(self.musicVol) + "%", True, self.fontColor)
txtSndVol = fontNSelected.render( str(self.soundVol) + "%", True, self.fontColor)
elif self.state == 1: # sound volume selected
txtMusVol = fontNSelected.render( str(self.musicVol) + "%", True, self.fontColor)
txtSndVol = fontSelected.render( str(self.soundVol) + "%", True, self.fontColor)
elif self.state == 0: # back selected
txtMusVol = fontNSelected.render( str(self.musicVol) + "%", True, self.fontColor)
txtSndVol = fontNSelected.render( str(self.soundVol) + "%", True, self.fontColor)
# blitting menu items to the screen
# we will center everything
i = 0 # counter
something = 50
for text in textList:
i += 1
screen.blit(text, [WINDOW_WIDTH/2-text.get_width()/2, WINDOW_HEIGHT/2-text.get_height()/2-i*self.spread+ something*len(self.items)])
# blit now the volume values
screen.blit(txtMusVol, [WINDOW_WIDTH/2+textList[-1].get_width()/2 + 30, WINDOW_HEIGHT/2-textList[-1].get_height()/2-3*self.spread+ something*len(self.items)])
screen.blit(txtSndVol, [WINDOW_WIDTH/2+textList[-2].get_width()/2 + 30, WINDOW_HEIGHT/2-textList[-2].get_height()/2-2*self.spread+ something*len(self.items)])