<More Python Programming for the Absolute Beginner>一书中的第二章练习3(P33)

使用Python的Pygame库

import sys, pygame, random    #导入库文件
from pygame.locals import * pygame.init()  #初始化 white= 0, 0, 0  #定义颜色
pos_x= 300    #定义初始位置
pos_y= 250
vel_x= 0.2
vel_y= 0.1 screen= pygame.display.set_mode((600, 500))  #获取窗口对象
#myfont= pygame.font.Font(None, 60)
#textImage= myfont.render("Hello, Pygame!", True, white)
pygame.display.set_caption("Drawing Moving Rectangle")  #窗口名字 color_r= 255
color_g= 0
color_b= 0
color= color_r, color_g, color_b  #定义初始颜色 while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(white) pos_x+= vel_x
pos_y+= vel_y if pos_x> 500 or pos_x< 0:  #若x轴超出范围,则
  color_r= random.randint(0, 255)  
  color_g= random.randint(0, 255)
   color_b= random.randint(0, 255)
   color= color_r, color_g, color_b    #则矩形边的颜色随机改变
   vel_x= -vel_x  #速度方向相反 if pos_y> 400 or pos_y< 0:  #类似上面
  color_r= random.randint(0, 255)
  color_g= random.randint(0, 255)
  color_b= random.randint(0, 255)
  color= color_r, color_g, color_b
  vel_y= -vel_y position= pos_x, pos_y, 100, 100  
width= 1
pygame.draw.rect(screen, color, position, width)  #画矩形 # screen.blit(textImage, (100, 100))
pygame.display.update()  #更新

第一个Pygame程序,如有建议,敬请提出。

欢迎交流。

最新文章

  1. 玩转spring boot——MVC应用
  2. 请注意,再次记住, centos7,fedora 24中 没有iptables服务, 而使用的firewalld, 也可以安装 iptables-services程序来实现
  3. Spring 4 官方文档学习 Spring与Java EE技术的集成
  4. Yocto开发笔记之《根文件系统裁剪》(QQ交流群:519230208)
  5. Mastering C# structs
  6. 添加图片按钮-UI界面编辑器(SkinStudio)教程
  7. phalcon框架学习之router
  8. python datetime 时间日期处理小结
  9. Dev ComboxTree的实现
  10. JQuery无法获取动态添加的图片宽度问题解决办法
  11. 树莓派高级GPIO库,wiringpi2 for python使用笔记(四)实战DHT11解码
  12. redirection in linux
  13. CodeForces - 893C Rumor【并查集】
  14. Codeforces Round #412 B. T-Shirt Hunt
  15. Docker3之Swarm
  16. Node学习笔记:建立TCP服务器和客户端之间的通信
  17. Hadoop错误集:Could not find the main class: org.apache.hadoop.*
  18. pta l2-20(功夫传人)
  19. 使用canvas绘制扇形图
  20. AutoCompleteTextView

热门文章

  1. Python数据分析扩展库
  2. MVC进阶之路:依赖注入(Di)和Ninject
  3. NAND FLASH ECC校验原理与实现
  4. 封装的localstorge的插件,store.js
  5. c++程序内存泄露检測工具
  6. 练习笔记:net,JqueryUI实现自动补全功能
  7. struts的MVC详细实现
  8. ASP.NET MVC Framework
  9. iOS现有工程 集成 Cordova/Ionic
  10. iOS 支持arm_64 和 x86_64 的OpenSSL 静态库(libcrypto.a, libssl.a)