So, i am trying to create a game where aliens spawn from 3 specific places. Each Alien will spawn randomly in one of the 3. But there will always be at least one alien, that will spawn on top of another one. I want to delete that alien and spawn him randomly in another spawn point. If it is empty he will stay if not the process will be repeated. The thing is that i cannot find a way to detect collision of 2 objects that are in the same group.
I just started learning pygame so 1) My question may be stupid 2) My way of spawning probably is very inefficient
Here is the Alien class:
JavaScript
x
24
24
1
class Alien(pygame.sprite.Sprite):
2
def __init__(self):
3
pygame.sprite.Sprite.__init__(self)
4
self.image = pygame.Surface((80,60))
5
self.image.fill(GREY)
6
self.rect = self.image.get_rect()
7
spawn_point1 = x1,y1 = -30, 70
8
spawn_point2 = x2,y2 = -30, 150
9
spawn_point3 = x3,y3 = -30, 230
10
random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
11
self.rect.center = random_spawn
12
self.speedx = 10
13
14
def update(self):
15
spawn_point1 = x1,y1 = -30, 70
16
spawn_point2 = x2,y2 = -30, 150
17
spawn_point3 = x3,y3 = -30, 230
18
self.speedx = 10
19
random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
20
self.rect.x += self.speedx
21
22
if self.rect.x > WIDTH + 20:
23
self.rect.center = random_spawn
24
And here is the part where i detect collision(This part doesnt work)
JavaScript
1
7
1
aliens_col = pygame.sprite.groupcollide(aliens, aliens, True, False)
2
3
for i in aliens_col:
4
alien = Alien()
5
aliens.add(alien)
6
all_sprites.add(aliens)
7
Advertisement
Answer
Here is an implementation of the Bounding Box test.
JavaScript
1
52
52
1
import random
2
3
4
class Rectangle:
5
6
def __init__(self, height, width, x, y):
7
self.height = height
8
self.width = width
9
self.x = x
10
self.y = y
11
12
def collided_with_another_rectangle(self, rect):
13
""" Assumes rectangles are same size or that this rectangle is smaller than the other rectangle"""
14
if self.x > (rect.x + rect.width):
15
# Is to the right of the other rectangle
16
return False
17
elif (self.x + self.width) < rect.x:
18
# is to the left of the other rectangle
19
return False
20
elif (self.y + self.height) < rect.y:
21
# is above the other rectangle
22
return False
23
elif self.y > (rect.y + rect.height):
24
# is below the other rectangle
25
return False
26
else:
27
return True
28
29
30
collision_count = 0
31
for i in range(0, 1000):
32
# Here I pick random locations on a 1000X1000 screen for the first rectangle
33
x1 = random.randint(0, 1000)
34
y1 = random.randint(0, 1000)
35
# Here I pick random locations on a 1000X1000 screen for the second rectangle
36
rect1 = Rectangle(100, 100, x1, y1)
37
x2 = random.randint(0, 1000)
38
y2 = random.randint(0, 1000)
39
rect2 = Rectangle(100, 100, x2, y2)
40
"""
41
I use the collided with another rectangle function to test if the first rectangle is above,below,
42
to the right or to the left of the other rectangle. If neither of these are true then the rectangles
43
have collided.
44
"""
45
if rect1.collided_with_another_rectangle(rect2):
46
collision_count += 1
47
print("Rect1 X and Y:" + str(x1) + " " + str(y1))
48
print("Rect2 X and Y:" + str(x2) + " " + str(y2))
49
print("collided")
50
51
print("Collision Count:" + str(collision_count))
52