pygame学习的第一天
pygame最小开发框架:
import pygame, sys pygame.init()
screen = pygame.display.set_mode((600, 480))
pygame.display.set_caption("Pygame游戏之旅") while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
pygame.display.update()
pygame壁球小游戏(不可控制版):
import pygame, sys pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed[0], speed[1])
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)
fclock.tick(fps)
pygame.display.update()
pygame壁球小游戏(键盘控制速度版):
import pygame, sys pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] -= 1
elif event.key == pygame.K_RIGHT:
speed[0] += 1
elif event.key == pygame.K_DOWN:
speed[1] += 1
elif event.key == pygame.K_UP:
speed[1] -= 1 ballrect = ballrect.move(speed[0], speed[1])
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)
fclock.tick(fps)
pygame.display.update()
游戏屏幕的设置:
pygame.display.set_mode(r = (0, 0), flags = 0)
r是游戏屏幕的分辨率,采用(width, height)方式输入
flags用来控制显示类型,可用|组合使用,常用显示标签如下:
pygame.RESIZABLE 窗口大小可调
pygame.NOFRAME 窗口没有边界显示
pygame.FULLSCREEN 窗口全屏显示
pygame壁球游戏(可调整窗口大小版):
import pygame, sys pygame.init()
vInfo = pygame.display.Info()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] -= 1
elif event.key == pygame.K_RIGHT:
speed[0] += 1
elif event.key == pygame.K_DOWN:
speed[1] += 1
elif event.key == pygame.K_UP:
speed[1] -= 1
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
ballrect = ballrect.move(speed[0], speed[1])
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)
fclock.tick(fps)
pygame.display.update()
pygame壁纸游戏(全屏版):
import pygame, sys pygame.init()
vInfo = pygame.display.Info()
size = width, height = vInfo.current_w, vInfo.current_h
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] -= 1
elif event.key == pygame.K_RIGHT:
speed[0] += 1
elif event.key == pygame.K_DOWN:
speed[1] += 1
elif event.key == pygame.K_UP:
speed[1] -= 1
elif event.key == pygame.K_ESCAPE:
sys.exit()
ballrect = ballrect.move(speed[0], speed[1])
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)
fclock.tick(fps)
pygame.display.update()
pygame壁球游戏(最小化可暂停版):
import pygame, sys pygame.init()
vInfo = pygame.display.Info()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] -= 1
elif event.key == pygame.K_RIGHT:
speed[0] += 1
elif event.key == pygame.K_DOWN:
speed[1] += 1
elif event.key == pygame.K_UP:
speed[1] -= 1
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
if pygame.display.get_active():
ballrect = ballrect.move(speed[0], speed[1])
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)
fclock.tick(fps)
pygame.display.update()
pygame学习的第一天的更多相关文章
- pygame学习笔记(3)——时间、事件、文字
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率 上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...
- RabbitMQ学习总结 第一篇:理论篇
目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...
- 学习KnockOut第一篇之Hello World
学习KnockOut第一篇之Hello World 笔者刚开始学习KnockOut.写的内容就相当于一个学习笔记.且在此处向官网致敬,比较喜欢他们家的Live Example版块,里面有jsFiddl ...
- ActionBarSherlock学习笔记 第一篇——部署
ActionBarSherlock学习笔记 第一篇--部署 ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...
- Java学习记录第一章
学习Java第一章的记录,这一章主要记录的是Java的最基础部分的了解知识,了解Java的特性和开发环境还有Java语言的优缺点. 计算机语言的发展大概过程:机器语言--->汇编语言---> ...
- oracle学习笔记第一天
oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字 1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...
- javascript的ES6学习总结(第一部分)
ES6(ESNext学习总结——第一部分) ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,2015.06 发版. ECMA每年6月份,发布一个版本 201 ...
- Web基础学习---HTML 第一天
Web基础学习---HTML 第一天 1 HTML标签 2.CSS Web开发基础HTML好吧离开Python几天...如何学好前端?? 多去看别人的网站.多看.多写.多练,(知乎.36Kr.)多练就 ...
- QT学习之第一个程序
QT学习之第一个程序 目录 手动创建主窗口 居中显示 添加窗口图标 显示提示文本 Message Box的应用 手动连接信号与槽 手动创建主窗口 窗口类型 QMainWindow: 可以包含菜单栏.工 ...
随机推荐
- Fluent_Python_Part3函数即对象,06-dp-1class-func,一等函数与设计模式
使用一等函数实现设计模式 中文电子书P278 合理利用作为一等对象的函数,把模式中涉及的某些类的实例替换成简单的函数,从而简化代码. 1. 重构"策略"模式 中文电子书P282 P ...
- Java工作流引擎关于数据加密流程(MD5数据加密防篡改)
关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单 拖拽式表单 工作流系统 流程数据加密 md5 数据保密流程数据防篡改 ...
- Webflux是什么东东
转自:百家号-薇薇心语 各位Javaer们,大家都在用SpringMVC吧?当我们不亦乐乎的用着SpringMVC框架的时候,Spring5.x又悄(da)无(zhang)声(qi)息(gu)的推出了 ...
- Bugku-CTF社工篇之王晓明的日记
- 样式计算的几种方式与兼容写法:getComputedStyle¤tStyle&style
window.getComputedStyle(element,[string]) 1参为需要获取样式的元素,2参指定伪元素字符串(如“::after”,不需要则为null),设置2参可获取eleme ...
- linux(centos6.10)下去掉mysql的强密码验证
vim /etc/my.cnf shift + G 光标移到最下方: o 进入插入模式,同时换行: 添加一行语句: validate_password=OFF 保存退出. servi ...
- 201771010131-王之泰 实验一 软件工程准备—<通读《现代软件工程—构建之法》后所思所想>周学习总结
项目 内容 作业所属课程 https://www.cnblogs.com/nwnu-daizh/ 作业要求 https://www.cnblogs.com/nwnu-daizh/p/12369881. ...
- iOS马甲包上架总结
https://www.jianshu.com/p/da0a259338ea iOS马甲包上架首先明白一点,这个上架的app马甲包一定是不合规的.不然也不会使用马甲包上架. 上架过程中遇到的坑. 因为 ...
- UIKeyWindow的设置
新建一个纯代码iOS项目,需要对AppDelegate文件和项目的Info.plist文件做一番配置. 第一步:将Info.plist中的下面两项的value删除掉(保留空字符串),如下图 第二步:在 ...
- SVM总结(参考源码ml.hpp)
如何使用,请查阅我的另一篇博客——SVM的使用 1.setType() SVM的类型,默认SVM::C_SVC.具体有C_SVC=100,NU_SVC=101,ONE_CLASS=102,EPS_SV ...