3

私はPyGameに比較的慣れていません。画面上のマウスの位置を示す文字列を表示する簡単なプログラムを作成しようとしています。

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,400),0,32)
myFont = pygame.font.SysFont('arial', 14)

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

    x,y = pygame.mouse.get_pos()
    label = myFont.render('mouse coords: ' + str(x) + ', ' + str(y), 1, (0,128,255))

    screen.blit(label, (10,10))
    pygame.display.update()

マウスを動かすと、テキストが読めなくなるまでラベルがぼやけます。screen.blit() と pygame.display.update() を正しく呼び出していると確信していますが、ラベルが更新されていないようです! どんな助けでも素晴らしいでしょう。

4

1 に答える 1

4

あなたがする必要があるのは、ループ内で背景をブリットすることです。

次のようにします。

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,400),0,32)
myFont = pygame.font.SysFont('arial', 14)

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

    x,y = pygame.mouse.get_pos()
    label = myFont.render('mouse coords: ' + str(x) + ', ' + str(y), 1, (0,128,255))
    screen.fill((0,0,0))
    screen.blit(label, (10,10))
    pygame.display.update()

このようにして、各更新の間に画面を黒で塗りつぶすので、マウスの位置がブリットされ、塗りつぶされてクリアされ、新しい位置がブリットされます。

于 2013-06-30T23:57:42.327 に答える