Thursday, March 17, 2011

Mar. 17

fruit = 'rampage'
"""
>>> type(fruit)
<type 'str'>
>>> len(fruit)
7
>>> fruit[:3]
'ram'
"""

if __name__ == '__main__':
import doctest
doctest.testmod()
This program I had to do to make it pass the doctest.
from gasp import *

begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)

ball_x = 10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4
dy = 1

while ball_x < 810:
ball_x += dx
ball_y += dy
move_to(ball, (ball_x, ball_y))
update_when('next_tick')

end_graphics()
The above program makes a ball appear then fly across the screen until it hits the side of the window and disappears. The background also to this graphic is then yellow. I can manipulate the color of the background by just changing the color, I can also change the speed of the ball flying across the screen.
>>> from gasp import *
>>> i = 0
>>> while i < 10:
...     print random_between(-5, 5)
...     i += 1
... 
5
2
-4
5
-5
-5
4
-5
4
-1
The above program is called a more or less random integer. This program will random choose 10 numbers between -5 and 5, so really your results to this program varies. 
from gasp import *

begin_graphics(800, 600, title="Catch", background=color.RED)
set_speed(120)

ball_x = 10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4
dy = random_between(-4, 4)

while ball_x < 810:
ball_x += dx
ball_y += dy
move_to(ball, (ball_x, ball_y))
update_when('next_tick')

end_graphics()
The above program has been modified to appear with a background of red and then I changed the dy = 1 to dy = random_between(-4, 4) which will cause a random number to be chosen to be used for the direction of the ball

No comments:

Post a Comment