-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbell.py
200 lines (160 loc) · 6.63 KB
/
bell.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
import pygame
import os
import sys
class Bell:
'''
The Bell object displays the image of a bell, along with its given bell
number. It also stores the key value that is used to ring the bell and
updates the bell from handstroke to backstroke, and visa versa, when the
key is pressed to ring the bell.
Parameters
----------
bellNumber : int
The number of the bell.
location : (int, int)
The (x, y) coordinate of the top most left position of the bell image.
bellImageFile : string
The file name and location of the bell image.
width : int
The width of the bell image.
height : int
The height of the bell image.
textLocation : (int, int)
The (x, y) coordinate of the top most left position of the bell number text.
font : Font
An initialised Font instance.
stroke : string
The stroke of the bell, either 'H' for 'handstroke' or 'B' for
backstroke.
key : pygame key
The pygame key used to ring the bell.
backgroundColour : (int, int, int)
A tuple of the RGB vales of the ringing screen background colour.
'''
def __init__(self, bellNumber, location, bellImageFile, width, height, textLocation, font, stroke='H', key=None, backgroundColour=(255, 255, 255)):
# Set the working directory based on if ReBel is being run from an
# executable or the Python source code.
if getattr(sys, 'frozen', False):
# In a bundle
self.exeDir = os.path.dirname(sys.executable)
else:
# In normal python
self.exeDir = ""
self.font = font
self.stroke = stroke
# Set the width and height of the bell image.
self.width = width
self.height = height
self.backgroundColour = backgroundColour
# Read in bell image and create handstroke and backstroke images from
# it. The backstroke image being a rotation and flip of the handstroke
# image. 'Blanks' are also made and are used to 'erase' the previous
# stroke when updating to the new stroke so that artifacts do not
# remain.
bell = pygame.image.load(bellImageFile)
self.handstrokeBell = pygame.transform.scale(bell, (self.width, self.height))
self.handstrokeBellBlank = self.handstrokeBell.copy()
self.handstrokeBellBlank.fill(self.backgroundColour)
self.backstrokeBell = pygame.transform.scale(bell, (self.width, self.height))
self.backstrokeBell = pygame.transform.rotate(self.backstrokeBell, -90.0)
self.backstrokeBell = pygame.transform.flip(self.backstrokeBell, True, False)
self.backstrokeBellBlank = self.backstrokeBell.copy()
self.backstrokeBellBlank.fill(self.backgroundColour)
# Location of the bell, with the (x, y) coordinates corresponding to
# the top most left part of the bell image.
self.x = location[0]
self.y = location[1]
# Location of the bell number text, with the (x, y) coordinates
# corresponding to the top most left part of the bell number text.
self.textX = textLocation[0]
self.textY = textLocation[1]
self.bellNumber = bellNumber
self.bellNumberText = self.font.FONT.render(str(self.bellNumber), True, (0, 0, 0))
self.bellNumberTextBlank = pygame.Rect(self.textX, self.textY, self.bellNumberText.get_width(), self.bellNumberText.get_height())
self.key = key
def fill(self, surface, colour):
'''
Fills a pygame surface with a given colour.
Parameters
----------
surface : pygame.surface
The pygame surface to be filled.
colour : (int, int, int)
A tuple of the RGB vales of the desired surface colour.
'''
surface.fill(colour)
def setKey(self, key):
'''
Sets the key that the bell is rung with.
Parameters
----------
key : pygame key
The pygame key value that the bell is rung with.
'''
self.key = key
def clearKey(self):
'''
Clears the current key value that the bell is rung with.
'''
self.key = None
def updateLocation(self, x, y, textX, textY):
'''
Updates the locations of the bell and associated bell number text.
Parameters
----------
x : int
The x-position of the left most point of the bell image.
y : int
The y-position of the top most point of the bell image.
textX : int
The x-position of the left most point of the bell number text.
textY : int
The y-position of the top most point of the bell number text.
'''
self.x = x
self.y = y
self.textX = textX
self.textY = textY
# Update the position of the 'blank' bell image too.
self.bellNumberTextBlank = pygame.Rect(self.textX, self.textY, self.bellNumberText.get_width(), self.bellNumberText.get_height())
def draw(self, display, renderNumber=True):
'''
Draws the bell image and bell number text to the screen, with the bell
stroke being drawn corresponding to the current value of the internal
'stroke' variable.
Parameters
----------
display : Display
The display instance used for displaying to the screen.
renderNumber : bool
The flag to decide whether to draw the bell number text.
'''
if self.stroke == 'H':
display.blit(self.backstrokeBellBlank, (self.x, self.y))
display.blit(self.handstrokeBell, (self.x, self.y))
else:
display.blit(self.handstrokeBellBlank, (self.x, self.y))
display.blit(self.backstrokeBell, (self.x, self.y))
if renderNumber:
display.draw.rect(self.backgroundColour, self.bellNumberTextBlank, 0)
display.blit(self.bellNumberText, (self.textX, self.textY))
def handle_event(self, send):
'''
The event handler that sends a message to the server saying the bell
has been rung.
Parameters
----------
send : Client.send
The Client function used to send messages to the server.
'''
send("R:" + self.stroke + str(self.bellNumber))
def bellRung(self, stroke):
'''
Changes the current stroke of the bell to the opposite value of the
stroke passed in.
Parameters
----------
stroke : string
The current stroke value of the bell.
'''
self.stroke = 'B' if stroke == 'H' else 'H'