forked from emilyk123/Titanium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.py
59 lines (46 loc) · 2.13 KB
/
power.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pygame
import random
# Import random module for random operations
from tilemap import Tilemap
class PowerUp:
def __init__(self, screen_width, screen_height, tilemap):
self.screen_width = screen_width
self.screen_height = screen_height
#
self.tilemap = tilemap
self.color = (255, 0, 0)
self.width = 16
self.height = 16
# Set initial random position
#*self.x = random.randint(0, self.screen_width - self.width) #***********
#*self.y = random.randint(0, self.screen_height - self.height)
self.randomize_position()
def draw(self, surface):
# Draw the power-up at its current position
pygame.draw.rect(surface, self.color, pygame.Rect(self.x, self.y, self.width, self.height))
#----------------------------------------
#def randomize_position(self):
# tile_x = random.randint(0, self.screen_width // self.tilemap.tile_size - 1)
# tile_y = random.randint(0, self.screen_height // self.tilemap.tile_size - 1)
# self.x = tile_x * self.tilemap.tile_size
# self.y = tile_y * self.tilemap.tile_size
def randomize_position(self):
# Loop until a non-water tile position is found
max_attempts = 100
for attempt in range(max_attempts):
tile_x = random.randint(0, self.screen_width // self.tilemap.tile_size - 1)
tile_y = random.randint(0, self.screen_height // self.tilemap.tile_size - 1)
position = (tile_x * self.tilemap.tile_size, tile_y * self.tilemap.tile_size)
tile_type = self.tilemap.get_tile_type(position)
if tile_type != 'water':
self.x, self.y = position
return # Exits once a valid position is found
# If no valid tile is found after max_attempts, print a warning
print("Warning: Could not find a non-water tile for the power-up.")
def collision(self, other):
return (
self.x < other.x + other.width and
self.x + self.width > other.x and
self.y < other.y + other.height and
self.y + self.height > other.y
)