import sys, pygame import random pygame.init() size = width, height = 480, 360 speed = [1, 1] screen = pygame.display.set_mode(size) pygame.font.init() font = pygame.font.Font(pygame.font.get_default_font(), 36) r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) bouncer = font.render('Welcome to PyGame', False, (r, g, b)) rect = bouncer.get_rect() while 1: clock = pygame.time.Clock() # Limit to 60 frames per second clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() rect.x += speed[0] rect.y += speed[1] if rect.left < 0 or rect.right > width: speed[0] = -speed[0] r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) bouncer = font.render('Welcome to PyGame', False, (r, g, b)) if rect.top < 0 or rect.bottom > height: speed[1] = -speed[1] r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) bouncer = font.render('Welcome to PyGame', False, (r, g, b)) screen.fill((0, 0, 0)) screen.blit(bouncer, (rect.x, rect.y)) pygame.display.flip()