2015-12-24 3 views
0

Просто для того, чтобы уточнить, что я все еще новичок на python и пытаюсь сделать две кнопки, которые меняют цвет из-за взаимодействия с мышью. Я использую учебник (Tutorial Link), и он использует координаты мышей, чтобы вызвать появление нового пигменного прямоугольника над текущим, но другого цвета. У меня проблема, когда я наводил указатель мыши на кнопку, она не меняет цвет. Я знаю, что это не от текущего типа цветов, как я тестировал с красным и зеленым. Любая помощь и вклад оценили :)Python. Pygame - Интерактивная кнопка не отвечает

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() 
+0

Вы должны проверить положение мыши (и перерисовать прямоугольник) внутри 'while intro' – furas

ответ

0

Вы должны сделать все, что внутри 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()