pygame初步(一)绘制一个运动的矩形
2024-08-25 12:05:52
<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程序,如有建议,敬请提出。
欢迎交流。
最新文章
- 玩转spring boot——MVC应用
- 请注意,再次记住, centos7,fedora 24中 没有iptables服务, 而使用的firewalld, 也可以安装 iptables-services程序来实现
- Spring 4 官方文档学习 Spring与Java EE技术的集成
- Yocto开发笔记之《根文件系统裁剪》(QQ交流群:519230208)
- Mastering C# structs
- 添加图片按钮-UI界面编辑器(SkinStudio)教程
- phalcon框架学习之router
- python datetime 时间日期处理小结
- Dev ComboxTree的实现
- JQuery无法获取动态添加的图片宽度问题解决办法
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(四)实战DHT11解码
- redirection in linux
- CodeForces - 893C Rumor【并查集】
- Codeforces Round #412 B. T-Shirt Hunt
- Docker3之Swarm
- Node学习笔记:建立TCP服务器和客户端之间的通信
- Hadoop错误集:Could not find the main class: org.apache.hadoop.*
- pta l2-20(功夫传人)
- 使用canvas绘制扇形图
- AutoCompleteTextView
热门文章
- Python数据分析扩展库
- MVC进阶之路:依赖注入(Di)和Ninject
- NAND FLASH ECC校验原理与实现
- 封装的localstorge的插件,store.js
- c++程序内存泄露检測工具
- 练习笔记:net,JqueryUI实现自动补全功能
- struts的MVC详细实现
- ASP.NET MVC Framework
- iOS现有工程 集成 Cordova/Ionic
- iOS 支持arm_64 和 x86_64 的OpenSSL 静态库(libcrypto.a, libssl.a)