Проблемы с программой PAINT в PyGame - PullRequest
1 голос
/ 07 августа 2020

Итак, я начинающий программист, и я подумал, что было бы круто создать программу 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()

Выше мой код.

Итак, моя проблема в том, как мне получить активное положение мыши и изменить его в классе для Bru sh?

Спасибо!

1 Ответ

1 голос
/ 07 августа 2020

Используйте это:

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()
...