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: 可以包含菜单栏.工 ...
随机推荐
- nginx-cache
test.conf proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m; server { listen 80; server_name ...
- SQL 层级数据查询出树形状态
WITH TEST AS (SELECT DEPTID,PARENTDEPT,SORTORDER,1 SPAC,CONVERT(CHAR(200),RTRIM(DEPTID)+CONVERT(CHA ...
- 2019牛客暑期多校训练营(第十场) Han Xin and His Troop (高精度+拓展中国剩余定理)
题意 裸题 思路 题中的模数之间并不互质,所以应该用拓展中国剩余定理. 但是交上去会炸,__int128过不了,所以用高精度的板子或者java大数都挺好过的. 这里推荐java大数,因为高精度板子用起 ...
- BUG搬运工:CSCun88303-CIMC Memory Leak : Can't SSH/HTTP to CIMC
Symptom:Unable to SSH/HTTP to the CIMC of the C-series server, however the CIMC can be pinged. Also ...
- 任意值运动框架Move模块 js
function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { retur ...
- C语言-调试
1 格式化输出函数printf("%d %s",a,str):格式化控制符之间不能有“逗号”,可以用空格 1.1格式化输入函数scanf(“%d”,t)格式化控制符之间不能有空格 ...
- 什么是SOA架构
什么是SOA架构 SOA是Service-Oriented Architecture的首字母简称,它是一种支持面向服务的架构样式.从服务.基于服务开发和服务的结果来看,面向服务是一种思考方式.其实SO ...
- DoublyLinkedList(双向链表)
本来还不会写双向链表的,但最近学习了二叉树后,突然意识到这不就是双向链表嘛,然后通过对二叉树的理解,实现了一下双向链表. 代码: #define SIZE 10 DouLL * head, *n, * ...
- url转码。
Javascript 中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,deco ...
- 【K-means算法】matlab代码实例学习
1. MATLAB函数Kmeans 使用方法:Idx=Kmeans(X,K)[Idx,C]=Kmeans(X,K) [Idx,C,sumD]=Kmeans(X,K) [Idx,C,sumD,D]=Km ...