File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments