简体   繁体   English

如何从另一个脚本更新 pygame window?

[英]How can I update pygame window from another script?

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

I want to separate "getKey" function from my script and use it from another script.我想从我的脚本中分离“getKey”function 并从另一个脚本中使用它。 Actually I can seperate it but I don't know how can I update pygame window from another script by using this getKey function.实际上我可以将它分开,但我不知道如何使用此 getKey function 从另一个脚本更新 pygame window。 What should I do?我应该怎么办?

For example:例如:

main.py-- 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 just start pygame window but the other script which contain getKey function update this window. Main.py 只是启动 pygame window 但包含 getKey function 的其他脚本更新此 Z05B8C74CBD9706FBF2DE44C1。

You need to pass win to the getKey function:您需要将win传递给getKey function:

def getKey(win, keyInput):
    # [...]
while True:
    # [...]

    if True in flag:
        reset = True
        getKey(win, keyInput)

    # [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM