Main.py 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
'''
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


# 游戏初始化
pygame.init()  # 初始化pygame


# 加载图片并转换
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)


# 游戏初始化
def GameInit(screen, Windows):
    """
    背景绘制
    """
    screen.blit(
        pygame.transform.scale(
            background, Windows["deafultresolution"]),  # 背景图片【自适应大小】
        (0, 0)  # 绘制坐标
    )

# 绘制鼠标
# 涉及到渲染层级问题,所以单独将鼠标绘制在最下方
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"]

    """
    游戏主循环
    """
    while True:
        """
        游戏窗口创建
        参数:分辨率,窗口模式,颜色深度
        """
        screen = pygame.display.set_mode(
            Windows["deafultresolution"],
            Windows["windowsmode"],
            Windows["colordepth"]
        )
        pygame.display.set_caption(
            GameInfo["title"]  # 窗口标题设置
        )

        """
        游戏开始
        """
        GameInit(screen, Windows)

        """
        鼠标绘制
        """
        GameCursor(screen)
        # 刷新画面
        pygame.display.update()
        # pygame.display.flip()

        # 判断退出
        for event in pygame.event.get():
            if(event.type == QUIT):
                pygame.quit()
                # 接收到退出时间后退出程序
                exit()