1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# 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]
|