From 77859efe266aa15337a0863b0d7169da60cdb261 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Sat, 6 Aug 2016 20:48:51 -0500 Subject: [PATCH 1/8] Add groundwork for easy text command Adds function (does nothing) and basic font/fontPath logic. --- slither/slither.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/slither/slither.py b/slither/slither.py index 1126cb5..9451121 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -237,6 +237,15 @@ def play(self): pass slitherSound = Sound() +# Convienience function to blit text +def blitText(text, x=0, y=0, size=12, font=False, fontPath=False): + if font: + textFont = pygame.font.SysFont(font, size) + elif fontPath: + textFont = pygame.font.Font(fontPath, size) + else: + textFont = pygame.font.SysFont("Helvetica", size) + def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen From 973b9582c91ecfd1355040f1ecb8e31df433e6da Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Sat, 6 Aug 2016 20:51:52 -0500 Subject: [PATCH 2/8] Render text Also add missing antialias and color options. --- slither/slither.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/slither/slither.py b/slither/slither.py index 9451121..88ff51a 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -238,7 +238,7 @@ def play(self): pass slitherSound = Sound() # Convienience function to blit text -def blitText(text, x=0, y=0, size=12, font=False, fontPath=False): +def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, color=(0,0,0)): if font: textFont = pygame.font.SysFont(font, size) elif fontPath: @@ -246,6 +246,8 @@ def blitText(text, x=0, y=0, size=12, font=False, fontPath=False): else: textFont = pygame.font.SysFont("Helvetica", size) + textImage = textFont.render(text, antialias, color) + def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen From d3476081d7e590aef637b1697a47f86fc05a56e2 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 15 Aug 2016 15:21:28 -0500 Subject: [PATCH 3/8] Add blit to screen Next up is a test. --- slither/slither.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/slither/slither.py b/slither/slither.py index 88ff51a..4be09f4 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -244,10 +244,12 @@ def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, c elif fontPath: textFont = pygame.font.Font(fontPath, size) else: - textFont = pygame.font.SysFont("Helvetica", size) + textFont = pygame.font.SysFont("Helvetica", size) # Users should always have Helvetica installed textImage = textFont.render(text, antialias, color) + slitherStage.blit(textImage, (x, y)) + def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen From 8974852d7360dfaba18118dda7cfafc907ea070a Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 15 Aug 2016 15:39:27 -0500 Subject: [PATCH 4/8] Add text test --- slither/tests/textTest.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 slither/tests/textTest.py diff --git a/slither/tests/textTest.py b/slither/tests/textTest.py new file mode 100644 index 0000000..2252446 --- /dev/null +++ b/slither/tests/textTest.py @@ -0,0 +1,12 @@ +import slither + +slither.slitherStage.bgColor = (40, 222, 40) + +slither.blitText("Hello, world!") + +slither.setup() # Begin slither + +def run_a_frame(): + pass + +slither.runMainLoop(run_a_frame) From 425916d9a2562998dd58ba877da4897faa85e175 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 15 Aug 2016 15:41:04 -0500 Subject: [PATCH 5/8] Fix missing pygame.font.init --- slither/tests/textTest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slither/tests/textTest.py b/slither/tests/textTest.py index 2252446..5f6064a 100644 --- a/slither/tests/textTest.py +++ b/slither/tests/textTest.py @@ -2,10 +2,10 @@ slither.slitherStage.bgColor = (40, 222, 40) -slither.blitText("Hello, world!") - slither.setup() # Begin slither +slither.blitText("Hello, world!") + def run_a_frame(): pass From 7875082038783e816e085f81f352806eb7c15bb5 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 15 Aug 2016 15:42:13 -0500 Subject: [PATCH 6/8] slitherStage is not globalscreen --- slither/slither.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/slither/slither.py b/slither/slither.py index 4be09f4..ea25e04 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -239,6 +239,7 @@ def play(self): pass # Convienience function to blit text def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, color=(0,0,0)): + global globalscreen if font: textFont = pygame.font.SysFont(font, size) elif fontPath: @@ -248,12 +249,13 @@ def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, c textImage = textFont.render(text, antialias, color) - slitherStage.blit(textImage, (x, y)) + globalscreen.blit(textImage, (x, y)) def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen pygame.init() + pygame.font.init() screen = pygame.display.set_mode(SCREEN_SIZE) caption = pygame.display.set_caption(caption) globalscreen = screen From bdaab7383988bc6366fd4bdf2820f8c6b001d2cf Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Mon, 15 Aug 2016 15:55:45 -0500 Subject: [PATCH 7/8] Add display.flip --- slither/slither.py | 2 ++ slither/tests/textTest.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/slither/slither.py b/slither/slither.py index ea25e04..794737c 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -251,6 +251,8 @@ def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, c globalscreen.blit(textImage, (x, y)) + pygame.display.flip() + def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' global globalscreen diff --git a/slither/tests/textTest.py b/slither/tests/textTest.py index 5f6064a..8f0b0a7 100644 --- a/slither/tests/textTest.py +++ b/slither/tests/textTest.py @@ -7,6 +7,6 @@ slither.blitText("Hello, world!") def run_a_frame(): - pass + slither.blitText("Hello, world!") slither.runMainLoop(run_a_frame) From b4236b13789d404a555cf1b2f647f1e8713003d3 Mon Sep 17 00:00:00 2001 From: Tymewalk Date: Sun, 11 Sep 2016 13:10:46 -0500 Subject: [PATCH 8/8] Only flip at the end My guess is that something's happening due to display.flip()-fighting. --- slither/slither.py | 5 +++-- slither/tests/textTest.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/slither/slither.py b/slither/slither.py index 794737c..302421b 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -251,7 +251,7 @@ def blitText(text, x=0, y=0, size=12, font=False, fontPath=False, antialias=0, c globalscreen.blit(textImage, (x, y)) - pygame.display.flip() + #pygame.display.flip() def setup(caption=sys.argv[0]): '''Sets up PyGame and returns a screen object that can be used with blit().''' @@ -294,7 +294,7 @@ def blit(screen): new_rect.center = (obj.xpos, obj.ypos) screen.blit(image, new_rect) - pygame.display.flip() + #pygame.display.flip() def registerCallback(eventname, func=None): '''Register the function func to handle the event eventname''' @@ -330,4 +330,5 @@ def runMainLoop(frameFunc): eventCallbacks[event.type](event) # eventCallbacks would be a dictionary mapping # event types to handler functions. + pygame.display.flip() # Always flip at the end clock.tick(projectFPS) # Run at however many FPS the user specifies diff --git a/slither/tests/textTest.py b/slither/tests/textTest.py index 8f0b0a7..5c49cf5 100644 --- a/slither/tests/textTest.py +++ b/slither/tests/textTest.py @@ -7,6 +7,6 @@ slither.blitText("Hello, world!") def run_a_frame(): - slither.blitText("Hello, world!") + slither.blitText("Hello, world!", size=40) slither.runMainLoop(run_a_frame)