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: 可以包含菜单栏.工 ...
随机推荐
- ubuntu 虚拟机添加多个站点
我们安装好lamp环境,然后开始操作,比如一个站点叫test.ubuntu1.com,一个叫test.ubuntu2.com 1.修改hosts文件,路径/etc/hosts sudo vim /et ...
- 模块学习-shutil
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(s ...
- kubernetes的Ingress资源介绍
Ingress 的资源帮助信息介绍 [root@master ~]# kubectl explain ingress KIND: Ingress VERSION: extensions/v1beta1 ...
- 寒假pta一
打印沙漏 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐 ...
- 重新梳理IT知识之java-01语法(一)
标识符的命名规范 包名:xxxyyyzzz 类名.接口名:XxxYyyZzz (大驼峰) 变量名.方法名:xxxYyyZzz 常量名:XXX_YYY_ZZZ //**************强制类型转 ...
- 杭电 1114 Piggy-Bank 完全背包问题
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- github日常的基本命令
git 常用命令 git clone 仓库地址 -从远端克隆项目 git pull -从远端拉取代码 git pull -p -从远端拉取代码和分支 提交代码流程: git add xxx -添加到暂 ...
- springboot中关于datasource的配置
datasource spring.dao.exceptiontranslation.enabled: 是否开启PersistenceExceptionTranslationPostProcessor ...
- broadcom sdk command
1.查看端口link状态 BCM.0>ps 2.查看vlan BCM.0>vlan show 3.查看pvlan BCM.0>pvlan show 4.CPU发包 BCM.0> ...
- HTML标签,CSS简介
一 http://www.w3school.com.cn/tags/tag_span.asp