python - 鼠标点击矩形的困境

标签 python click pygame mouse blit

所以我的想法是,我有一个 windowSurface.blit 矩形,当我用鼠标左键单击按下它时,它将执行这些命令 MOVE_SPEED = 9 end_it_levels = True 但我无法包裹我的头围绕如何做到这一点。鼠标功能可以在这里找到http://www.pygame.org/docs/ref/mouse.html#comment_pygame_mouse_get_pos

buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)

end_it_levels = False
while (end_it_levels == False):
    windowSurface.fill(WHITE)
    textlevelFont = pygame.font.SysFont("impact", 26)
    level = textlevelFont.render("LEVEL:", True, (BLACK))
    level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
                                        , True, (BLACK))
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_1:
            MOVE_SPEED = 9
            end_it_levels = True
        if event.type == KEYDOWN and event.key == K_2:
            MOVE_SPEED = 7
            end_it_levels = True
        if event.type == KEYDOWN and event.key == K_3:
            MOVE_SPEED = 5
            end_it_levels = True
    windowSurface.blit (level, (290, 115))
    #windowSurface.blit (level_levels, (140, 150))
    windowSurface.blit(buttonEasyImage, buttonEasy)
    windowSurface.blit(buttonNormalImage, buttonNormal)
    windowSurface.blit(buttonHardImage, buttonHard)
    pygame.display.flip()

基本上就是这里的代码,但是您单击鼠标左键并在 blit 的坐标内 [buttonEasy = pygame.Rect(10, 150, 200, 90)]

for event in pygame.event.get():
    if event.type == KEYDOWN and event.key == K_1:
        MOVE_SPEED = 9
        end_it_levels = True

类似的新困境

end_it = False
while (end_it == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
    if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
        mouse_coordinates = pygame.mouse.get_pos()
        if buttonStart.collidepoint(mouse_coordinates):
            end_it_levels = True
windowSurface.blit(buttonStartImage, buttonStart)
pygame.display.flip()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 直到具体循环的代码:

import pygame, sys, random, math
from pygame.locals import *
from threading import Timer


pygame.init()
mainClock = pygame.time.Clock()

WINDOW_WIDTH = 640
WINDOW_HEIGHT = 400
windowSurface = pygame.display.set_mode ((WINDOW_WIDTH,
WINDOW_HEIGHT), 0)
pygame.display.set_caption('Catch the rabbits!')

BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)

textFont = pygame.font.SysFont("impact", 60)
text = textFont.render("YOU WIN!", True, (193, 0, 0))

rabbitCounter = 0
NEW_RABBIT = 40
RABBIT_SIZE = 64
buttonStartImage = pygame.image.load('buttonStart.png')
background_image = pygame.image.load('bg.jpg').convert()
playerImage = pygame.image.load('Fox.png')
playerImageTwo = pygame.image.load('Fox2.png')
rabbitImage = pygame.image.load('topic_rabbit.png')
rabbitImageTwo = pygame.image.load('topic_rabbit2.png')
buttonEasyImage = pygame.image.load('buttonEasy.png')
buttonNormalImage = pygame.image.load('buttonNormal.png')
buttonHardImage = pygame.image.load('buttonHard.png')
player = pygame.Rect(420, 100, 40, 40)
buttonStart = pygame.Rect(220, 150, 200, 90)
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
rabbits = []
for i in range (20):
    rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
    - RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
    RABBIT_SIZE, RABBIT_SIZE))

moveLeft = False
moveRight = False
moveUp = False
moveDown = False

MOVE_SPEED = 0

end_it = False
while (end_it == False):
    windowSurface.fill(WHITE)
    for event in pygame.event.get():
        if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
            mouse_coordinates = pygame.mouse.get_pos()
            if buttonStart.collidepoint(mouse_coordinates):
                end_it_levels = True
    windowSurface.blit(buttonStartImage, buttonStart)
    pygame.display.flip()

最佳答案

您将必须添加额外的事件来检查。有一个 Rect 方法专门用于这种事情,称为 collidepoint

buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)

end_it_levels = False
while (end_it_levels == False):
    windowSurface.fill(WHITE)
    textlevelFont = pygame.font.SysFont("impact", 26)
    level = textlevelFont.render("LEVEL:", True, (BLACK))
    level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
                                        , True, (BLACK))
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_1:
            MOVE_SPEED = 9
            end_it_levels = True
        if event.type == KEYDOWN and event.key == K_2:
            MOVE_SPEED = 7
            end_it_levels = True
        if event.type == KEYDOWN and event.key == K_3:
            MOVE_SPEED = 5
            end_it_levels = True
        # Is the left mousebutton being clicked?
        if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
            mouse_coordinates = pygame.mouse.get_pos()
            if Your_Rect.collidepoint(mouse_coordinates):
                # do stuff...
    windowSurface.blit (level, (290, 115))
    #windowSurface.blit (level_levels, (140, 150))
    windowSurface.blit(buttonEasyImage, buttonEasy)
    windowSurface.blit(buttonNormalImage, buttonNormal)
    windowSurface.blit(buttonHardImage, buttonHard)
    pygame.display.flip()

我不太确定这是否是您想要做的,但我希望这会有所帮助。

关于python - 鼠标点击矩形的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21365285/

相关文章:

javascript - 如何在每次点击时将 +1 添加到文本值?

Python Launcher 没有响应 Pygame

python - 尝试制作动画,但程序会在此之前关闭

python - VS Code 运行 python 比 IDLE 慢 2-3 倍

python - 使用多处理模块的脚本不会终止

python - 在 Django 中使用 Cython。是否有意义?

python - 在 Python 中将正则表达式与 fileinput 结合使用会出现错误

javascript:长按一个书签

jquery-ui - DIV 上的单击事件在 IE 中不起作用

python - 打开和关闭 Pygame WIndows 时出现段错误