Created
May 4, 2018 16:09
-
-
Save pathunstrom/af45570f8c2a92456ae6fff86bc32649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterable | |
import pygame as pg | |
class Target(pg.sprite.Sprite): | |
def __init__(self, image: pg.Surface, position: Iterable[int]): | |
"""position needs to be length 2.""" | |
super().__init__() | |
self.image = image | |
self.rect = self.image.get_rect() | |
self.rect.center = position | |
class FollowText(pg.sprite.Sprite): | |
font = pg.font.Font(None, 15) | |
def __init__(self, text: str, target: pg.sprite.Sprite, offset: Iterable[int]=(0, 0)): | |
super().__init__() | |
self.image = font.render(text, True, (255, 255, 255)) | |
self.rect = self.image.get_rect() | |
self.target = target | |
self.offset = offset | |
self.rect.center = self.target_position() | |
def update(self, *args): | |
self.rect.center = self.target_position() | |
def target_positon(self): | |
position = self.target.rect.center | |
return position[0] + self.offset[0], position[1] + self.offset[1] | |
def main() | |
game_objects = pg.sprite.Group() | |
follow_text_group = pg.sprite.Group() | |
target_sprite = Target(SOME_IMAGE, (500, 500)) | |
game_objects.add(target_sprite) | |
follow_text = FollowText("Example", target_sprite) | |
follow_text_group.add(follow_text) | |
running = True | |
while running: | |
handle_events() | |
game_objects.update() | |
follow_text_group.update() | |
game_objects.draw(window) | |
follow_text_group.draw(window) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment