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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# lines: make lots of lines on the screen
# pep8-ignore: E501
import pippy
import pygame
import sys
from pygame.locals import *
from random import *
# always need to init first thing
pygame.init()
# turn off the cursor
pygame.mouse.set_visible(False)
# create the window and keep track of the surface
# for drawing into
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# ask for screen's width and height
size = width, height = screen.get_size()
# start the screen all black
screen.fill((0, 0, 0))
# we need starting endpoints for the line and seed motion vectors
start = [randint(0, size[0]), randint(0, size[1])]
end = [randint(0, size[0]), randint(0, size[1])]
# randomize the motion, 1..3 in each direction, positive or negative, but
# never 0
mvect_start = [choice((-1, 1)) * randint(1, 3), choice((-1, 1)) * randint(1, 3)]
mvect_end = [choice((-1, 1)) * randint(1, 3), choice((-1, 1)) * randint(1, 3)]
# start with a random color and color direction
color = [randint(0, 255), randint(0, 255), randint(0, 255)]
direction = [choice((-1, 1)), choice((-1, 1)), choice((-1, 1))]
while pippy.pygame.next_frame():
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYDOWN:
sys.exit()
# draw the line using the current values and width=3
pygame.draw.line(screen, color, start, end, 3)
# update the display
pygame.display.flip()
# update the end points and the color
for i in range(2):
start[i] = start[i] + mvect_start[i]
end[i] = end[i] + mvect_end[i]
for i in range(3):
color[i] = color[i] + direction[i]
# check if anything has gone out of range and
# if so, bring back to edge and reverse the
# corresponding motion vector
if start[0] < 0:
start[0] = 0
mvect_start[0] = (-1) * mvect_start[0]
elif start[0] >= size[0]:
start[0] = size[0] - 1
mvect_start[0] = (-1) * mvect_start[0]
if start[1] < 0:
start[1] = 0
mvect_start[1] = (-1) * mvect_start[1]
elif start[1] >= size[1]:
start[1] = size[1] - 1
mvect_start[1] = (-1) * mvect_start[1]
if end[0] < 0:
end[0] = 0
mvect_end[0] = (-1) * mvect_end[0]
elif end[0] >= size[0]:
end[0] = size[0] - 1
mvect_end[0] = (-1) * mvect_end[0]
if end[1] < 0:
end[1] = 0
mvect_end[1] = (-1) * mvect_end[1]
elif end[1] >= size[1]:
end[1] = size[1] - 1
mvect_end[1] = (-1) * mvect_end[1]
for i in range(3):
if color[i] < 0:
color[i] = 0
direction[i] = direction[i] * (-1)
elif color[i] >= 255:
color[i] = 255
direction[i] = direction[i] * (-1)
# randomly change the color directon on occasion
if randint(0, 511) == 128:
for i in range(3):
direction[i] = choice((-1, 1))
|