如何在 pygame 窗口中单击的任何地方绘制正方形



你可能从标题中知道我想做什么,但这里有一个简单的例子:

#User clicks somewhere in the pygame window
pos = cursorPosition()
#Function/Class that creates a square where the user clicked.

我试过这个:

import pygame
import sys
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
pos = pygame.mouse.get_pos()
class Create():
cx, cy = pygame.mouse.get_pos()
square = pygame.Rect(cx, cy, 50, 50)
def cube(self):
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
pygame.draw.rect(screen, (255, 0, 0), self.square)
pygame.display.flip()
create = Create()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
create.cube()
screen.fill((0, 255, 0))
pygame.display.flip()

pygame.quit()
sys.exit()

但它只是给了我一个蓝屏,当我单击任何地方时,它不会做任何事情,所以如果你能帮助我,将不胜感激。谢谢!

编辑:我已经设法做到了,但现在我面临另一个问题: 只有当我按住鼠标按钮时,才会出现正方形。 这是代码

import pygame
import sys
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
pos = pygame.mouse.get_pos()
class Create():
cx, cy = pygame.mouse.get_pos()
square = pygame.Rect(cx, cy, 50, 50)
def cube(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
pygame.draw.rect(screen, (255, 0, 0), self.square)
pygame.display.flip()
create = Create()
while running:
screen.fill((0, 255, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
create.cube()
pygame.display.flip()

pygame.quit()
sys.exit()

很高兴看到您正在自己努力解决这个问题,并且您正在用您到目前为止所做的工作来编辑问题!

我添加到代码中的内容基本上允许用户更改立方体的绘制位置。如果您想绘制多个立方体,其位置基于用户的鼠标点击,请编辑您的问题以添加这些详细信息,并在下面发表评论。

首先,我写了一个名为Cube的新类,它本质上具有与Create相同的代码。我不会详细介绍,但通常在面向对象编程中,对象是名词,它们的方法是动作。你的类正好相反,这不是通常编写面向对象代码的方式。

我添加了update()方法,该方法只是使用鼠标位置更新对象的某些字段。您的原始代码正在定义类字段静态变量。我不会在这里详细介绍,但是如果我们要制作 100 个立方体实例,我们希望拥有所有立方体的位置,对吧?在这种情况下,您是在对象上操作,而不是对类进行操作。

然后,有一个变量在第一次鼠标单击后设置为 true,结果,立方体开始被绘制到屏幕上。

这是固定代码:

import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self): 
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
cube.update()
drawing_cube = True     
screen.fill((0, 255, 0))
if drawing_cube:
cube.draw()
pygame.display.flip()

pygame.quit()
quit()

希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!

我也遇到了这个问题,经过很多混乱和沮丧之后,我想出了以下内容:

import sys
import pygame
import time
pygame.init()
white = (255, 255, 255)
red = (255, 0, 0)
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("OwO")
mouse_cords = []
clicked = False
def draw(cord_list, zero):
''' This is what will draw on the screen when the mouse clicks using recursion'''
pygame.draw.circle(screen, red, cord_list[zero], 100, 1)
zero += 1
if len(cord_list) > zero:
draw(cord_list, zero)
done = False
clock = pygame.time.Clock()

while not done:
for event in pygame.event.get(): 
if event.type == pygame.QUIT:
done = True 
screen.fill(white)
if pygame.mouse.get_pressed()[0]:
mouse_cords.append(pygame.mouse.get_pos())
clicked = True
if clicked == True:
draw(mouse_cords, 0)
pygame.display.flip()
clock.tick(60)
pygame.quit()

如您所知,我使用递归来解决此问题。抱歉,这是圆圈而不是盒子,但我相信您可以弄清楚如何更改它。

我是一个初学者开发人员,我的方法可能不是最有效的,但它确实有效。我知道我写得晚了 2 年,但我写这篇文章是为了以防有人和我有同样的问题,即通过按下鼠标按钮或一般任何事件创建多个矩形、正方形或圆形。

我实现了一个类来创建 Cube 对象,就像 Michael O'Dwyer 在他的解释中所写的那样,虽然他的解决方案可以一次创建一个立方体,但我需要为自己创建多个立方体。

除了他的解决方案之外,我刚刚创建了一个列表"cubeList",并且使用每个鼠标按钮事件,我创建了一个新的多维数据集对象并将其附加到该cubeList中。然后,我使用 for 循环来循环访问 cubeList 中的每个多维数据集对象,并使用了 draw 方法。这样,我就可以同时绘制多个立方体,我相信可能有更有效或更好的方法来做到这一点,但这对我有用。

import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

Square1 = pygame.Rect(30,30,60,60)
Square2 = pygame.Rect(90,30,60,60)
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx - 25, self.cy - 25, 50, 50)

def draw(self):
pygame.draw.rect(WIN, GREEN, self.square)

def main():
cubeCount = 0
run = True
cubeList = []
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Ending")
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
cube = Cube()
cubeList.append(cube)
cube.update()

clock.tick(60)
WIN.fill((BLUE))
pygame.draw.rect(WIN,RED, Square1)
pygame.draw.rect(WIN, GREEN, Square2)
for x in cubeList:
x.draw()



pygame.display.flip()


pygame.quit()
if __name__ == "__main__":
main()

最新更新