concept ball in category python

appears as: ball, The ball, The ball, balls, balls, ball
Hello World! Third Edition

This is an excerpt from Manning's book Hello World! Third Edition.

Here’s another example of a program that has all three elements: in a video game, the input is the signals from the joystick or game controller; the processing is the program figuring out whether you have shot the alien, dodged the ball of fire, completed the level, or whatever; and the output is the graphics on the screen and the sound from the speakers or headphones.

I can’t play volleyball with the ball just standing there!

That’s okay, Carter. Pretty soon we’ll start moving the ball around!

We’re not just going to make the steps smaller—we’re going to add a loop to move the ball (because we want to make many small steps). Starting with listing 16.12, edit the code so it looks like this.

Listing 16.13. Moving a beach ball image smoothly
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load('beach_ball.png')
x = 50                                                              #1
y = 50                                                              #1
screen.blit(my_ball,[x, y])                                         #2
pygame.display.flip()
for looper in range (1, 100):                                       #3
    pygame.time.delay(20)                                           #4
    pygame.draw.rect(screen, [255,255,255], [x, y, 90, 90], 0)      #1
    x = x + 5                                                       #1
    screen.blit(my_ball, [x, y])                                    #1
    pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

If you run this program, you should see the ball moving from its original position over to the right side of the window.

The key to bouncing the ball off the sides of the window is lines 19 and 20. In line 19 (if x > screen.get_width() - 90 or x < 0:), we detect whether the ball is at the edge of the window, and if it is, we reverse its direction in line 20 (x_speed = - x_speed).

Get Programming: Learn to code with Python

This is an excerpt from Manning's book Get Programming: Learn to code with Python.

Suppose you’d like to simulate throwing a ball at your friend in a field. You want to see whether the throw reaches your friend, allowing for a bit of leniency because your friend can jump up to catch, if need be. Let’s see how to write a program that does this simulation for you. You’ll ask the user for the distance your friend is standing away from you, the speed at which you can throw the ball, and an angle at which to throw the ball. The program will tell you whether the ball will make it far enough to be caught. Figure 35.2 shows the setup.

Figure 35.2. Setup for throwing a ball at a certain speed and angle so that it goes a certain distance

The formula that you can use to calculate how far a ball goes when it’s thrown with a certain speed at a certain angle is as follows:

reach = 2 * speed2 * sin(angle) * cos(angle) / 9.8

Listing 35.1 shows the code for this simple program. It first asks the user for the distance to the user’s friend, the speed at which to throw the ball, and the angle at which to throw it. Then it uses the formula to calculate how far the ball will go. Allowing some tolerance (to account for the receiver being able to reach for it), it’ll display one of three messages: it was caught, it fell short, or it went too long. To calculate the sin and cos values, you’ll use functions in the math library, so you have to bring in the library using an import statement. Aside from implementing the formula, only one detail is left. An angle can be measured in degrees or radians. The functions in the math library assume that angles are given in radians, so you need to convert the angle from degrees to radians by using a function in the math library.

Listing 35.1. Using the math library to throw a ball at an angle
import math                                                              #1

distance = float(input("How far away is your friend? (m) "))
speed = float(input("How fast can you throw? (m/s) "))
angle_d = float(input("What angle do you want to throw at? (degrees) "))
tolerance = 2

angle_r = math.radians(angle_d)                                          #2

reach = 2*speed**2*math.sin(angle_r)*math.cos(angle_r)/9.8               #3

if reach > distance - tolerance and reach < distance + tolerance:
    print("Nice throw!")
elif reach < distance - tolerance:
    print("You didn't throw far enough.")
else:
    print("You threw too far.")
Figure 35.2. Setup for throwing a ball at a certain speed and angle so that it goes a certain distance
Listing 35.1. Using the math library to throw a ball at an angle
import math                                                              #1

distance = float(input("How far away is your friend? (m) "))
speed = float(input("How fast can you throw? (m/s) "))
angle_d = float(input("What angle do you want to throw at? (degrees) "))
tolerance = 2

angle_r = math.radians(angle_d)                                          #2

reach = 2*speed**2*math.sin(angle_r)*math.cos(angle_r)/9.8               #3

if reach > distance - tolerance and reach < distance + tolerance:
    print("Nice throw!")
elif reach < distance - tolerance:
    print("You didn't throw far enough.")
else:
    print("You threw too far.")
Hello App Inventor!: Android programming for kids and the rest of us

This is an excerpt from Manning's book Hello App Inventor!: Android programming for kids and the rest of us.

The interval of the sprite you’re going to fling—in this case, the ball—is important. The interval means how often (in milliseconds) the sprite’s position is updated. For example, if the interval is 50 and the speed is 10, then the sprite will move 10 pixels every 50 milliseconds. One thousand milliseconds is one second. The speed is measured in how many pixels per interval the sprite (or ball) moves. To get a smooth fling movement, an interval of 10 works well. We’ll sort out the speed side of things in the Blocks Editor.

This mini app demonstrates the use of properties in the Designer to make a ball appear to bounce from one corner to another, changing the canvas color as it does so.

Because most of the work is done in the Designer we’ll give an explanation of each of the properties. We encourage you to tinker with the properties once you’ve completed the app and have the emulator open to see what difference your changes make to the app’s behavior. Sprites and balls default to Heading 0 (heading east), Interval 100 (moving every 1/10 of a second), and—the important part—Speed 0.0. We know Heading 0 being east is a bit peculiar, but as a default it makes sense to go from left to right. In order for your sprite to move, you need to give it some speed (explained further in a moment).

Learning Point: Reminder about setting properties!

You should think of properties as a jumping-off point for components. You might start with a ball that has a Radius property of 10 and change the property in your coding. But every time you restart (or initialize) the app, all properties go back to those set in the Designer.