Python Forum
Sprite on mouse-button hold
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sprite on mouse-button hold
#1
I'm working with pygame 1.9.6
I am trying to make a program that displays a translucent sprite only when the left mouse-button is held down.
This sprite is supposed to track with the mouse position, show the background through it, and disappear when the mouse-button is released.

Currently:
It is tracking with the mouse position.
It is stamping a new copy instead of just moving the sprite.
The sprite(s) are not disappearing when the mouse-button is released.
The sprite alpha is not working, they are a solid color.

Here is the complete code. It is relatively short.
import pygame
import sys
from pygame.locals import *

pygame.init()

LTGRAY = (199, 210, 199)
HLIGHT = ( 80, 100,   0)
BLACK  = (  0,   0,   0)

main_surface = pygame.display.set_mode((900, 650), 0, 32)
main_surface.fill(LTGRAY)

clock = pygame.time.Clock()
fps = 30

pygame.draw.line(main_surface, BLACK,
                     (0, 0), (900, 650), 3)
pygame.draw.line(main_surface, BLACK,
                     (900, 0), (0, 650), 3)

class hlight(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50), flags=pygame.SRCALPHA)
        self.image.fill(HLIGHT)
        self.image.set_alpha(35)
        self.rect = self.image.get_rect()
        
    def update(self, pos):
        self.rect.x, self.rect.y = pos
        
highlights = pygame.sprite.Group()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif pygame.mouse.get_pressed()[0]:
            if len(highlights) == 0:
                highlights.add(hlight())
        elif event.type == pygame.MOUSEBUTTONUP:
            for highlight in highlights:
                highlight.kill()
        
    highlights.update(pygame.mouse.get_pos())
    highlights.draw(main_surface)
    pygame.display.update() 
    
    clock.tick(fps)
Thanks for any help
Reply


Messages In This Thread
Sprite on mouse-button hold - by Clunk_Head - Oct-07-2019, 02:12 AM
RE: Sprite on mouse-button hold - by Windspar - Oct-07-2019, 11:34 AM
RE: Sprite on mouse-button hold - by Clunk_Head - Oct-08-2019, 04:31 AM
RE: Sprite on mouse-button hold - by Windspar - Oct-09-2019, 12:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,616 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,162 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,258 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,611 May-15-2019, 12:52 PM
Last Post: pfaber11
  [PyGame] Need Help With Sprite ghost0fkarma 2 3,300 Jan-09-2018, 02:14 PM
Last Post: ghost0fkarma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020