Chp 8-10 Activity lab

Please complete the following chp 8-10 activity by answering the following questions below. Copy and past the code into your wing IDE and try to fix the mistakes. Then, copy and paste your corrected code into a new MS Word or Google Docs document and save it on the server when done.

When done, you can finish off your drawing functions assignment that I assigned last Mon.

  1. Why does using this code in the main loop not work to move the rectangle?
    rect_x = 50
    pygame.draw.rect(screen, WHITE, [rect_x, 50, 50, 50])
    rect_x += 1
  2. If the screen is 400 pixels tall, and the shape is 20 pixels high, at what point should the code check to see if the shape is in contact with the bottom of the screen.
  3. When drawing a starry background, explain why it doesn’t work to put code like this in the main program loop:
    for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    pygame.draw.circle(screen, WHITE, [x, y], 2)
  4. If you have a list of coordinates like the following, what code would be required to print out the array location that holds the number 33?
    stars = [[ 3, 4],
    [33, 94],
    [ 0, 0]]
  5. This code example causes snow to fall:
    # Process each snow flake in the list
    for i in range(len(snow_list)):
    # Get the x and y from the list
    x = snow_list[i][0]
    y = snow_list[i][1]
    # Draw the snow flake
    pygame.draw.circle(screen, WHITE, [x, y], 2)
    # Move the snow flake down one pixel
    snow_list[i][1] += 1

    So does the example below. Explain why this example works as well.

    # Process each snow flake in the list
    for i in range(len(snow_list)):
    # Draw the snow flake
    pygame.draw.circle(screen, WHITE, snow_list[i], 2)
    # Move the snow flake down one pixel
    snow_list[i][1] += 1

 

6. What’s wrong with this code that uses a function to draw a stick figure? Assume the colors are already defined and the rest of the program is ok. What is wrong with the code in the function?

def draw_stick_figure(screen, x, y):
# Head
pygame.draw.ellipse(screen, BLACK, [96,83,10,10], 0)
# Legs
pygame.draw.line(screen, BLACK, [100,100], [105,110], 2)
pygame.draw.line(screen, BLACK, [100,100], [95,110], 2)
# Body
pygame.draw.line(screen, RED, [100,100], [100,90], 2)
# Arms
pygame.draw.line(screen, RED, [100,90], [104,100], 2)
pygame.draw.line(screen, RED, [100,90], [96,100], 2)

7. Show how to grab the mouse coordinates, and then grab just the x coordinate of where the mouse is.

8. When we created a bouncing rectangle, we multiplied the speed times -1 when the rectangle hit the edge of the screen. Explain why that technique won’t work for moving an object with the keyboard.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s