Main.py 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'''
FileName: Main.py
Remark: Main.py
Project: JiangHu
Author: EasternDay
File Created: Sunday, 22nd March 2020 10:44:39 pm
Last Modified: Tuesday, 24th March 2020 11:51:56 am
Modified By: EasternDay
-------------------------------------------------------------
Description:
梦开始的地方。
'''

# pygame.locals为pygame中所有按键的静态值
import pygame
from pygame.locals import *
from sys import exit
东方怂天's avatar
东方怂天 已提交
18
from JiangHu.Scripts.Logger import logger
19 20 21 22


# 游戏初始化
pygame.init()  # 初始化pygame
东方怂天's avatar
东方怂天 已提交
23
logger.info("Pygame模块初始化……")
24 25


东方怂天's avatar
东方怂天 已提交
26
# TODO//后期将资源文件读取集成到一个脚本里
27 28 29 30 31 32 33 34 35 36 37
# 加载图片并转换
background_image_filename = r'JiangHu\Src\Background\bg.jpg'
mouse_image_filename = r'JiangHu\Src\Background\0.png'
background = pygame.image.load(background_image_filename)
mouse_cursor = pygame.image.load(mouse_image_filename)

# 隐藏鼠标
pygame.mouse.set_visible(False)


# 游戏初始化
东方怂天's avatar
东方怂天 已提交
38
def GameInit(screen, screensize):
39 40 41 42 43
    """
    背景绘制
    """
    screen.blit(
        pygame.transform.scale(
东方怂天's avatar
东方怂天 已提交
44
            background, screensize),  # 背景图片【自适应大小】
45 46 47 48 49
        (0, 0)  # 绘制坐标
    )

# 绘制鼠标
# 涉及到渲染层级问题,所以单独将鼠标绘制在最下方
东方怂天's avatar
东方怂天 已提交
50 51


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
def GameCursor(screen):
    """
    鼠标绘制
    """
    # 获得鼠标位置
    x, y = pygame.mouse.get_pos()
    # 计算光标左上角位置
    #x -= int(mouse_cursor.get_width() / 2)
    #y -= int(mouse_cursor.get_height() / 2)
    # 将光标画上去
    screen.blit(
        mouse_cursor,  # 鼠标图片
        (x, y)  # 鼠标位置
    )


def Init(Config):
    """
    配置读入
    """
    GameInfo = Config["GameInfo"]
    Windows = Config["Windows"]

东方怂天's avatar
东方怂天 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87
    """
    游戏窗口创建【默认创建方式】
    参数:分辨率,窗口模式,颜色深度
    """
    screen = pygame.display.set_mode(
        Windows["deafultresolution"],
        Windows["windowsmode"],
        Windows["colordepth"]
    )
    pygame.display.set_caption(
        GameInfo["title"]  # 窗口标题设置
    )

88 89 90 91 92 93 94
    """
    游戏主循环
    """
    while True:
        """
        游戏开始
        """
东方怂天's avatar
东方怂天 已提交
95
        GameInit(screen, Windows["deafultresolution"])
96 97 98 99 100

        """
        鼠标绘制
        """
        GameCursor(screen)
东方怂天's avatar
东方怂天 已提交
101 102 103 104

        """
        刷新画面
        """
105
        pygame.display.update()
东方怂天's avatar
东方怂天 已提交
106

107 108
        # pygame.display.flip()

东方怂天's avatar
东方怂天 已提交
109 110 111
        """
        事件监听
        """
112
        for event in pygame.event.get():
东方怂天's avatar
东方怂天 已提交
113 114 115
            """
            判断是否退出
            """
116 117 118 119
            if(event.type == QUIT):
                pygame.quit()
                # 接收到退出时间后退出程序
                exit()
东方怂天's avatar
东方怂天 已提交
120 121 122 123 124 125 126 127 128
            
            """
            用户调整窗口尺寸
            更新屏幕大小信息
            """
            if event.type == VIDEORESIZE:
                Windows["deafultresolution"] = event.size
                print(Windows["deafultresolution"])
                screen = pygame.display.set_mode(Windows["deafultresolution"], RESIZABLE)