24 Aug PMCA Sunday 14:00 Python Homework 21.08.22.
In the previous class, we wrote the code to display the image on the screen. We also modified the code to move the image to the right. The code for displaying the image is as below. Modify the below code, to bounce the image in both x-direction and y-direction.
# understanding how to load images into pygame # download the file and save it at the same location as the place # where you store the python file # create a pygame window import pygame, random, sys pygame.init() screen_height = 480 screen_width = 640 screen=pygame.display.set_mode([screen_width, screen_height]) screen.fill([150,222,232]) # load the image into pygame # if you image name is something else, please write that name image = pygame.image.load("beach_ball.png") # display the image on screen at position x = 200 and y = 200 screen.blit(image, [200,200]) pygame.display.flip() speed_x = 1 pos_x = 100 clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # write the code to move the to the left and right screen.fill([150,222,232]) screen.blit(image, [pos_x,200]) pos_x = pos_x + speed_x pygame.display.flip() clock.tick(36)