【问题标题】:How can I update pygame window from another script?如何从另一个脚本更新 pygame 窗口?
【发布时间】:2022-01-12 07:23:56
【问题描述】:
import pygame
import sys

pygame.init()
win = pygame.display.set_mode((400, 140))

font = pygame.font.SysFont('Arial', 50)


def getKey(keyInput):
    if keyInput[119]:  # W
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(70, 5, 60, 60))
        win.blit(font.render('W', True, (0, 0, 0)), (80, 10))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(70, 5, 60, 60))
        win.blit(font.render('W', True, (0, 0, 0)), (80, 10))

    if keyInput[97]:  # A
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(5, 70, 60, 60))
        win.blit(font.render('A', True, (0, 0, 0)), (20, 70))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(5, 70, 60, 60))
        win.blit(font.render('A', True, (0, 0, 0)), (20, 70))

    if keyInput[115]:  # S
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(70, 70, 60, 60))
        win.blit(font.render('S', True, (0, 0, 0)), (85, 70))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(70, 70, 60, 60))
        win.blit(font.render('S', True, (0, 0, 0)), (85, 70))

    if keyInput[100]:  # D
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(135, 70, 60, 60))
        win.blit(font.render('D', True, (0, 0, 0)), (150, 70))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(135, 70, 60, 60))
        win.blit(font.render('D', True, (0, 0, 0)), (150, 70))

    if keyInput[1073741906]:  # UP
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(270, 5, 60, 60))
        win.blit(font.render('▲', True, (0, 0, 0)), (282, 5))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(270, 5, 60, 60))
        win.blit(font.render('▲', True, (0, 0, 0)), (282, 5))

    if keyInput[1073741904]:  # LEFT
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(205, 70, 60, 60))
        win.blit(font.render('◄', True, (0, 0, 0)), (208, 73))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(205, 70, 60, 60))
        win.blit(font.render('◄', True, (0, 0, 0)), (208, 73))

    if keyInput[1073741905]:  # DOWN
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(270, 70, 60, 60))
        win.blit(font.render('▼', True, (0, 0, 0)), (282, 73))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(270, 70, 60, 60))
        win.blit(font.render('▼', True, (0, 0, 0)), (282, 73))

    if keyInput[1073741903]:  # RIGHT
        pygame.draw.rect(win, [220, 30, 30], pygame.Rect(335, 70, 60, 60))
        win.blit(font.render('►', True, (0, 0, 0)), (350, 73))
    else:
        pygame.draw.rect(win, [255, 100, 0], pygame.Rect(335, 70, 60, 60))
        win.blit(font.render('►', True, (0, 0, 0)), (350, 73))

    pygame.display.update()


keyList = [97, 100, 115, 119, 1073741904, 1073741906, 1073741905, 1073741903]
reset = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keyInput = pygame.key.get_pressed()
    flag = [True if keyInput[i] == 1 else False for i in keyList]

    if True in flag:
        reset = True
        getKey(keyInput)
    else:
        if reset:
            getKey(keyInput)
            reset = False
        else:
            pass

我想将“getKey”函数从我的脚本中分离出来,并从另一个脚本中使用它。实际上我可以将它分开,但我不知道如何使用这个 getKey 函数从另一个脚本更新 pygame 窗口。我该怎么办?

例如:

main.py--

import pygame
import sys
import getKeyFunction 

pygame.init()
win = pygame.display.set_mode((400, 140))

keyList = [97, 100, 115, 119, 1073741904, 1073741906, 1073741905, 1073741903]
reset = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keyInput = pygame.key.get_pressed()
    flag = [True if keyInput[i] == 1 else False for i in keyList]

    if True in flag:
        reset = True
        getKey(keyInput)
    else:
        if reset:
            getKey(keyInput)
            reset = False
        else:
            pass

Main.py 只是启动 pygame 窗口,但包含 getKey 函数的其他脚本会更新此窗口。

【问题讨论】:

    标签: python class user-interface pygame


    【解决方案1】:

    您需要将win 传递给getKey 函数:

    def getKey(win, keyInput):
        # [...]
    
    while True:
        # [...]
    
        if True in flag:
            reset = True
            getKey(win, keyInput)
    
        # [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多