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.

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

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.

When drawing a starry background, explain why it doesn’t work to put code like this in the main program loop:

fori inrange(50):

x =random.randrange(0, 400)

y =random.randrange(0, 400)

pygame.draw.circle(screen, WHITE, [x, y], 2)

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]]

This code example causes snow to fall:

# Process each snow flake in the list

fori inrange(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

fori inrange(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?

defdraw_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.