python 包之 Pygame 游戏开发教程

一、安装

  • pygame可以实现python游戏的开发

pip install pygame

 

二、查看版本号

pip show pygame

 

三、测试内置游戏

  • 运行 pygame 自带的游戏

  • 看是否运行正常

python -m pygame.examples.aliens

 

四、基础使用

  • 初始化一个窗口,设置窗口标题

  • 检测用户关闭事件,进行关闭操作

import pygame, sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500, 400))
 
# 设置窗口标题
pygame.display.set_caption('我的第一个游戏')
 
# 程序主循环
while True:
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
 
  # 绘制屏幕内容
  pygame.display.update()

 

五、设置背景色

  • 通过 screen.fill 方法可以给背景板填充颜色

  • 颜色参数是一个三原色的元组

import pygame, sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500, 400))

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
 
# 设置背景颜色
screen.fill(WHITE)
 
# 设置窗口标题
pygame.display.set_caption('我的第一个游戏')
 
# 程序主循环
while True:
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
 
  # 绘制屏幕内容
  pygame.display.update()

 

六、图形绘制

  • 绘制一条线段:pygame.draw.line(Surface, color, start_pos, end_pos, width)

  • 绘制一条抗锯齿的线:pygame.draw.aaline(Surface, color, start_pos, end_pos, blend)

  • 绘制一条折线:pygame.draw.lines(Surface, color, closed, pointlist, width)

  • 绘制一个矩形:pygame.draw.rect(Surface, color, Rect)

  • 绘制一个矩形框:pygame.draw.rect(Surface, color, Rect, width)

  • 绘制一个多边形:pygame.draw.polygon(Surface, color, pointlist, width)

  • 绘制一条弧线:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width)

  • 绘制一个圆:pygame.draw.circle(Surface, color, Rect, radius)

  • 绘制一个椭圆:pygame.draw.ellipse(Surface, color, Rect)

  • 绘制一个椭圆框:pygame.draw.ellipse(Surface, color, Rect, width)

import pygame, sys
from pygame.locals import *
from math import pi

# 初始化pygame
pygame.init()
 
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((400,300))
 
# 设置窗口标题
pygame.display.set_caption('图形绘制')
 
# 绘制一条线
pygame.draw.line(screen, GREEN, [0, 0], [50,30], 5)
 
# 绘制一条抗锯齿的线
pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80],True)
 
# 绘制一条折线
pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
 
# 绘制一个空心矩形
pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
 
# 绘制一个矩形
pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
 
# 绘制一个空心椭圆
pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
 
# 绘制一个椭圆
pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])
 
# 绘制多边形
pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
 
# 绘制多条弧线
pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2)
pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3*pi/2, 2*pi, 2)
 
# 绘制一个圆
pygame.draw.circle(screen, BLUE, [60, 250], 40)
 
# 程序主循环
while True:
 
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
 
  # 绘制屏幕内容
  pygame.display.update()

 

七、绘制字体

  • 获取字体:pygame.font.Font(filename, size)

  • 显示字体:pygame.font.Font.render(text, antialias, color, background=None)

  • 获取坐标对象:get_rect()

import pygame,sys
from pygame.locals import *

pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("文字绘制")
surface.fill((255, 255, 255))

# 获取字体对象,可以获取系统自带的,也可以自定义字体
fonts = pygame.font.get_fonts()
fonts = 'fonts/ARBERKLEY.ttf'
basicFont = pygame.font.SysFont(fonts, 50)

# surface对象
text = basicFont.render('这是一串字符', True, (255,255,255), (0,255,0))

# 设置文本位置
textRect = text.get_rect()

textRectObj.center = (250, 200)

# 将渲染的surface对象更新到屏幕上
surface.blit(text,textRect)

# 程序主循环
while True:
 
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
 
  # 绘制屏幕内容
  pygame.display.update()

 

八、音频播放

  • 播放特效声音:pygame.mixer.Sound(filename)

  • 加载背景音乐:pygame.mixer.music.load(filename)

import pygame, sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400))
 
# 设置窗口的标题
pygame.display.set_caption('音频播放')
 
# 设置背景
screen.fill((255, 255, 255))
 
# 加载并播放一个特效音频文件
sound = pygame.mixer.Sound('./music.mp3')
sound.play()
 
# 加载背景音乐文件
pygame.mixer.music.load('./bgmusic.mp3')
 
# 播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒)
pygame.mixer.music.play(-1, 0.0)
 
# 程序主循环
while True:
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 停止播放背景音乐
      pygame.mixer.music.stop()
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
 
  # 绘制屏幕内容
  pygame.display.update()

 

九、用户事件

  • 用户按下关闭按钮:QUIT

  • Pygame被激活或者隐藏:ACTIVEEVENT

  • 键盘被按下:KEYDOWN

  • 键盘被放开:KEYUP

  • 鼠标移动:MOUSEMOTION

  • 鼠标按下:MOUSEBUTTONDOWN

  • 鼠标放开:MOUSEBUTTONUP

  • Pygame窗口缩放:VIDEORESIZE

import pygame, sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400), 0, 32)
 
# 设置窗口的标题
pygame.display.set_caption('用户事件监控')
 
# 设置背景
screen.fill((255, 255, 255))
 
# 程序主循环
while True:
  # 获取事件
  for event in pygame.event.get():
    # 判断事件是否为退出事件
    if event.type == QUIT:
      # 退出pygame
      pygame.quit()
      # 退出系统
      sys.exit()
      
    # 获得键盘按下的事件  
    if event.type == KEYDOWN:
      if(event.key==K_UP or event.key==K_w):
        print("上")
      if(event.key==K_DOWN or event.key==K_s):
        print("下")
      if(event.key==K_LEFT or event.key==K_a):
        print("左")
      if(event.key==K_RIGHT or event.key==K_d):
        print("右")
      # 按下键盘的Esc键退出
      if(event.key==K_ESCAPE):
        # 退出pygame
        pygame.quit()
        # 退出系统
        sys.exit()
 
    # 获得鼠标当前的位置  
    if event.type ==MOUSEMOTION:
      print(event.pos)
 
    # 获得鼠标按下的位置
    if event.type ==MOUSEBUTTONDOWN:
      print("鼠标按下:", event.pos)
 
    # 获得鼠标抬起的位置
    if event.type ==MOUSEBUTTONUP:
      print("鼠标抬起:", event.pos) 
 
  # 绘制屏幕内容
  pygame.display.update()

 

posted @ 2022-04-25 09:40  sunnyeden  阅读(674)  评论(0编辑  收藏  举报