how to create a blinking or glowing button using pygame

Vipul

I'm new to pygame and I want to know how to make a rect object which blinks or glows on or without a mousebutton event. Thanks in advance !

Serial

I whipped this up:

from pygame.locals import *
import pygame
import os
import sys


pygame.init()

CAPTION = "TEST"

class Game():
    def __init__(self):
        #window setup
        pygame.display.set_caption(CAPTION)
        os.environ["SDL_VIDEO_CENTERED"] = "True"
        self.fps = 60.0

        self.clock = pygame.time.Clock()
        self.last_tick = pygame.time.get_ticks()
        self.screen_res = [200, 200]

        self.screen = pygame.display.set_mode(self.screen_res,pygame.HWSURFACE)

        self.rect = pygame.Surface((100, 100))
        self.rect.fill((250, 0,0))

        self.alpha = 1
        self.a_change = True
        #Tweak this to change speed
        self.blink_spd = 0.1

        #start loop
        self.clock.tick(self.fps)
        while 1:
            self.Loop()

    def Loop(self):
        # main game loop
        self.eventLoop()

        self.last_tick = pygame.time.get_ticks()

        self.screen.fill((0,0,0))
        #Check if alpha is going up
        if self.a_change:
            self.alpha += self.blink_spd
            if self.alpha >= 175:#if all the way up go down
                self.a_change = False
        #Check if alpha is going down        
        elif self.a_change == False:
            self.alpha += -self.blink_spd
            if self.alpha <= 30: #if all the way down go up
                self.a_change = True

        self.rect.set_alpha(self.alpha)
        self.screen.blit(self.rect,(50,50))

        pygame.display.update()


    def eventLoop(self):
        # the main event loop, detects keypresses
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

Game()

use pygame.Surface.set_alpha() to change the transparency, you can tweak the numbers to get different speeds and such

you can replace the Surface with an image because an image is also a surface

Hope this helps! good luck :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create glowing effect with HTML 5

From Dev

How to create glowing effect with HTML 5

From Dev

Create a glowing border in QSS

From Dev

How to draw a glowing halo around elements using Processing 2.0 (Java)?

From Dev

How to create sprites using ConfigParser in Pygame

From Dev

How to create sprites using ConfigParser in Pygame

From Dev

Blinking rectangle in pygame

From Dev

pygame blinking screen fix

From Dev

How create button using reflection?

From Dev

How to make a button stop blinking when updating title

From Dev

Button blinking on Datatrigger

From Dev

Fixed button on mobile blinking

From Dev

Blinking Button for Simon Says

From Dev

How to create bullets in pygame?

From Dev

How can I create a Xylophone program using pygame mixer or pyaudio?

From Dev

How do I create sprite collision in PyGame using vectors?

From Dev

How to create a button using only JS and HTML

From Dev

How to create a button dynamically using jQuery Mobile?

From Dev

How to create radio button dynamically using javascript?

From Dev

How to create previous button using javascript?

From Dev

python pygame how to debounce a button?

From Java

How to create timers in pygame efficiently?

From Dev

How to fix blinking/flashing

From Dev

glowing text on button with transparent background in layout with other background

From Dev

WPF Ribbon Button glowing effects and changing image when hover

From Dev

WPF Ribbon Button glowing effects and changing image when hover

From Java

How to create custom button in Android using XML Styles

From Dev

How to create submit button in PHP using HTML forms?

From Dev

How to create a Next and Preview button using JQuery? (image gallery)

Related Related

HotTag

Archive