1.设置变量,很重要,提高可读性
2.编写代码可读性很重要
因为当给自己的程序修改Bug,或添加新的功能的时候会很容易
3.在一些大型程序,要平衡内存和时间
4.颜色:RGB #注明:(0,23,45,128)半透明 第四个参数表示透明度,
湖绿色: ( , , ) 海军蓝:( 0,0 ,128 ) 灰色:( 128,128 128, ) 银色:( 192,192,192 )
黑色: ( 0, 0 ,0 ) 橄榄色:( 128,128 ,0 ) 绿色:( 0,128 ,0 ) 青色:( 0,128 ,128 )
蓝色: ( 0, 0,255 ) 紫色:( 128, 0,128) 浅绿色:( 0,255 ,0 ) 白色:(255 , 255, 255)
紫红色: ( 255,0 ,255 ) 红色:( 255, 0, 0 ) 褐红色:( 128,0 ,0 ) 黄色:( 255,255 ,0 )
5.绘制图形函数:
绘制矩形:
pygame.draw.rect(surface,color,(x,y,width,height))
绘制多边形:
pygame.draw.line(surface,color,pointlist,width)
pointlist:为一个点的列表或元组
width:为可选项,漏掉这个参数,则绘制为填充的;若传递了一个数值:则只绘制出宽度为width的边框
如:pygame.draw.line(SURFACE,BLUE,((2,3),(234,346),(235,22),(0,105))
绘制线:
pygame.line(surface,color,start_point,end_point,width)
pygame.lines(surface,color,closed,pointlist,width)
和:绘制多边形一样,区别是,closed为False时,最后一点不会封口,为True时,封口
绘制圆:
pygame.draw.circle(surface,color,center_point,radius,width)
绘制椭圆:
pygame.draw.ellipse(surface,color,bounding_rectangle,width)
bonding_rectangle为:矩形即:(x,y,width,height)
过程:
import pygame,sys
from pygame.locals import *
1.初始化: pygame.init()
2.FPS和pygame.time.Clock:
FPS:即 帧速率或刷新速率,是程序每秒钟沪指的图像的数目,用FPS或帧/秒度量,常见名称赫兹,很多为60Hz
pygame.time.Clock:可以帮助我们确保程序以摸一个最大的FPS运行,Clock对象将会在游戏循环的每一次迭代上设置一个小小的暂停
,从而确保游戏程序不会运行的太快。
fpsClock = pygame.time.clock()
...
#每次游戏循环的最后调用以下:根据前一次调用tick()之后经过了多长时间,来计算需要暂停多长时间。
pygame.display.update()
fpsClock.tick(FPS)
3.画框:
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
名字: pygame.display.set_caption('name')
游戏循环:
while True:
4.按键处理:
def checkForQuit():
# 功能:检查是否按键按下Esc按键推出
# 输入:无
# 输出:无
for event in pygame.event.get(QUIT):
terminate()
for event in pygame.event.get(KEYUP):
if event.key == K_ESCAPE:
terminate()
pygame.event.post(event) #如果不是K_ESCAPE,则放回KEYUP,不消耗
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
#检查鼠标
elif event.type == MOUSEMOTION:
mousex,mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex,mousey = event.pos
mouseClicked = True
#检查键盘
elif event.type == KEYUP:
if event.key in (K_LEFT, K_a):
pass
if event.key in (K_RIGHT,K_d):
pass
if event.key == K_w:
pass
if event.key == K_UP:
pass
5.文字显示:
my_font = pygame.font.Font(file, size=-1)
#如BASICFONT = pygame.font.Font('freesansbold.ttf',BASICFONTSIZE)
#参数file:采用字体文件的路径,如果file参数设置为None则默认采用系统自带字体 如:'freesansbold.ttf'
#参数size:字体的大小
#返回值:返回一个特定字体对象,可使用该特定字体去定义文本
textsurf = my_font.render(text, antialias, color, background=None)
#参数text:文本字符串;
#参数antialias:抗锯齿 ,为True时文本图像显示更光滑,为False时文本图像显示有锯齿状
#参数color:文本的颜色
#参数background:为文本背景颜色,默认为小黑屏
#返回值:返回一个surface对象(字体的渲染成的图像)
#设置图片位置
textRect = textsurf.get_rect()
textRect.topleft = (top,left)
#使用surface.blit()可将textsurf显示出来
surface.blit(picture,(x,y))
6.查看鼠标在哪个矩形内:
RectSURF = pygame.Rect(left,top,TILESIZE,TILESIZE)
if RectSURF.collidepoint(x,y):
pass
7.产生随机:
values = [a,b,c,...]
return random.choice(values)
8.播放声音:
后续更新…
For Example:
import pygame,sys
from pygame.locals import *
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
FPS = 30
def main():
global SURFACE,FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
SURFACE = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption('贪吃蛇')
while True:
rungame()
def rungame():
for event in pygame.event.get():
if event.type == QUIT:
terminal()
pygame.display.update() #一直更新屏幕才能出来屏幕
FPSCLOCK.tick(FPS)
def terminal():
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()