Python Based on pygame to achieve the ball effect (with the source code), pythonpygame
This article describes how to use pygame to create a ball. 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': '123 ', 'version': '1. 0'} pygame. init () size = width, height = 600,500 speed = [1, 1] black = 249,130, 57 screen = 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. BITs (ball, ballrect) pygame. display. flip ()
Click here to download the complete instance code.
I hope this article will help you with Python programming.