Traktor/source/area/field.py

104 lines
3.5 KiB
Python
Raw Normal View History

2024-03-07 18:01:12 +01:00
# create a field here : 1: add tiles, 2: place them
import pygame
import random
2024-03-07 18:01:12 +01:00
2024-04-20 14:47:25 +02:00
from area.constants import WIDTH,FIELD_WIDTH,TILE_SIZE,GREY,ROWS,COLS
2024-03-07 18:01:12 +01:00
from tile import Tile
from ground import Dirt
2024-03-07 18:01:12 +01:00
from genetic import genetic_algorithm
import os
2024-03-07 18:01:12 +01:00
tiles = []
2024-03-07 18:01:12 +01:00
fieldX = (WIDTH-FIELD_WIDTH)/2
# in center of the screen
fieldY = 100
# move field elements to field position
def positionFieldElements():
for t in tiles:
t.x += fieldX
t.y += fieldY
2024-03-07 18:01:12 +01:00
''' def createTiles():
for y in range(0, COLS):
for x in range(0, ROWS):
2024-03-07 18:01:12 +01:00
tile = Tile(x*TILE_SIZE, y*TILE_SIZE)
dirt = Dirt(random.randint(1, 100), random.randint(1, 100))
dirt.pests_and_weeds()
tile.ground = dirt
2024-03-07 18:01:12 +01:00
tile.randomizeContent()
tiles.append(tile)
positionFieldElements()
return tiles '''
def createTiles():
best = genetic_algorithm(50, 20, 0.7, 10)
for y in range(COLS):
for x in range (ROWS):
tile = Tile(x * TILE_SIZE, y * TILE_SIZE)
dirt = Dirt(random.randint(1, 100), random.randint(1, 100))
dirt.pests_and_weeds()
crop = best[y][x]
if crop == 'apple':
tile.image = "resources/images/sampling.png"
photo_path = random.choice(os.listdir("resources/images/plant_photos/apples"))
tile.photo = os.path.join("resources/images/plant_photos/apples", photo_path)
elif crop == 'cauliflower':
tile.image = "resources/images/sampling.png"
photo_path = random.choice(os.listdir("resources/images/plant_photos/cauliflowers"))
tile.photo = os.path.join("resources/images/plant_photos/cauliflowers", photo_path)
elif crop == 'radish':
tile.image = "resources/images/sampling.png"
photo_path = random.choice(os.listdir("resources/images/plant_photos/radishes"))
tile.photo = os.path.join("resources/images/plant_photos/radishes", photo_path)
elif crop == 'wheat':
tile.image = "resources/images/sampling.png"
photo_path = random.choice(os.listdir("resources/images/plant_photos/wheats"))
tile.photo = os.path.join("resources/images/plant_photos/wheats", photo_path)
elif crop == 'rock_dirt':
tile.image = "resources/images/rock_dirt.png"
dirt.set_ocstacle(True)
else:
tile.image = "resources/images/dirt.png"
tile.ground = "resources/images/background.jpg"
tile.ground = dirt
tiles.append(tile)
positionFieldElements()
2024-04-10 01:52:13 +02:00
return tiles
2024-03-07 18:01:12 +01:00
2024-03-07 18:01:12 +01:00
def createField(win):
createTiles()
for t in tiles:
image = pygame.image.load(t.image).convert()
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
win.blit(image, (t.x, t.y))
pygame.display.flip()
2024-03-07 18:01:12 +01:00
def drawWindow(win):
win.fill(GREY)
createField(win)
pygame.display.flip()
2024-04-10 01:52:13 +02:00
2024-04-14 14:29:11 +02:00
def get_tile_coordinates(index):
if index < len(tiles):
tile = tiles[index]
return tile.x, tile.y
else:
return None
def get_tile_index():
valid_indices = []
for index, tile in enumerate(tiles):
if tile.image=="resources/images/sampling.png":
valid_indices.append(index)
return random.choice(valid_indices)