Pygame基础知识

建立一个Pygame程序

#导入库
import pygame,sys

#导入Pygame的本地常量,这样导入可以避免使用pygame.locals的形式使用
from pygame.locals import *
#初始化pygame库
pygame.init()

创建一个窗口

#返回一个pygame.surface对象
screen=pygame.display.set_mode((400,300))

设置窗口标题

pygame.display.set_caption("Hello,world")

游戏循环和游戏状态

while True:
    for event in pygame.event.get():

以上是游戏主循环,主要进行以下三件事情

  1. 处理事件
  2. 更新游戏状态
  3. 在屏幕上绘制游戏状态

pygame.event.Event对象

当用户触发了某些动作,Pygame库就会创建一个pygame.event.Event对象来记录这个动作,也就是事件

#获取事件列表
pygame.event.get()

QUIT事件和pygame.quit()函数

if event.type == QUIT:
    pygame.quit()
    sys.exit()

type是一个成员变量,用于表示event对象的类型

在退出之前,总是先调用pygame.quit()会使得Pygame库停止工作

pygame.display.update()

以上函数表示,对屏幕进行刷新

颜色对象

#通过元组的形式构造函数
color=(255,0,0)
#最后一个参数表示透明度
color=(255,0,0,1)

#通过对象的形式构造函数
mycolor=pygame.Color(255,0,0)

在任何需要传递颜色的函数中,可以传入元组,也可以传入颜色对象

矩形对象

用元组表示矩形区域

  1. 左上角的X坐标
  2. 左上角的Y坐标
  3. 矩形的宽度(以像素为单位)
  4. 矩形的高度(以像素为单位)

创建RECT对象

spamRect=pygame.Rect(10,20,200,300)

RECT对象有一些属性

image-20210123192111557

image-20210123192120303

基本图形绘制

#绘制线条
pygame.draw.line(screen,color,star_pos,end_pos,width)

#绘制圆
#object screeen
#tuple  color
#tuple  position
#int    radius
#int    width
pygame.draw.circle(screen,(255,0,0),(200,100),20,1)

#设置标题
#string title
pygame.draw.set_caption("Drawing")

#绘制矩形
pygame.draw.rect(screen,color,pos,width)
#eg:pos=(pos_x,pos_y,100,100),后面两个参数表示的是矩形的宽度和高度
#eg:pygame.draw.rect(screen,(255,0,0),pos,0)

#屏幕 颜色  位置  起始角度    结束角度    宽度
#注:弧形是画在左上角的位置,因此位置参数有:坐标,长度、宽度
#注:角度是使用弧度,因此需要进行转换,可以使用math.radians(度数)进行转换
pygame.draw.arc(screen,color,pos,start_angle,end_angle,width)

#eg:pygame.draw.arc(screen,(255,0,0),(100,100,300,100),math.radians(0),math.radians(180),20)

#多边形绘制
#由多条边组成,pointlist参数是一个元组或者点的列表,width是一个可选参数,如果为0,则表示填充
pygame.draw.polygon(surface,color,pointlist,width)

#绘制椭圆,bounding_rectangle可以是一个pygame.Rect对象或者是一个4个整数的元组,不用指定圆心
pygame.draw.ellipse(surface,color,bounding_rectangle,width)
screen=pygame.draw.set_mode((300,400))
#填充颜色
screen.fill(color)

pygame.PixelArray对象

如果想要绘制单个像素,除了使用line来进行绘制外,可以创建一个pixelArray对象

#返回的对象将会锁定surface对象,锁定后不能使用blit()方法绘制图片
pixel=screen.PixelArray()

#使用get_locked()判断是否锁定,锁定返回True,没有锁定返回False
pixle.get_locked()

#设置单个像素的颜色
pixle[480][380]=(255,0,0)

#使用del语句进行解锁

Clock对象

pygame.time.Clock()对象可以帮助我们确保程序以某一个最大的FPS运行,Clock对象将会在游戏循环中每一次迭代上都设置一个小小的暂停,从而确保游戏不会运行太快

#创建Clock对象
fpsClock=pygame.time.Clock()
#在每次刷新后,调用tick方法
#参数传入帧速率
fpsClock.tick(30)

绘制图像

如果想要加载文件中的图像,需要使用load()方法进行加载

#返回一个surface对象,与显示surface对象不同
catimage=pygame.image.load('cat.png')

可以使用blit()对象将surface对象复制到显示对象中

#将catimage复制到screen中,并在第二参数中提供坐标
screen.blit(catimage,(catx,caty))

字体

#创建一个pygame.font.Font对象,字体和大小
myfont=pygame.font.Font(None,32)

#创建一个surface对象,并使用render将文字绘制在上面
fontImage = myfont.render("hello",True,(255,0,0))
import pygame
from pygame.locals import *
import sys

pygame.init()

screen = pygame.display.set_mode((600,600))

myfont = pygame.font.Font(None,32)
fontimage= myfont.render("hello",True,(255,0,0))

#设定文字的坐标
fontx=275
fonty=275
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        #按下按键,文字位置进行反应
        if event.type == KEYDOWN:
            if event.key == K_UP:
                fonty=fonty-5
            if event.key == K_DOWN:
                fonty=fonty+5
            if event.key == K_RIGHT:
                fontx=fontx+5
            if event.key == K_LEFT:
                fontx=fontx-5

    #不断进行刷新绘制           
    screen.fill((255,255,255))
    pygame.draw.rect(screen,(255,0,0),(200,200,200,200),2)
    screen.blit(fontimage,(fontx,fonty))
    pygame.display.update()

抗锯齿

#第二个参数表示抗锯齿效果
myfont.render("hello",True,(255,0,0))
#参数与line相同,不过带有抗锯齿效果
pygame.draw.aaline()
pygame.draw.aalines()

播放声音

#创建pygame.mixer.Sound对象
soundobj=pygame.mixer.Sound()

#播放声音
soundobj.play()

#停止声音
soundobj.stop()

#停止一秒
time.sleep(1)
#载入背景音乐
pygame.mixer.music.load("back.wav")

#播放背景音乐
#-1表示永远循环,如果填写2,表示循环两次播放
#0.0表示,从开始播放,如果填写13.5,则表示从开始播放到13.5的地方
pygame.mixer.music.play(-1,0.0)

#停止背景音乐
pygame.mixer.music.stop()
© 版权声明
THE END
喜欢就支持以下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容