[英]Python. Pygame - Interactive button does not respond
Just to clarify I am still a newbie to python and am trying to make two buttons that change colour due to interaction from the mouse. 为了澄清起见,我仍然是python的新手,并且试图制作两个按钮来改变颜色,这是由于鼠标的交互作用所致。 I am using a tutorial ( Tutorial Link ) and it uses the mouses co-ordinates to trigger a new pygame rect to appear above the current one but in a different colour. 我正在使用一个教程( Tutorial Link ),它使用鼠标坐标来触发一个新的pygame rect,使其显示在当前的pygame rect上方,但颜色不同。 I am getting a problem that when I hover my mouse over the button it does not change colour. 我遇到一个问题,当我将鼠标悬停在按钮上时,它不会改变颜色。 I know that it is not from the current type of colours as I have tested with red and green. 我知道它不是来自我用红色和绿色测试过的当前颜色类型。 Any help and input is appreciated :) 任何帮助和意见表示赞赏:)
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()
You have to do everything inside while intro
你所要做的一切里面while intro
Beside you can use pygame.Rect()
and collidepoint(mouse)
to check mouse hover. 在旁边,您可以使用pygame.Rect()
和collidepoint(mouse)
检查鼠标悬停。
import pygame
# --- constants ---- UPPERCASE
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)
# --- functions ---
def text_objects(text, font):
text_surface = font.render(text, True, BLACK)
return text_surface, text_surface.get_rect()
def game_intro():
large_text = pygame.font.Font('freesansbold.ttf', 115)
text_surf, text_rect = text_objects("Broom!", large_text)
# center using screen
text_rect.center = screen.get_rect().center
button_green_rect = pygame.Rect(75,400,100,50)
button_red_rect = pygame.Rect(550,400,100,50)
intro = True
while intro:
# --- events ---
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
intro = False
mouse = pygame.mouse.get_pos()
# --- draws ----
screen.fill(BLUE)
screen.blit(text_surf, text_rect)
#Button
if button_green_rect.collidepoint(mouse):
pygame.draw.rect(screen, GREEN_BRIGHT, button_green_rect)
else:
pygame.draw.rect(screen, GREEN, button_green_rect)
if button_red_rect.collidepoint(mouse):
pygame.draw.rect(screen, RED_BRIGHT, button_red_rect)
else:
pygame.draw.rect(screen, RED, button_red_rect)
pygame.display.flip()
clock.tick(15)
# --- main ---
pygame.init()
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("BETA::00.0.1")
clock = pygame.time.Clock()
game_intro()
pygame.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.