python 常用函数用法

pygame

初始化

pygame.init()

创建窗口

_screen_ = pygame.display.set_mode((_x_, _y_))
pygame.display.set_caption(_name(string)_)

显示图片

_screen_.blit(_image_, (_x_, _y_))

加载图片

_name_ = pygame.image.load(_image_)

修改图片

_name_ = pygame.transform.rotozoom(_origine_name_, _rad_, _k_of_size_)

获取图片长宽

_x_, _y_ = _image_.get_size()

加载汉字

FONT= pygame.font.Font(pygame.font.match_font('Comic Sans MS'), _size_)

打印文字

screen.blit(font.render(_string_, False, '_color_'), (_x_, _y_))

常用按键检测

# events
for event in pygame.event.get():
    # quit
    if event.type == pygame.QUIT:
        sys.exit()
    if event.type == pygame.KEYDOWN:
        # W 119        1073741906
        if event.key == 119:
            hero.derta_y -= 1
            history_W = True
        # S 115        1073741905
        if event.key == 115:
            hero.derta_y += 1
            history_S = True
        # A 97         1073741904
        if event.key == 97:
            hero.derta_x -= 1
            history_A = True
        # D 100        1073741903
        if event.key == 100:
            hero.derta_x += 1
            history_D = True
        # W 119        1073741906
        if event.key == 1073741906:
            hero.beita_y -= 1
            history_W = True
        # S 115        1073741905
        if event.key == 1073741905:
            hero.beita_y += 1
            history_S = True
        # A 97         1073741904
        if event.key == 1073741904:
            hero.beita_x -= 1
            history_A = True
        # D 100        1073741903
        if event.key == 1073741903:
            hero.beita_x += 1
            history_D = True
        # K 107 spped
        if event.key == 112:
            if GAME == True:
                GAME = False
                pause_time = time.time()
            else:
                GAME = True
                strt_time += (time.time() - pause_time)
    if event.type == pygame.KEYUP:
        # W 119
        if event.key == 119 and history_W:
            hero.derta_y += 1
        # S 115
        if event.key == 115 and history_S:
            hero.derta_y -= 1
        # A 97
        if event.key == 97 and history_A:
            hero.derta_x += 1
        # D 100
        if event.key == 100 and history_D:
            hero.derta_x -= 1
        # W 119        1073741906
        if event.key == 1073741906 and history_W:
            hero.beita_y += 1
        # S 115        1073741905
        if event.key == 1073741905 and history_S:
            hero.beita_y -= 1
        # A 97         1073741904
        if event.key == 1073741904 and history_A:
            hero.beita_x += 1
        # D 100        1073741903
        if event.key == 1073741903 and history_D:
            hero.beita_x -= 1 up
        if event.key == 107 and time.time() > hero.nxt_time:
            hero.sped_up = True
            hero.sped_up_time = time.time() + 3
            hero.speed = 0.4
            hero.nxt_time = hero.sped_up_time + 9
        # PAUSE P 112

math

开根 sqrt()

dr = math.sqrt(x)

开数组

_a_ = []

time

获取当前时间

_time_ = time.time()

none

在数组中加入元素

_a_.append(_element_)

文件输入输出

with open('_txt_', 'r')as TT:
    lst_record = float(TT.read())
with open('_txt_', 'w')as TT:
    TT.write(str(t))
posted @ 2023-10-04 14:35  Richard_H  阅读(47)  评论(0)    收藏  举报