JavaScript
x
26
26
1
import pygame
2
from pygame.constants import( QUIT, KEYDOWN, KEYUP, K_LEFT, K_RIGHT, K_ESCAPE, K_SPACE,
3
MOUSEBUTTONDOWN)
4
import os
5
from random import randint
6
7
8
class Settings:
9
w_width = 800
10
w_height = 600
11
w_border = 50
12
file_path = os.path.dirname(os.path.abspath(__file__))
13
image_path = os.path.join(file_path, "pictures")
14
15
16
17
18
class Background(object):
19
def __init__(self, filename):
20
self.imageo = pygame.image.load(os.path.join(Settings.image_path, filename))
21
self.image = pygame.transform.scale(self.imageo, (Settings.w_width, Settings.w_height)).convert()
22
self.rect = self.image.get_rect()
23
24
def draw(self, screen):
25
screen.blit(self.image, self.rect)
26
I have here the bubbles class and i want to do it that when you click a bubble it despawns but i dont know how the bubbles detect the mouse cursor and how the bubbles despawn. I already tried to add the mouse position function but I dont know hot to use it. And I searched on the Internet but I found nothing about that.
JavaScript
1
33
33
1
class Bubble(pygame.sprite.Sprite):
2
def __init__(self):
3
super().__init__()
4
self.width = 50
5
self.height = 300
6
self.image = pygame.image.load(os.path.join(Settings.image_path, "Blase.png ")).convert_alpha()
7
self.image = pygame.transform.scale(self.image, (25, 25))
8
self.rect_br = (Settings.w_width//2) + 100, (Settings.w_height//2) + 100
9
self.rect = self.image.get_rect()
10
self.mousex, self.mousey = pygame.mouse.get_pos()
11
self.cx = self.width - 25
12
self.cy = self.height - 25
13
self.rect.left = randint(Settings.w_border, Settings.w_width - Settings.w_border)
14
self.rect.top = randint(Settings.w_border, Settings.w_height - (Settings.w_border * 2))
15
16
17
def update(self):
18
pass
19
20
def scale_up(self):
21
self.scale["width"] += 2
22
self.scale["height"] += 2
23
24
def scale_down(self):
25
self.scale["width"] -= 2
26
self.scale["height"] -= 2
27
28
def events(self):
29
pass
30
31
def draw(self, screen):
32
screen.blit(self.image,self.rect )
33
Here is where everythings run from the code
JavaScript
1
42
42
1
class Game(object):
2
def __init__(self):
3
self.screen = pygame.display.set_mode((Settings.w_width, Settings.w_height))
4
self.clock = pygame.time.Clock()
5
self.runn = False
6
self.background = Background("Hintergrund.png")
7
self.bubble = pygame.sprite.Group()
8
9
10
def run(self):
11
self.runn = True
12
while self.runn:
13
self.clock.tick(60)
14
self.watch_for_events()
15
self.draw()
16
self.events()
17
18
19
def events(self):
20
if len(self.bubble)< 100:
21
self.bubble.add(Bubble())
22
time.sleep(0.2)
23
24
def draw(self):
25
self.background.draw(self.screen)
26
self.bubble.draw(self.screen)
27
28
pygame.display.flip()
29
30
def watch_for_events(self):
31
for event in pygame.event.get():
32
if event.type == QUIT:
33
self.runn = False
34
35
36
if __name__ == '__main__':
37
os.environ['SDL_VIDEO_WINDOWS_POS'] = "50, 1100"
38
pygame.init()
39
game = Game()
40
game.run()
41
pygame.quit()
42
Thanks for Helping
Advertisement
Answer
“Despawn” a bubble by calling pygame.sprite.Sprite.kill
. kill
remove
the Sprite from all Groups. Use the MOUSEBUTTONDOWN
event and pygame.Rect.collidepoint
to determine if a bubble is clicked:
JavaScript
1
13
13
1
class Game(object):
2
# [...]
3
4
def watch_for_events(self):
5
for event in pygame.event.get():
6
if event.type == QUIT:
7
self.runn = False
8
9
if event.type == MOUSEBUTTONDOWN:
10
for bubble in self.bubble:
11
if bubble.rect.collidepoint(event.pos):
12
bubble.kill()
13