python - 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 类中更改它?
谢谢!
解决方案
用这个:
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()
推荐阅读
- python - Python - 在 setup.py 安装之前和之后导入包模块
- django - 如何仅在 Django 管理更改列表中自定义字段名称?
- javascript - Chrome - background-size:cover;-based 画廊导致性能问题
- opencv - 带有 CUDA 的 OpenCV
- python - 在 Python 中使用 Google 时获取与查询匹配的段落
- java - 将属性写入 URL
- javascript - 克隆后如何删除dragula中的元素?
- javascript - 选择性地交错两个数组
- javascript - 获取嵌套 JSON 对象的最小值和最大值
- java - Keycloak terms.ftl 文件网址