【问题标题】:Dilemma with mouse clicking in a rectangle鼠标单击矩形时的困境
【发布时间】:2014-01-26 15:29:36
【问题描述】:

所以我的想法是我有一个 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()

【问题讨论】:

  • 您知道MOUSEBUTTONDOWN 事件是否被捕获但坐标不匹配?

标签: python click pygame mouse blit


【解决方案1】:

您将不得不添加额外的事件来检查。有一个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()

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

【讨论】:

  • 不知道为什么但是点击这个按钮不起作用。 [在那里]
  • 做到了。我不知道是否需要发布其余代码。
  • 还有其他人有想法/技巧吗?
  • @user3232394 “它工作完美地满足了我的要求”发生了什么?
  • 哦,没关系,我发现了一个错字,抱歉。 end_it_levels = True 应该是 end_it = True
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多