蟒蛇。 Pygame - 交互式按钮没有响应

时间:2015-12-24 05:25:57

标签: python button pygame

只是为了澄清我仍然是python的新手,并且我试图制作两个按钮,这些按钮会因为鼠标的交互而改变颜色。我正在使用一个教程(Tutorial Link),它使用鼠标坐标来触发一个新的pygame rect,它出现在当前的一个但是颜色不同。我遇到一个问题,当我将鼠标悬停在按钮上时,它不会改变颜色。我知道它不是来自我用红色和绿色测试的当前颜色类型。任何帮助和输入表示赞赏:)

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]

#Colours
black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_intro():

  screen.fill(blue)

  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects("Broom!", largeText)
  TextRect.center = ((width/2),(height/2))
  screen.blit(TextSurf, TextRect)

  mouse = pygame.mouse.get_pos()

  #Button

  if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
      pygame.draw.rect(screen, green_bright,(75,400,100,50))
  else:
      pygame.draw.rect(screen, green,(75,400,100,50))

  if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
      pygame.draw.rect(screen, red_bright,(550,400,100,50))
  else:
      pygame.draw.rect(screen, red,(550,400,100,50))

  pygame.display.flip()
  pygame.display.update()
  clock.tick(15)

  intro = True

  while intro:
     for event in pygame.event.get():
         print(event)
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()


game_intro()

1 个答案:

答案 0 :(得分:0)

您必须在execlp()

中执行所有操作

您可以使用while intropygame.Rect()来检查鼠标悬停。

collidepoint(mouse)