本文實(shí)例講述了Python基于pygame實(shí)現(xiàn)的彈力球效果。分享給大家供大家參考,具體如下:
運(yùn)行效果:
代碼部分如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#A bouncing ball import sys, pygame __author__ = { 'name' : 'Hongten' , 'QQ' : '648719819' , '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.blit(ball, ballrect) pygame.display.flip() |
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對大家Python程序設(shè)計有所幫助。