29 Jul PMCA Sunday 14:00 Python Homework 21.07.25.
Write code to animate a rectangle in the same way we animated the circle. Make sure that the rectangle moves from left to right and also from right to left. Also move the rectangle from up to down and down to up.
You can use the below starting code:
# write code to create a rectangle/square # animate the rectange to move in the x-direction import pygame, sys, random pygame.init() screen_height = 480 screen_width = 640 screen=pygame.display.set_mode([screen_width, screen_height]) screen.fill([150,222,232]) pygame.display.flip() #create the variables rect_x = 10 rect_y = 30 speed_x = 10 speed_y = 10 clock = pygame.time.Clock() # help us control, how fast the animation is going while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # write animation code # step 1 is clear the previous animation (fill the screen again ) screen.fill([150,222,232]) # step 2 is to update the rect_x and rect_y position # step 3 is to write the conditions for when the rectangle goes # out of the screen # step 4 to draw the actual rectangle pygame.draw.rect(screen, [30,100,200], [rect_x, rect_y, 50, 50]) pygame.display.flip() clock.tick(50)