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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

django 发送邮件和缓存的实现代码

发送邮件 概述:Django中内置了邮件发送功能,发送邮件需要使用SMTP服务,常用的免费服务器有:163、126、QQ 注册并登陆163邮箱 打开POP3/SMTP服务与I...

Python骚操作之动态定义函数

在 Python 中,没有可以在运行时简化函数定义的语法糖。然而,这并不意味着它就不可能,或者是难以实现。 from types import FunctionType foo_c...

Python 线程池用法简单示例

本文实例讲述了Python 线程池用法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 ''' Created on 2019-...

ipython和python区别详解

ipython和python区别详解

ipython介绍 IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell命令,内...

python实现获取Ip归属地等信息

如果你有一批IP地址想要获得这些IP具体的信息,比如归属国家,城市等,最好的办法当时是调用现有的api接口来获取,我在之前就写过一篇文章,是关于我的博客被莫名攻击的时,就有获取过一批IP...