# bounce: move some text around the screen import pippy import pygame import sys from pygame.locals import * # the text to bounce around the screen msg = 'Hello!' # the size of the text, in pixels fsize = 36 # vector for motion, will control speed and angle mvect = [3, 2] # always need to init first thing pygame.init() # turn off cursor pygame.mouse.set_visible(False) # create the window and keep track of the surface # for drawing into screen = pygame.display.set_mode() # ask for screen's width and height size = width, height = screen.get_size() # create a Font object from a file, or use the default # font if the file name is None. size param is height # in pixels # usage: pygame.font.Font(filename|object, size) font = pygame.font.Font(None, fsize) # Font.render draws text onto a new surface. # # usage: Font.render(text, antialias, color, bg=None) text = font.render(msg, True, (10, 10, 10)) # the Rect object is used for positioning textRect = text.get_rect() # start at the top left textRect.left = 0 textRect.top = 0 while pippy.pygame.next_frame(): # every time we move the text, check for quit or keydown events and exit for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: sys.exit() # fill the screen with almost white screen.fill((250, 250, 250)) # draw the text screen.blit(text, textRect) # update the display pygame.display.flip() # move the text # # Rect.move returns a new Rect while # Rect.move_ip moves in place, so we'll use # the latter textRect.move_ip(mvect) # bounce off edges if textRect.left < 0: textRect.left = 0 mvect[0] = -1 * mvect[0] elif textRect.right >= size[0]: textRect.right = size[0] - 1 mvect[0] = -1 * mvect[0] if textRect.top < 0: textRect.top = 0 mvect[1] = -1 * mvect[1] elif textRect.bottom >= size[1]: textRect.bottom = size[1] - 1 mvect[1] = -1 * mvect[1]