私は関数の中でdisplay_wまたはdisplay_hを使用できない理由を理解していません。私はそれを再び使用するときにリセットしたくないので、関数の中に入れることはできません。どのように私はそれらの変数を使用することができますか?Pythonは関数内で変数を使用させません
import pygame
pygame.init()
display_w = 800
display_h = 600
white = (255, 255, 255)
black = (0, 0, 0)
grey = (100, 100, 100)
def run():
print("Input your message: ")
msg = input()
print("Enter font size: ")
font_size = int(input())
display = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption("TextPos")
text_x = 0
text_y = 0
def message_to_screen(msg, color, font_size, x, y):
font = pygame.font.SysFont(None, font_size)
screen_text = font.render(msg, True, color)
display.blit(screen_text, [x, y])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
pygame.quit()
run()
if event.key == pygame.K_q:
print("Enter display width: ")
display_w = int(input())
print("Enter display height: ")
display_h = int(input())
running = False
message_to_screen(msg, (255, 255, 255), 50, text_x, text_y)
m_x, m_y = pygame.mouse.get_pos()
text_x = m_x
text_y = m_y
display.fill(white)
message_to_screen("X: %s" % m_x, black, 30, 10, 10)
message_to_screen("Y: %s" % m_y, black, 30, 10, 30)
message_to_screen(msg, black, font_size, text_x, text_y)
message_to_screen("Press Q to Change Screen Size", grey, 30, display_w - 310, 0)
message_to_screen("Press R to Restart", grey, 30, display_w - 180, 30)
pygame.display.update()
run()
あなたの質問は何ですか?どのようなエラーが発生していますか? –