This article mainly introduces the pygame-based stretch ball effect in Python, involves techniques related to pygame graphics dynamic operations, and comes with a complete source code for readers to download and reference, if you need it, refer to the example in this article to describe the effect of the pygame-based stretch ball in Python. We will share this with you for your reference. The details are as follows:
Running effect:
The Code is as follows:
#A bouncing ballimport sys, pygame__author__ = {'name' : 'Hongten', 'mail' : 'hongtenzone@foxmail.com', 'QQ' : '648719819', 'Version' : '1.0'}pygame.init()size = width, height = 600, 500speed = [1, 1]black = 249, 130, 57screen = pygame.display.set_mode(size)ball = pygame.image.load('c:\\py\\ball.png')ballrect = ball.get_rect()while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = - speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip()
Click here to download the complete instance code.
I hope this article will help you with Python programming.