SztIn_gr.234798/field.py

124 lines
4.2 KiB
Python
Raw Normal View History

import random
2023-03-12 16:50:42 +01:00
import pygame
pygame.init()
2023-03-12 17:46:49 +01:00
BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
2023-03-12 16:50:42 +01:00
BLUE = (46, 34, 240)
2023-03-12 17:46:49 +01:00
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600
BLOCK_SIZE = 40
ROCKS_NUMBER = 10
VEGETABLES_NUMBER = 20
VEGETABLES = ('Potato', 'Broccoli', 'Carrot', 'Onion')
2023-03-12 17:46:49 +01:00
2023-03-28 20:05:01 +02:00
def generate_locations(number, flag=False, rocks=[]):
locations = []
if flag:
for i in range(number):
x = random.randrange(0, WINDOW_WIDTH, BLOCK_SIZE)
y = random.randrange(0, WINDOW_HEIGHT, BLOCK_SIZE)
if (x, y) not in rocks and (x, y) not in locations:
locations.append((x, y, VEGETABLES[random.randrange(0, len(VEGETABLES))]))
else:
i -= 1
return locations
else:
for i in range(number):
x = random.randrange(0, WINDOW_WIDTH, BLOCK_SIZE)
y = random.randrange(0, WINDOW_HEIGHT, BLOCK_SIZE)
if (x, y) not in locations:
locations.append((x, y))
else:
i -= 1
return locations
2023-03-12 17:46:49 +01:00
def draw_grid():
2023-03-28 20:05:01 +02:00
#Set the size of the grid block
wei = pygame.image.load("images/wet_earth_tile.jpg")
dei = pygame.image.load("images/dry_earth_tile.jpg")
2023-03-12 17:46:49 +01:00
for x in range(0, WINDOW_WIDTH, BLOCK_SIZE):
for y in range(0, WINDOW_HEIGHT, BLOCK_SIZE):
2023-03-28 20:05:01 +02:00
if (x, y) in wet_tiles_coordinates:
sc.blit(wei, (x, y))
else:
sc.blit(dei, (x, y))
2023-03-12 17:46:49 +01:00
rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(sc, WHITE, rect, 1)
2023-03-28 20:05:01 +02:00
2023-03-12 17:46:49 +01:00
def draw_interface():
global sc
sc = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Pole i ciągnik")
pygame.display.set_icon(pygame.image.load("images/icon.png"))
2023-03-12 16:50:42 +01:00
2023-03-12 17:46:49 +01:00
clock = pygame.time.Clock()
sc.fill(BLACK)
FPS = 60
2023-03-12 16:50:42 +01:00
#region Images import
bg = pygame.image.load("images/field_image.jpg")
tractor_image = pygame.image.load("images/tractor_image.png")
rock_image = pygame.image.load("images/rock_image.png")
potato_image = pygame.image.load("images/potato.png")
carrot_image = pygame.image.load("images/carrot.png")
broccoli_image = pygame.image.load("images/broccoli.png")
onion_image = pygame.image.load("images/onion.png")
#endregion
2023-03-17 15:56:15 +01:00
2023-03-12 17:46:49 +01:00
x = BLOCK_SIZE / 4
y = BLOCK_SIZE / 4
2023-03-12 16:50:42 +01:00
rocks = generate_locations(ROCKS_NUMBER)
vegetables = generate_locations(VEGETABLES_NUMBER, flag=True, rocks=rocks)
2023-03-28 20:05:01 +02:00
global wet_tiles_coordinates
wet_tiles_coordinates = []
2023-03-12 17:46:49 +01:00
flRunning = True
while flRunning:
2023-03-17 15:56:15 +01:00
#sc.fill(BLACK)
sc.blit(bg, (0, 0))
draw_grid()
2023-03-12 17:46:49 +01:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
flRunning = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if x > BLOCK_SIZE/2:
x -= BLOCK_SIZE
2023-03-12 17:46:49 +01:00
elif event.key == pygame.K_RIGHT:
if x < WINDOW_WIDTH - BLOCK_SIZE/2:
x += BLOCK_SIZE
2023-03-12 17:46:49 +01:00
elif event.key == pygame.K_DOWN:
if y < WINDOW_HEIGHT - BLOCK_SIZE/2:
y += BLOCK_SIZE
2023-03-12 17:46:49 +01:00
elif event.key == pygame.K_UP:
if y > BLOCK_SIZE/2:
y -= BLOCK_SIZE
2023-03-28 20:05:01 +02:00
elif event.key == pygame.K_SPACE:
wet_tiles_coordinates.append((x-BLOCK_SIZE/4, y-BLOCK_SIZE/4))
2023-03-17 15:56:15 +01:00
#pygame.draw.rect(sc, BLUE, (x, y, BLOCK_SIZE / 2, BLOCK_SIZE / 2))
2023-03-28 20:05:01 +02:00
for rock in rocks:
sc.blit(rock_image, (rock[0], rock[1]))
for vegetable in vegetables:
if vegetable[2] == 'Potato':
sc.blit(potato_image, (vegetable[0], vegetable[1]))
elif vegetable[2] == 'Carrot':
sc.blit(carrot_image, (vegetable[0], vegetable[1]))
elif vegetable[2] == 'Broccoli':
sc.blit(broccoli_image, (vegetable[0], vegetable[1]))
elif vegetable[2] == 'Onion':
sc.blit(onion_image, (vegetable[0], vegetable[1]))
2023-03-28 20:05:01 +02:00
sc.blit(tractor_image, (x - 5, y - 5))
2023-03-12 17:46:49 +01:00
pygame.display.update()
2023-03-12 16:50:42 +01:00
2023-03-12 17:46:49 +01:00
clock.tick(FPS)