Python游戏开发笔记之pygame库(一)

记录pygame的基本框架和事件响应。

pygame安装

pip install pygame

开发框架

import pygame,sys

pygame.init()
speed=[1,1]
ball=pygame.image.load("ball.gif")
ballrect=ball.get_rect()

width,height=600,400
screen=pygame.display.set_mode((width,height))
pygame.display.set_caption("标题")
clock=pygame.time.Clock()

while True:

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_UP:
                speed[1]+=1
    ballrect=ballrect.move(speed[0],speed[1])

    clock.tick(60)
    screen.fill("black")
    screen.blit(ball,ballrect)
    pygame.display.update()

屏幕窗口-pygame.display

  • .set_mode((width,height),flags=0) #设置窗口大小
    flags:pygame.RESIZABLE 可调,pygame.NOFRAME 无边界,pygame.FULLSCREEN 全屏。
  • .Info() #获得窗口信息,.current_w,.current_h 获得宽度和高度。
  • .get_active() #最小化时False
  • .set_icon(surface) #设置图标
  • .set_caption(“text”) #设置标题
  • .flip() #重绘整个窗口
  • .update() #重绘变化区域

位图操作-pygame.transform

  • 加载:surface=pygame.image.load(
  • 缩放:surface=pygame.transform.scale(surface,size) #平滑缩放 smoothscale
  • 提取:surface=surface.subsurface([left,top,width,height])
  • 翻转:surface=pygame.transform.flip(surface,xbool,ybool)
  • 旋转:surface=pygame.transform.rotate(surface,angle)
  • 生成rect:rect=surface.get_rect()
  • 绘制:screen.blit(surface,rect/(x,y))
  • 移动:rect=rect.move(x,y)

事件响应-pygame.event

  • .get() #获取事件列表
  • .poll() #获取单一事件
  • .clear() #清空事件列表
  • .set_blocked(type/typelist) #禁止事件被保存
  • .set_allowed(type/typelist) #只允许事件被保存
  • .post() #产生自定义事件 (type,dict)

  • pygame.QUIT #退出

  • pygame.ACTIVEEVENT #窗体变化
  • pygame.KEYDOWN #键盘按下
  • pygame.KEYUP #键盘抬起
  • pygame.MOUSEMOTION #鼠标移动
  • pygame.MOUSEBUTTONDOWN #鼠标按下
  • pygame.MOUSEBUTTONUP #鼠标抬起

键盘:

  • pygame.unicode #编码
  • pygame.K_小写字母/ESCAPE/UP/DOWN/… #名称
  • pygame.mode #或运算

鼠标:

  • pygame.pos #坐标
  • pygame.button #左/中/右 1/2/3
  • pygame.rel #相对运动(在pygame.MOUSEMOTION下)

绘制图形-pygame.draw

  • rect(surface,color,rect,width=0
  • polygon(surface,color,pointlist
  • circle(surface,color,pos,radius,width=0) #圆
  • ellipse(surface,color,rect,width=0) #椭圆
  • arc(surface,color,rect,start_angle,stop_angle,width=0) #椭圆弧
  • line(surface,color,start_pos,end_pos,width=0
  • lines(surface,color,close

绘制文字-pygame.freetype

字体文件 C:/windows/Fonts .ttf,.ttc
import pygame.freetype
Font=pygame.freetype.Font(

色彩机制-pygame.Color

  • pygame.Color(name)
  • pygame.Color(r,g,b,) #浅蓝(193,210,240)
  • pygame.Color(rgbvalue)
  • .normalize #归一到(0,1)

二维坐标-pygame.math.Vector2

from pygame import Vector2
vect=pygame.math.Vector2
pos0=vect(0,0)
pos1=vect(1,1)
pos2=vect(2,2)
dist01=pos1-pos0
dist12=pos2-pos1
angle01=pos0.angle_to(dist01) >>>45
angle12=pos1.angle_to(dist01) >>>0

添加音乐-pypame.

音效文件.使用sound类
音乐文件.使用music类

pygame.mixer.init()
sound=pygame.mixer.Sound(<路径>)
pygame.mixer.music.load(<文件>)

音效(sound):

  • .play() #播放
  • .stop() #停止
  • .fadeout() #淡出
  • .set_volume() #设置音量
  • .get_volumn() #获得音量
  • .get_num_channels() #播放次数
  • .get_length() #获取长度

音乐(pygame.mixer.music.):

  • load()
  • play()
  • rewind()
  • stop()
  • pause()
  • unpause()
  • fadeout()
  • set_volume()
  • get_volume()
  • get_busy()
  • set_pos()
  • get_pos()
  • queue()

pygame所支持的功能丰富,游戏的代码量较大,需要创建多个py文件,不宜使用IDLE编写,使用pycharm编写较为方便。pygame通过不断更新图层来实现游戏效果,控制好绘制的先后顺序至关重要。游戏逻辑的编写对基础语法的要求较高,代码量往往较大,适时备注很重要。

Original: https://blog.csdn.net/qq_53715621/article/details/113973626
Author: Mr_Stutter
Title: Python游戏开发笔记之pygame库(一)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781482/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球