Oct-07-2019, 02:12 AM
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.
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