PyGame中绘画程序的问题

2024-09-29 19:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我是一个初级程序员,我认为用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()

上面是我的代码

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

谢谢


Tags: selfeventinitdefdisplaylocationpygamecolor
1条回答
网友
1楼 · 发布于 2024-09-29 19:26:41

使用以下命令:

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

相关问题 更多 >

    热门问题