1

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

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

2 回答 2

1

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

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

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

我添加了update()简单地用鼠标位置更新对象的一些字段的方法。您的原始代码是定义类字段静态变量。我不会在这里详细介绍,但是如果我们要制作 100 个立方体实例,我们会想要所有立方体的位置,对吗?在这种情况下,您是在对objects进行操作,而不是class进行操作。

然后,有一个变量在第一次鼠标单击后设置为 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()

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

于 2018-09-05T18:47:26.450 回答
1

我也遇到过这个问题,经过一番折腾和挫败后,我想到了以下问题:

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

正如你所知道的,我使用递归来解决这个问题。抱歉,它是圆圈而不是框,但我相信你可以弄清楚如何改变它。

于 2019-03-01T20:13:20.510 回答