add enemy

This commit is contained in:
Aron Petau 2025-10-11 15:40:28 +02:00
parent ab1f1cdaea
commit 466812a64f
10 changed files with 83 additions and 4 deletions

3
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

8
.idea/cdec.iml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.12 (smims-2024)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (smims-2024)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (smims-2024)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cdec.iml" filepath="$PROJECT_DIR$/.idea/cdec.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

BIN
images/enemy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
images/player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
results/s2_2025.tif Normal file

Binary file not shown.

View file

@ -6,7 +6,7 @@ import rasterio
pygame.init()
WIDTH, HEIGHT = 1920, 1080
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Water Bottle Shooter")
@ -20,7 +20,7 @@ PLAYER_SPEED = 5
WATER_SIZE = 10
WATER_SPEED = 10
PLAYER_IMAGE_PATH = "player.png" # Path to your player image
PLAYER_IMAGE_PATH = "images/player.png" # Path to your player image
try:
PLAYER_IMAGE = pygame.image.load(PLAYER_IMAGE_PATH).convert_alpha()
PLAYER_IMAGE = pygame.transform.scale(PLAYER_IMAGE, (PLAYER_SIZE, PLAYER_SIZE))
@ -28,6 +28,15 @@ except Exception as e:
print(f"Could not load player image: {e}")
PLAYER_IMAGE = None
ENEMY_IMAGE_PATH = "images/enemy.png" # Path to your player image
try:
ENEMY_IMAGE = pygame.image.load(ENEMY_IMAGE_PATH).convert_alpha()
ENEMY_IMAGE = pygame.transform.scale(ENEMY_IMAGE, (PLAYER_SIZE, PLAYER_SIZE))
except Exception as e:
print(f"Could not load player image: {e}")
ENEMY_IMAGE = None
clock = pygame.time.Clock()
# Load raster map as background
@ -56,7 +65,7 @@ def load_full_map_surface(path):
print(f"Could not load map background: {e}")
return None, WIDTH, HEIGHT
MAP_PATH = "results/lst.tif"
MAP_PATH = "results/s2_2025.tif"
map_surface, MAP_W, MAP_H = load_full_map_surface(MAP_PATH)
@ -91,6 +100,37 @@ class Player:
else:
pygame.draw.rect(win, PLAYER_COLOR, (self.x - offset_x, self.y - offset_y, PLAYER_SIZE, PLAYER_SIZE))
class Enemy:
def __init__(self, x, y):
self.x = x
self.y = y
self.rect = pygame.Rect(self.x, self.y, PLAYER_SIZE /2, PLAYER_SIZE /2)
self.direction = "up" # Default direction
def move(self, keys):
if keys[pygame.K_UP]:
self.y -= PLAYER_SPEED
self.direction = "up"
if keys[pygame.K_DOWN]:
self.y += PLAYER_SPEED
self.direction = "down"
if keys[pygame.K_LEFT]:
self.x -= PLAYER_SPEED
self.direction = "left"
if keys[pygame.K_RIGHT]:
self.x += PLAYER_SPEED
self.direction = "right"
# Clamp to map boundaries
self.x = max(0, min(MAP_W - PLAYER_SIZE, self.x))
self.y = max(0, min(MAP_H - PLAYER_SIZE, self.y))
self.rect.topleft = (self.x, self.y)
def draw(self, win, offset_x, offset_y):
if ENEMY_IMAGE:
win.blit(ENEMY_IMAGE, (self.x - offset_x, self.y - offset_y))
else:
pygame.draw.rect(win, PLAYER_COLOR, (self.x - offset_x, self.y - offset_y, PLAYER_SIZE, PLAYER_SIZE))
class WaterBottle:
def __init__(self, x, y, direction):
@ -117,6 +157,7 @@ class WaterBottle:
def main():
# Start player in center of map
player = Player(MAP_W // 2, MAP_H // 2)
enemy = Enemy(MAP_W // 3, MAP_H // 3)
water_bottles = []
running = True
@ -132,7 +173,7 @@ def main():
keys = pygame.key.get_pressed()
player.move(keys)
enemy.move(keys)
for bottle in water_bottles:
bottle.move()