脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python基于pygame实现的弹力球效果(附源码)

(编辑:jimmy 日期: 2025/6/22 浏览:3 次 )

本文实例讲述了Python基于pygame实现的弹力球效果。分享给大家供大家参考,具体如下:

运行效果:

Python基于pygame实现的弹力球效果(附源码)

Python基于pygame实现的弹力球效果(附源码)

代码部分如下:

#A bouncing ball
import sys, pygame
__author__ = {'name' : 'Hongten',
       'mail' : 'hongtenzone@foxmail.com',
       '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()

完整实例代码代码点击此处本站下载。

希望本文所述对大家Python程序设计有所帮助。

上一篇:python基于pygame实现响应游戏中事件的方法(附源码)
下一篇:Python中pygame安装方法图文详解
一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。