Skip to content

Instantly share code, notes, and snippets.

@Mekire
Created November 8, 2017 04:19
Show Gist options
  • Save Mekire/17a6ff788d993244cf15231061a91bdc to your computer and use it in GitHub Desktop.
Save Mekire/17a6ff788d993244cf15231061a91bdc to your computer and use it in GitHub Desktop.
import sys
import pygame as pg
import textwrap
BACKGROUND = pg.Color("darkslategray")
SCREEN_SIZE = (500, 500)
FPS = 60
SAMPLE_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tortor ornare, posuere turpis nec, maximus ipsum. Etiam a dui feugiat, convallis augue vitae, pretium nisl. Cras gravida ac eros et aliquam. Phasellus sagittis metus sed mattis egestas. Praesent auctor ante a ullamcorper consequat. Vestibulum eleifend porttitor turpis ac tempor. Quisque ut efficitur felis. Quisque posuere molestie nibh ut ornare. Morbi hendrerit ultrices scelerisque. Praesent tincidunt dictum eros, nec egestas turpis euismod eget. Sed interdum enim gravida mollis ultricies."
class TextSprite(pg.sprite.Sprite):
def __init__(self, image, rect, *groups):
super(TextSprite, self).__init__(*groups)
self.image = image
self.rect = rect
class App(object):
def __init__(self):
self.screen = pg.display.get_surface()
self.screen_rect = self.screen.get_rect()
self.clock = pg.time.Clock()
self.done = False
self.font = pg.font.Font(None, 25)
self.multiline = self.prerender_text(SAMPLE_TEXT)
def prerender_text(self, text):
text_wrapped = textwrap.wrap(text, width=50, initial_indent=" "*4)
y_start, y_width = 25, 25
rendered = pg.sprite.Group()
for string in text_wrapped:
image = self.font.render(string, True, pg.Color("tomato"))
place = {"x" : 25, "centery" : y_start}
y_start += y_width
rect = image.get_rect(**place)
TextSprite(image, rect, rendered)
return rendered
def update(self):
"""
All updates to all actors occur here.
Exceptions include things that are direct results of events which
may occasionally occur in the event loop.
For example, updates based on held keys should be found here, but
updates to single KEYDOWN events would be found in the event loop.
"""
pass
def render(self):
"""
All calls to drawing functions here.
No game logic.
"""
self.screen.fill(BACKGROUND)
self.multiline.draw(self.screen)
pg.display.update()
def event_loop(self):
"""
Event handling here. Only things that are explicit results of the
given events should be found here. Do not confuse the event and update
phases.
"""
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
def main_loop(self):
"""
Main loop for your whole app. This doesn't need to be touched until
you start writing framerate independant games.
"""
while not self.done:
self.event_loop()
self.update()
self.render()
self.clock.tick(FPS)
def main():
"""
Prepare pygame and the display and create an App instance.
Call the app instance's main_loop function to begin the App.
"""
pg.init()
pg.display.set_mode(SCREEN_SIZE)
App().main_loop()
pg.quit()
sys.exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment