对象随鼠标移动(Python)

时间:2016-05-08 16:47:24

标签: python graphics pygame

以下代码行,我借用了一个例子来使用。它做的是它需要一个字符串并在pygame中打印出来,并且该行将跟随鼠标移动的位置。我只是想知道如何更改draw.circle的字符串行,我不断收到错误。干杯!代码下面,有问题的字符串是“按下的最后一个按钮是”。

from pygame import * 
init()
size = width, height = 800, 600
screen = display.set_mode(size)
button = 0
BLACK = (0, 0, 0)
RED = (255, 255, 255)
font = font.SysFont("Times New Roman",30)

def drawScene(screen, mx, my, button):
    draw.rect(screen, BLACK, (0, 0, width, height))
    # Draw circle if the left mouse button is down.
    string = "The last button pressed is " + str(button) + "."
    text = font.render(string, 1, RED)
    size = font.size(string)
    screen.blit(text, Rect(mx, my, size[0], size[1]))
    display.flip()

running = True
myClock = time.Clock()

mx = my = 0
# Game Loop
while running:
    for evnt in event.get():             # checks all events that happen
        if evnt.type == QUIT:
            running = False
        if evnt.type == MOUSEBUTTONDOWN:
            mx, my = evnt.pos          
            button = evnt.button
        if evnt.type == MOUSEMOTION:
            mx, my = evnt.pos
    drawScene(screen, mx, my, button)
    myClock.tick(60)                     # waits long enough to have 60 fps

quit()

1 个答案:

答案 0 :(得分:1)

这将在mx,my:

处绘制一个5像素的半径圆
pygame.draw.circle(screen, BLACK, (mx, my), 5)