Skip to content

Commit fc47db5

Browse files
authored
Create items.py
1 parent dbf3b21 commit fc47db5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Shadow-Labyrinth/items.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pygame
2+
import random
3+
4+
TILE_SIZE = 40
5+
6+
class Item(pygame.sprite.Sprite):
7+
def __init__(self, item_type, walls):
8+
super().__init__()
9+
self.type = item_type
10+
colors = {
11+
'battery': (0, 255, 255), # Cyan
12+
'torch': (255, 165, 0), # Orange
13+
'health': (255, 0, 0), # Red
14+
'speed': (0, 255, 0) # Green
15+
}
16+
self.image = pygame.Surface((TILE_SIZE // 4, TILE_SIZE // 4))
17+
self.image.fill(colors[item_type])
18+
self.rect = self.image.get_rect(center=self.random_position(walls))
19+
20+
def random_position(self, walls):
21+
pos = (random.randint(TILE_SIZE, 800 - TILE_SIZE), random.randint(TILE_SIZE, 600 - TILE_SIZE))
22+
temp_rect = self.rect.copy()
23+
temp_rect.center = pos
24+
while any(temp_rect.colliderect(wall.rect) for wall in walls):
25+
pos = (random.randint(TILE_SIZE, 800 - TILE_SIZE), random.randint(TILE_SIZE, 600 - TILE_SIZE))
26+
temp_rect.center = pos
27+
return pos

0 commit comments

Comments
 (0)