Skip to content

Commit

Permalink
Added wrapper class for pygame display updates so they happen in one …
Browse files Browse the repository at this point in the history
…location and so additional future improvements can be easier to implement
  • Loading branch information
Samuel Senior committed May 26, 2020
1 parent e91bfaf commit 681233f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 71 deletions.
10 changes: 5 additions & 5 deletions src/aboutScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def drawBackground(self, display):
display : Display
The Display instance used for displaying to the screen.
'''
display.draw.rect(self.win, (170, 170, 170), self.aboutBackground, 0)
display.draw.rect(self.win, (100, 100, 100), self.aboutBackground, 2)
display.draw.rect((170, 170, 170), self.aboutBackground, 0)
display.draw.rect((100, 100, 100), self.aboutBackground, 2)

def display(self, display, source):
'''
Expand Down Expand Up @@ -143,11 +143,11 @@ def display(self, display, source):
for button in self.buttons:
if button.updated:
button.hovered = False
button.draw(self.win)
button.draw(display)

# Draw the text to the screen.
for txt in self.aboutText:
txt.draw(self.win)
txt.draw(display)

# Update the screen to display the updated draws.
display.flip()
Expand Down Expand Up @@ -191,7 +191,7 @@ def display(self, display, source):
# If any of the buttons have had changes happen to them
# then redraw them.
if button.updated:
button.draw(self.win)
button.draw(display)

# Update the screen to display the updated draws.
display.flip()
Expand Down
18 changes: 9 additions & 9 deletions src/bell.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,29 @@ def updateLocation(self, x, y, textX, 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, win, renderNumber=True):
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
----------
win : pygame.display
The pygame display window.
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':
win.blit(self.backstrokeBellBlank, (self.x, self.y))
win.blit(self.handstrokeBell, (self.x, self.y))
display.blit(self.backstrokeBellBlank, (self.x, self.y))
display.blit(self.handstrokeBell, (self.x, self.y))
else:
win.blit(self.handstrokeBellBlank, (self.x, self.y))
win.blit(self.backstrokeBell, (self.x, self.y))
display.blit(self.handstrokeBellBlank, (self.x, self.y))
display.blit(self.backstrokeBell, (self.x, self.y))
if renderNumber:
pygame.draw.rect(win, self.backgroundColour, self.bellNumberTextBlank, 0)
win.blit(self.bellNumberText, (self.textX, self.textY))
display.draw.rect(self.backgroundColour, self.bellNumberTextBlank, 0)
display.blit(self.bellNumberText, (self.textX, self.textY))

def handle_event(self, send):
'''
Expand Down
12 changes: 6 additions & 6 deletions src/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ def set_inactive(self):
self.active = False
self.hovered = True

def draw(self, win):
def draw(self, display):
self.set_rend()

if self.border:
pygame.draw.rect(win, self.buttonColour, (self.rect.x, self.rect.y, self.rect.w, self.rect.h))
win.blit(self.rend, (self.rect.x+5, self.rect.y))
pygame.draw.rect(win, self.borderColour,(self.rect.x, self.rect.y, self.rect.w, self.rect.h), 2)
display.draw.rect(self.buttonColour, (self.rect.x, self.rect.y, self.rect.w, self.rect.h))
display.blit(self.rend, (self.rect.x+5, self.rect.y))
display.draw.rect(self.borderColour,(self.rect.x, self.rect.y, self.rect.w, self.rect.h), 2)
else:
pygame.draw.rect(win, self.buttonColour, (self.rect.x, self.rect.y, self.rect.w, self.rect.h))
win.blit(self.rend, (self.rect.x+5, self.rect.y))
display.draw.rect(self.buttonColour, (self.rect.x, self.rect.y, self.rect.w, self.rect.h))
display.blit(self.rend, (self.rect.x+5, self.rect.y))

def set_rend(self):
self.rend = self.font.render(self.text, True, self.get_color())
Expand Down
8 changes: 4 additions & 4 deletions src/display.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pygame

class Draw:
def __init__(self):
pass
def __init__(self, win):
self.win = win

def rect(self, *args):
pygame.draw.rect(*args)
pygame.draw.rect(self.win, *args)

class Display:

Expand All @@ -23,7 +23,7 @@ def __init__(self, width, height, caption=None, iconFile=None):
self.icon = pygame.image.load(self.iconFile)
pygame.display.set_icon(self.icon)

self.draw = Draw()
self.draw = Draw(self.win)

def set_mode(self, *args):
self.win = pygame.display.set_mode(*args)
Expand Down
14 changes: 7 additions & 7 deletions src/helpScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def drawBackground(self, display):
display : Display
The Display instance used for displaying to the screen.
'''
display.draw.rect(self.win, (170, 170, 170), self.helpBackground, 0)
display.draw.rect(self.win, (100, 100, 100), self.helpBackground, 2)
display.draw.rect((170, 170, 170), self.helpBackground, 0)
display.draw.rect((100, 100, 100), self.helpBackground, 2)

def display(self, display, source):
self.win = display.win
Expand Down Expand Up @@ -152,10 +152,10 @@ def display(self, display, source):
for button in self.buttons:
if button.updated:
button.hovered = False
button.draw(self.win)
button.draw(display)

for txt in text:
txt.draw(self.win)
txt.draw(display)

display.flip()

Expand All @@ -179,14 +179,14 @@ def display(self, display, source):
self.button_ringingPage.active = True
self.drawBackground(display)
for txt in text:
txt.draw(self.win)
txt.draw(display)
elif self.button_ringingPage.rect.collidepoint(event.pos) and self.button_ringingPage.active == True:
text = self.ringingText
self.button_serverPage.active = True
self.button_ringingPage.active = False
self.drawBackground(display)
for txt in text:
txt.draw(self.win)
txt.draw(display)

for button in self.buttons:
if button.rect.collidepoint(pygame.mouse.get_pos()):
Expand All @@ -198,7 +198,7 @@ def display(self, display, source):

for button in self.buttons:
if button.updated:
button.draw(self.win)
button.draw(display)

display.flip()
clock.tick(self.frameRate)
8 changes: 4 additions & 4 deletions src/inputBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def update(self):
self.rectOld.h = self.rectOld.h
self.rect.w = self.width

def draw(self, screen):
pygame.draw.rect(screen, (255, 255, 255), self.rectOld, 0)
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+2))
pygame.draw.rect(screen, self.color, self.rect, 2)
def draw(self, display):
display.draw.rect((255, 255, 255), self.rectOld, 0)
display.blit(self.txt_surface, (self.rect.x+5, self.rect.y+2))
display.draw.rect(self.color, self.rect, 2)

12 changes: 6 additions & 6 deletions src/menuScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def connectionStatusMessage(self):
return self.blankMessage

def updateConnectionStatusMessage(self, display):
display.draw.rect(self.win, (255, 255, 255), self.connectionRectWhite, 0)
display.draw.rect((255, 255, 255), self.connectionRectWhite, 0)
if self.connection:
display.blit(self.connectionStatusMessage(), (self.button_serverConnect.width+25, self.button_serverConnect.rect.y+5))

Expand Down Expand Up @@ -138,22 +138,22 @@ def display(self, display):
display.blit(self.rebelLogo, (self.width/2-self.rebelLogo.get_width()/2, (self.inputBox_userName.rect.y - self.rebelLogo.get_height())*3/8))

for box in self.input_boxes:
box.draw(self.win, redrawTitle=True)
box.draw(display, redrawTitle=True)
box.updated = False
for button in self.buttons:
button.hovered = False
button.draw(self.win)
button.draw(display)

self.updateConnectionStatusMessage(display)

while self.run_menu:
for box in self.input_boxes:
if box.updated:
box.draw(self.win, redrawTitle=False)
box.draw(display, redrawTitle=False)
box.updated = False
for button in self.buttons:
if button.updated:
button.draw(self.win)
button.draw(display)

for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand Down Expand Up @@ -225,7 +225,7 @@ def display(self, display):
elif button.active == True:
button.hovered = False
button.updated = True
button.draw(self.win)
button.draw(display)

display.flip()
clock.tick(self.frameRate)
34 changes: 17 additions & 17 deletions src/optionsScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def saveConfig(self):
self.config.set('keys', bellKeyList)

def drawBackground(self, display):
display.draw.rect(self.win, (170, 170, 170), self.optionsBackground, 0)
display.draw.rect(self.win, (100, 100, 100), self.optionsBackground, 2)
display.draw.rect((170, 170, 170), self.optionsBackground, 0)
display.draw.rect((100, 100, 100), self.optionsBackground, 2)

def display(self, display, source):
self.win = display.win
Expand Down Expand Up @@ -213,23 +213,23 @@ def display(self, display, source):

self.drawBackground(display)

self.bellNumberInputBoxes[0].draw(self.win, redrawTitle=True)
self.bellNumberInputBoxes[0].draw(display, redrawTitle=True)
self.bellNumberInputBoxes[0].updated = False
for box in self.bellNumberInputBoxes[1:]:
box.draw(self.win)
box.draw(display)
box.updated = False
self.bellKeyInputBoxes[0].draw(self.win, redrawTitle=True)
self.bellKeyInputBoxes[0].draw(display, redrawTitle=True)
self.bellKeyInputBoxes[0].updated = False
for box in self.bellKeyInputBoxes[1:]:
box.draw(self.win)
box.draw(display)
box.updated = False
for button in self.buttons:
if button.updated:
button.hovered = False
button.draw(self.win)
button.draw(display)

for txt in text:
txt.draw(self.win)
txt.draw(display)

display.flip()

Expand Down Expand Up @@ -261,27 +261,27 @@ def display(self, display, source):
self.button_tuning.active = True
self.button_keys.active = False
self.button_other.active = True
self.drawBackground()
self.drawBackground(display)
for txt in text:
txt.draw(self.win)
txt.draw(display)
elif self.button_tuning.rect.collidepoint(event.pos) and self.button_tuning.active == True:
optionsPage = 'tuning'
text = self.tuningText
self.button_keys.active = True
self.button_tuning.active = False
self.button_other.active = True
self.drawBackground()
self.drawBackground(display)
for txt in text:
txt.draw(self.win)
txt.draw(display)
elif self.button_other.rect.collidepoint(event.pos) and self.button_other.active == True:
optionsPage = 'other'
text = self.otherText
self.button_keys.active = True
self.button_tuning.active = True
self.button_other.active = False
self.drawBackground()
self.drawBackground(display)
for txt in text:
txt.draw(self.win)
txt.draw(display)

for box in self.bellNumberInputBoxes:
box.mouseDownEvent(event, self.win)
Expand All @@ -307,16 +307,16 @@ def display(self, display, source):
if optionsPage == 'keys':
for box in self.bellNumberInputBoxes:
if box.updated:
box.draw(self.win)
box.draw(display)
box.updated = False
for box in self.bellKeyInputBoxes:
if box.updated:
box.draw(self.win)
box.draw(display)
box.updated = False

for button in self.buttons:
if button.updated:
button.draw(self.win)
button.draw(display)

display.flip()
clock.tick(self.frameRate)
14 changes: 7 additions & 7 deletions src/ringingScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,18 @@ def display(self, display):

self.win.fill(self.backgroundColour)
for bell in self.bells.values():
bell.draw(self.win, renderNumber=True)
bell.draw(display, renderNumber=True)
for button in self.buttons:
button.active = True
button.hovered = False
button.draw(self.win)
button.draw(display)

run_ringing = True
while run_ringing:

for button in self.buttons:
if button.updated:
button.draw(self.win)
button.draw(display)

for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand All @@ -238,10 +238,10 @@ def display(self, display):
self.initialise()
self.win.fill(self.backgroundColour)
for bell in self.bells.values():
bell.draw(self.win, renderNumber=True)
bell.draw(display, renderNumber=True)
for button in self.buttons:
button.hovered = False
button.draw(self.win)
button.draw(display)
elif self.button_options.rect.collidepoint(event.pos):
run_ringing = False
return 'optionsScreen'
Expand Down Expand Up @@ -270,7 +270,7 @@ def display(self, display):
elif button.active == True:
button.hovered = False
button.updated = True
button.draw(self.win)
button.draw(display)

try:
stroke, bellNumber = self.network.getBellRung()
Expand All @@ -280,7 +280,7 @@ def display(self, display):
pass
else:
self.bells[bellNumber].bellRung(stroke)
self.bells[bellNumber].draw(self.win)
self.bells[bellNumber].draw(display)
pygame.mixer.Channel(bellNumber-1).play(self.audio.bells[bellNumber])

display.flip()
Expand Down
4 changes: 2 additions & 2 deletions src/textBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def generateFormattedText(self, startingY, width):
else:
self.h = (y - self.y)

def draw(self, win):
def draw(self, display):
for text in self.textFormatted:
win.blit(*text)
display.blit(*text)

def set_rect(self):
self.rect = pygame.Rect(self.x, self.y, self.width, 0)
Expand Down
Loading

0 comments on commit 681233f

Please sign in to comment.