[英]Python. Pygame - Interactive button does not respond
為了澄清起見,我仍然是python的新手,並且試圖制作兩個按鈕來改變顏色,這是由於鼠標的交互作用所致。 我正在使用一個教程( Tutorial Link ),它使用鼠標坐標來觸發一個新的pygame rect,使其顯示在當前的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()
你所要做的一切里面while intro
在旁邊,您可以使用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.