首页 > 解决方案 > PyGame 中 PAINT 程序的问题

问题描述

所以,我是一个初学者级别的程序员,我认为用 PyGame (Python) 制作一个绘画程序会很酷。

这样做时我遇到了一些问题:

import pygame


class Brush:
    def __init__(self, color, x, y, radius, location, center):
        self.color = color
        self.x = x
        self.y = y
        self.radius = radius
        self.location = location
        self.center = center

    def draw(self):
        pygame.draw.circle(self.location, self.color, self.center, self.radius)

    def getMouse(self):
        pass




pygame.init()

brushX = 400
brushY = 300

colorWhite = (255, 255, 255)
radius = 15

thickness = 0

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Paint")


brush = Brush(colorWhite, brushX, brushY, radius, screen, pos)

run = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()



    brush.draw()
    pygame.display.update()

以上是我的代码。

所以,我的问题是如何主动获取鼠标位置,并在 Brush 类中更改它?

谢谢!

标签: pythonclasspygamemousepaint

解决方案


用这个:

pygame.mouse.get_pos()

要获取鼠标的位置,它会返回一个元组,因此如果要将其分配给两个不同的变量,则可以执行以下操作:

x, y = pygame.mouse.get_pos()

要将其添加到您的代码中,您可能需要执行以下操作:

while run:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()

mouseX, mouseY = pygame.mouse.get_pos()
brush.x = mouseX
brush.y = mouseY

brush.draw()
pygame.display.update()

推荐阅读