Python实战之Pygame魂斗罗(一)

307 阅读3分钟

说是开发魂斗罗,反正本篇博客没有遇到什么技术难题 说到底还是Pygame太强大了,基本上都是按照Pygame的doc实现

Pygame的官方文档

简单写几个这次出现频率较高的Pygame的方法

函数名臣作用返回值
pygame.display.init()初始化展示模块NONE
pygame.display.set_mode(size = (0, 0))初始化窗口(窗口就是一个Surface对象)Surface
pygame.display.set_caption(title, icontitle = None)设置窗口标题和图标NONE
pygame.Surface.fill(color, rect = None)用纯色填充Surface对象NONE
pygame.Color(b, g, r)创建用于表示颜色的pygame对象Color
pygame.display.update()更新屏幕NONE
pygame.event.get()获取队列事件Evenlist
pygame.image.load(filename)从文件加载图片(图片也是Surface对象)Surface
pygame.Surface.get_rect()获取Surface对象的rect属性Rect
pygame.Surface.blit(source, dest, area = None)在一个Surface对象上显式另一个Surface对象Rect
pygame.font.init()初始化字体模块NONE
pygame.font.SysFont(name, size, bold = False, italic = False)从系统字体创建字体对象Font
pygame.font.render(text, antialias, color, background = None)创建一个带有指定字体的Surface对象Surface
pygame.mixer.init()初始化混合器模块None
pygame.mixer.Sound(filename)从文件创建声音对象Sound
pygame.mixer.Sound.play(loops = 0, maxtime = 0)播放音乐,loops参数控制播放次数,播放次数为loops + 1次。默认为0,表示声音不会重复,只播放一次。如果loops为-1,声音将无限循环Channel
pygame.mixer.Sound.stop()声音停止播放None
pygame.mixer.Sound.set_volume(value)设置音量None
pygame.transform.flip(surface, flip_x, flip_y)图片翻转Surface
pygame.transform.scale(surface, size)将图片(Surface对象)调整为大小为(width, height)的新图片Surface
pygame.time.Clock()用于帮助跟踪时间以及控制游戏帧速率的类Clock
pygame.time.Clock.tick(framerate = 0)设置游戏运行的帧率milliseconds
pygame.time.Clockget_fps()计算游戏的帧速率(以帧每秒为单位)float

附代码:已经实现初始背景图片与背景音乐,自定义图片的放缩、旋转

import sys

import pygame
import time


def getWinEvent():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()  # 退出
        # 鼠标按下事件
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            print('鼠标按下,鼠标位置({x}, {y})'.format(x=x, y=y))
        elif event.type == pygame.MOUSEBUTTONUP:
            print('鼠标抬起')   # 鼠标抬起事件
        elif event.type == pygame.KEYDOWN:
            print('键盘按下')
            if event.key == pygame.K_j:
                print('按下键盘 j 键')
                # 键盘按键抬起事件
        elif event.type == pygame.KEYUP:
            print('键盘抬起')


# 初始化模块展示
pygame.display.init()
SCREEN_WIDTH = 1100
SCREEN_HEIGHT = 600
win_size = (SCREEN_WIDTH, SCREEN_HEIGHT)
window = pygame.display.set_mode(win_size)
pygame.display.set_caption('Contra')

# 此时窗口一闪而过
# 设置背景颜色  RGB模式
blue, green, red = 255, 0, 255
background_color = pygame.Color(blue, green, red)
window.fill(background_color)   # 填充背景色
# 窗口还是一闪而过

# time.sleep(200)  # 程序休眠200 秒    便于窗口界面
path = 'E:\PyCharm\Contra\Image\Map\第一关BG.png'
image = pygame.image.load(path)
rect = image.get_rect()
rect.left = 0
rect.top = 0
window.blit(image, rect)

rect = pygame.Rect(200, 200, 200, 300)
region_color = pygame.Color(50, 50, 50)
window.fill(region_color, rect)
#  为屏幕中的某个区域绘制一定的颜色

'''
# 在窗口中显式文字
text = 'Hello Contra'
pygame.font.init()   # 初始化字体
fontSize = 16
font = pygame.font.SysFont('georgia', fontSize)
fontColor = pygame.Color(255, 255, 255)
fontObject = font.render(text, True, fontColor)
# 创建文字对象
position = (50, 50)
# 展示文字位置
window.blit(fontObject, position)
'''


def enlarge(image: pygame.Surface, scale):
    """
    图片缩放
    :param image:  要缩放的图片
    :param scale:  图片缩放尺寸
    :return:  缩放后的图片(函数返回值)
    """
    rect = image.get_rect()
    largeSize = (int(rect.width * scale), int(rect.height * scale))
    large_picture = pygame.transform.scale(origin, largeSize)
    return large_picture


def flip(image: pygame.Surface, horizontal, vertical):
    """
    实现图片翻转
    :param image:      初始图像
    :param horizontal: 是否水平翻转
    :param vertical:   是否垂直翻转
    :return:           翻转后的图片
    """
    return pygame.transform.flip(image, horizontal, vertical)


# 设置一下背景音乐
# 初始化混合器模块
pygame.mixer.init()
sound_path = './Sound/1.mp3'
# 这里暂且设置成1.mp3   pygame播放m4a好像有些问题
sound = pygame.mixer.Sound(sound_path)
sound.play()

origin = pygame.image.load('./Image/Map/第一关BG.png')
# 显示原图
rect = origin.get_rect()
rect.x = 0
rect.y = 0
window.blit(origin, rect)

# 更新窗口
pygame.display.update()
time.sleep(2)

# 图片缩放2.5倍
large = enlarge(origin, 2.5)
# 显示原图
rect = large.get_rect()
rect.x = 0
rect.y = 0
window.blit(large, rect)

# 更新窗口
pygame.display.update()
time.sleep(2)



# 创建一个对象来帮助跟踪时间,并设置帧率
clock = pygame.time.Clock()
fps = 60


while 1:
    # 获取窗口键盘事件
    # getWinEvent()
    # 更新窗口
    clock.tick(fps)
    r = clock.get_fps()
    caption = 'Contra - {:.2f}'.format(r)  # 帧率显式
    pygame.display.set_caption(caption)
    pygame.display.update()