added obstacles, move, rotate
@ -5,7 +5,7 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (ai-project)" project-jdk-type="Python SDK" />
|
||||
</project>
|
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 1.1 MiB |
Before Width: | Height: | Size: 123 KiB |
Before Width: | Height: | Size: 84 KiB |
BIN
assets/images/tractor/tractor-transparent-down.png
Normal file
After Width: | Height: | Size: 165 KiB |
BIN
assets/images/tractor/tractor-transparent-left.png
Normal file
After Width: | Height: | Size: 154 KiB |
BIN
assets/images/tractor/tractor-transparent-right.png
Normal file
After Width: | Height: | Size: 154 KiB |
Before Width: | Height: | Size: 147 KiB After Width: | Height: | Size: 147 KiB |
14
main.py
@ -10,27 +10,27 @@ def main():
|
||||
|
||||
settings = Settings()
|
||||
world = World(settings)
|
||||
tractor = Tractor("Spalinowy", "Nawóz 1", settings)
|
||||
tractor = Tractor("Spalinowy", "Nawóz 1", settings, 8*settings.tile_size, 8*settings.tile_size)
|
||||
obstacles = [tile for tile in world.tiles if tile.type == 'rock']
|
||||
clock = pygame.time.Clock() # FPS purpose
|
||||
|
||||
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
|
||||
pygame.display.set_caption('TRAKTOHOLIK')
|
||||
|
||||
# background_image = pygame.image.load('assets/images/sky.png')
|
||||
# background_image = pygame.transform.scale(background_image, (settings.screen_width, settings.screen_height))
|
||||
|
||||
run = True
|
||||
while run:
|
||||
# screen.blit(background_image, (0, 0)) to chyba sie nie przyda, wiec narazie comment
|
||||
clock.tick(settings.fps)
|
||||
world.draw_tiles(screen)
|
||||
world.draw_lines(screen)
|
||||
tractor.draw_tractor(screen)
|
||||
tractor.draw(screen)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
tractor.update()
|
||||
tractor.check_collision(obstacles)
|
||||
pygame.display.update()
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
|
@ -2,9 +2,16 @@ class Settings:
|
||||
""" Class to represent all static settings for the app, maybe later as .cfg """
|
||||
|
||||
def __init__(self):
|
||||
# Game settings
|
||||
self.fps = 60
|
||||
self.freeze_time = 500
|
||||
|
||||
# Screen settings
|
||||
self.screen_width = 800
|
||||
self.screen_height = 800
|
||||
self.screen_width = 700
|
||||
self.screen_height = 700
|
||||
|
||||
# Tile settings
|
||||
self.tile_size = 80
|
||||
self.tile_size = 70
|
||||
|
||||
# Tractor settings
|
||||
self.tractor_speed = self.tile_size
|
||||
|
100
src/tractor.py
@ -1,20 +1,106 @@
|
||||
import pygame
|
||||
import math
|
||||
from pygame.sprite import Sprite
|
||||
|
||||
|
||||
class Tractor(Sprite):
|
||||
""" Class to represent our agent """
|
||||
|
||||
def __init__(self, engine, fertilizer, settings):
|
||||
def __init__(self, engine, fertilizer, settings, x, y):
|
||||
super().__init__()
|
||||
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor-transparent.png'),
|
||||
(0.9*settings.tile_size, 0.9*settings.tile_size))
|
||||
self.settings = settings
|
||||
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor/tractor-transparent-up.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.x = x
|
||||
self.rect.y = y
|
||||
self.curr_direction = [0, 1] # wektor w ukladzie wspolrzednych wskazujacy kierunek traktora
|
||||
self.engine = engine
|
||||
self.fertilizer = fertilizer
|
||||
self.settings = settings
|
||||
|
||||
def draw_tractor(self, screen):
|
||||
screen.blit(self.image, (6 * self.settings.tile_size, 5 * self.settings.tile_size))
|
||||
def turn_right(self):
|
||||
if self.curr_direction == [0, 1]:
|
||||
self.curr_direction = [1, 0]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-right.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [1, 0]:
|
||||
self.curr_direction = [0, -1]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-down.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [0, -1]:
|
||||
self.curr_direction = [-1, 0]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-left.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [-1, 0]:
|
||||
self.curr_direction = [0, 1]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-up.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
|
||||
pygame.time.wait(500) # bez tego sie kreci jak hot-wheels
|
||||
|
||||
def turn_left(self):
|
||||
if self.curr_direction == [0, 1]:
|
||||
self.curr_direction = [-1, 0]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-left.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [-1, 0]:
|
||||
self.curr_direction = [0, -1]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-down.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [0, -1]:
|
||||
self.curr_direction = [1, 0]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-right.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
elif self.curr_direction == [1, 0]:
|
||||
self.curr_direction = [0, 1]
|
||||
self.image = pygame.transform.scale(
|
||||
pygame.image.load('assets/images/tractor/tractor-transparent-up.png'),
|
||||
(self.settings.tile_size, self.settings.tile_size))
|
||||
|
||||
pygame.time.wait(self.settings.freeze_time) # bez tego sie kreci jak hot-wheels
|
||||
|
||||
def move(self):
|
||||
self.rect.x += self.curr_direction[0] * self.settings.tractor_speed
|
||||
self.rect.y -= self.curr_direction[1] * self.settings.tractor_speed
|
||||
|
||||
if self.rect.x <= 0:
|
||||
self.rect.x = 1 # musi być 1, bo przy idk czemu 0 się buguje i nie może się obrócić
|
||||
if self.rect.y <= 0:
|
||||
self.rect.y = 1 # j.w.
|
||||
if self.rect.x >= self.settings.screen_width - self.settings.tile_size:
|
||||
self.rect.x = self.settings.screen_width - self.settings.tile_size
|
||||
if self.rect.y >= self.settings.screen_height - self.settings.tile_size:
|
||||
self.rect.y = self.settings.screen_height - self.settings.tile_size
|
||||
|
||||
pygame.time.wait(500) # bez tego sie kreci jak hot-wheels
|
||||
|
||||
def check_collision(self, obstacles):
|
||||
if pygame.sprite.spritecollideany(self, obstacles):
|
||||
print('yes')
|
||||
self.rect.x -= self.curr_direction[0] * self.settings.tile_size # no to troche prymitywne jest, ale
|
||||
self.rect.y += self.curr_direction[1] * self.settings.tile_size # jak wejdzie na kolizje to cofamy ruch
|
||||
# w przyszlosci mozna zmienic
|
||||
|
||||
def update(self):
|
||||
key = pygame.key.get_pressed()
|
||||
|
||||
if key[pygame.K_d] and self.rect.x:
|
||||
self.turn_right()
|
||||
elif key[pygame.K_a]:
|
||||
self.turn_left()
|
||||
elif key[pygame.K_w]:
|
||||
self.move()
|
||||
# print(self.rect)
|
||||
|
||||
def draw(self, screen):
|
||||
screen.blit(self.image, self.rect)
|
||||
|
||||
def hydrate(self, tile):
|
||||
if tile.hydration < tile.plant.min_hydration:
|
||||
@ -34,4 +120,4 @@ class Tractor(Sprite):
|
||||
def fertilize(self, tile):
|
||||
if not tile.is_fertilized:
|
||||
tile.is_fertilized = True
|
||||
tile.fertilizer = tile.plant.fertilizer
|
||||
tile.fertilizer = tile.plant.fertilizer
|
28
src/world.py
@ -6,18 +6,18 @@ class World:
|
||||
""" Class to represent complete game board, storing Tile classes inside Sprite Group """
|
||||
|
||||
def __init__(self, settings):
|
||||
self.world_data = [[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1]]
|
||||
self.world_data = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
|
||||
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
|
||||
self.dirt_image = pygame.image.load('assets/images/dirt.jpeg')
|
||||
self.gravel_image = pygame.image.load('assets/images/cobblestone.jpg')
|
||||
self.rock_image = pygame.image.load('assets/images/cobblestone.jpg')
|
||||
self.settings = settings
|
||||
self.tiles = pygame.sprite.Group() # mamy tiles jako Sprite Group, to sie przyda potem do kolizji itp.
|
||||
self.create_tiles()
|
||||
@ -31,8 +31,8 @@ class World:
|
||||
img = pygame.transform.scale(self.dirt_image, (self.settings.tile_size, self.settings.tile_size))
|
||||
type = 'dirt'
|
||||
elif tile == 0:
|
||||
img = pygame.transform.scale(self.gravel_image, (self.settings.tile_size, self.settings.tile_size))
|
||||
type = 'gravel'
|
||||
img = pygame.transform.scale(self.rock_image, (self.settings.tile_size, self.settings.tile_size))
|
||||
type = 'rock'
|
||||
img_rect = img.get_rect()
|
||||
img_rect.x = col_count * self.settings.tile_size
|
||||
img_rect.y = row_count * self.settings.tile_size
|
||||
@ -49,4 +49,4 @@ class World:
|
||||
pygame.draw.line(screen, (255, 255, 255), (0, line * self.settings.tile_size),
|
||||
(self.settings.screen_width, line * self.settings.tile_size))
|
||||
pygame.draw.line(screen, (255, 255, 255), (line * self.settings.tile_size, 0),
|
||||
(line * self.settings.tile_size, self.settings.screen_height))
|
||||
(line * self.settings.tile_size, self.settings.screen_height))
|