简体   繁体   English

如何检查该pygame程序中每个矩形的矩形内是否都单击鼠标?

[英]How do I check to see if a mouse click is within a rectangle for each rectangle in this pygame program?

I'm trying to make a pygame program to see if a user clicked on a rectangle, and if so, to change that rectangle's colour. 我正在尝试制作一个pygame程序,以查看用户是否单击了矩形,如果是,则更改该矩形的颜色。

Here's my initial code to draw the screen: 这是我绘制屏幕的初始代码:

import pygame
import sys

pygame.init()

size_x = 650
size_y = 550
bkg_colour = 0, 0, 0
white = 255, 255, 255
colour = 0, 130, 90
position_x, position_y = 70, 100
screen = pygame.display.set_mode((size_x, size_y))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        screen.fill(bkg_colour)

        for x in range(-50, 500, position_x + 20):
            for y in range(-50, 400, position_y + 20):
                pygame.draw.rect(screen, colour, (position_x + x, position_y + y, position_x, position_y), 0)

        pygame.display.flip()

What I tried doing was using pygame.Rect and collisionpoint like this: 我尝试做的是使用pygame.Rect和碰撞点,像这样:

for x in range(-50, 500, position_x + 20):
            for y in range(-50, 400, position_y + 20):
                rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y)
                pygame.draw.rect(screen, colour, rect_size, 0)

if event.type == pygame.MOUSEBUTTONDOWN:
            click = rect_size.collidepoint(pygame.mouse.get_pos())

            if click == 1:
                print("Clicked")
                pygame.draw.rect(screen, white, rect_size, 0)

pygame.display.flip()

But what it does is only work for the last drawn rectangle on the screen. 但是,它仅适用于屏幕上最后绘制的矩形。 How do I make it work for every rectangle on the screen? 如何使它适用于屏幕上的每个矩形?

rect_size holds one value at a time, and you override it again and again until it holds the last rectangle. rect_size保存一个值,然后一次又一次覆盖它,直到它保存最后一个矩形。

you want to hold a list of the rectangles and check collision with all of them. 您想保存一个矩形列表并检查所有矩形的碰撞。

instead of 代替

rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y)

you want to do something like: 您想要做类似的事情:

rect_sizes.append(pygame.Rect(position_x + x, position_y + y, position_x, position_y))

and then iterate rect_sizes and check collision with each one. 然后迭代rect_sizes并检查每个冲突。

rect_sizes = []
for x in range(-50, 500, position_x + 20):
    for y in range(-50, 400, position_y + 20):
        rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y)
        pygame.draw.rect(screen, colour, rect_size, 0)
        rect_sizes.append(rect_size)

if event.type == pygame.MOUSEBUTTONDOWN:
    for rect_size in rect_sizes:
        click = rect_size.collidepoint(pygame.mouse.get_pos())

        if click == 1:
            print("Clicked")
            pygame.draw.rect(screen, white, rect_size, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM