added pygame sprites, some code refactors
This commit is contained in:
parent
b1d33c5e41
commit
f5aea6c3f6
23
main.py
23
main.py
@ -5,16 +5,6 @@ from src.tractor import Tractor
|
|||||||
from src.settings import Settings
|
from src.settings import Settings
|
||||||
|
|
||||||
|
|
||||||
def draw_line(screen: pygame.display, settings):
|
|
||||||
for line in range(0, 11):
|
|
||||||
pygame.draw.line(screen, (255, 255, 255), (0, line * settings.tile_size),
|
|
||||||
(settings.screen_width, line*settings.tile_size))
|
|
||||||
pygame.draw.line(screen, (255, 255, 255), (line * settings.tile_size, 0),
|
|
||||||
(line * settings.tile_size, settings.screen_height))
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: add Sprite to every class and store Tile objects as Sprite group
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
@ -25,16 +15,15 @@ def main():
|
|||||||
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
|
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
|
||||||
pygame.display.set_caption('TRAKTOHOLIK')
|
pygame.display.set_caption('TRAKTOHOLIK')
|
||||||
|
|
||||||
background_image = pygame.image.load('assets/images/sky.png')
|
# background_image = pygame.image.load('assets/images/sky.png')
|
||||||
background_image = pygame.transform.scale(background_image, (settings.screen_width, settings.screen_height))
|
# background_image = pygame.transform.scale(background_image, (settings.screen_width, settings.screen_height))
|
||||||
|
|
||||||
run = True
|
run = True
|
||||||
|
|
||||||
while run:
|
while run:
|
||||||
screen.blit(background_image, (0, 0))
|
# screen.blit(background_image, (0, 0)) to chyba sie nie przyda, wiec narazie comment
|
||||||
world.draw(screen)
|
world.draw_tiles(screen)
|
||||||
draw_line(screen, settings)
|
world.draw_lines(screen)
|
||||||
screen.blit(tractor.image, (6*settings.tile_size, 5*settings.tile_size))
|
tractor.draw_tractor(screen)
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
class Plant:
|
from pygame.sprite import Sprite
|
||||||
|
|
||||||
|
class Plant(Sprite):
|
||||||
""" Class representing plants """
|
""" Class representing plants """
|
||||||
|
|
||||||
def __init__(self, species, tile):
|
def __init__(self, species, tile):
|
||||||
|
super().__init__()
|
||||||
self.species = species
|
self.species = species
|
||||||
self.tile = tile
|
self.tile = tile
|
||||||
self.position = tile.position
|
self.position = tile.position
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
class Settings:
|
class Settings:
|
||||||
""" Class to represent all settings for the app, maybe later as .cfg """
|
""" Class to represent all static settings for the app, maybe later as .cfg """
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Screen settings
|
# Screen settings
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
class Tile:
|
from pygame.sprite import Sprite
|
||||||
|
|
||||||
|
|
||||||
|
class Tile(Sprite):
|
||||||
""" Class to represent single board tile """
|
""" Class to represent single board tile """
|
||||||
|
|
||||||
def __init__(self, row_id, col_id, image, rect):
|
def __init__(self, type, row_id, col_id, image, rect):
|
||||||
|
super().__init__()
|
||||||
|
self.type = type
|
||||||
self.row_id = row_id
|
self.row_id = row_id
|
||||||
self.col_id = col_id
|
self.col_id = col_id
|
||||||
self.position = (row_id, col_id)
|
self.position = (row_id, col_id)
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from pygame.sprite import Sprite
|
||||||
|
|
||||||
|
|
||||||
class Tractor:
|
class Tractor(Sprite):
|
||||||
""" Class to represent our agent """
|
""" Class to represent our agent """
|
||||||
|
|
||||||
def __init__(self, engine, fertilizer, settings):
|
def __init__(self, engine, fertilizer, settings):
|
||||||
|
super().__init__()
|
||||||
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor.png'),
|
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor.png'),
|
||||||
(0.9*settings.tile_size, 0.9*settings.tile_size))
|
(0.9*settings.tile_size, 0.9*settings.tile_size))
|
||||||
self.engine = engine
|
self.engine = engine
|
||||||
self.fertilizer = fertilizer
|
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 hydrate(self, tile):
|
def hydrate(self, tile):
|
||||||
if tile.hydration < tile.plant.min_hydration:
|
if tile.hydration < tile.plant.min_hydration:
|
||||||
|
50
src/world.py
50
src/world.py
@ -3,44 +3,50 @@ from src.tile import Tile
|
|||||||
|
|
||||||
|
|
||||||
class World:
|
class World:
|
||||||
""" Class to represent complete game board """
|
""" Class to represent complete game board, storing Tile classes inside Sprite Group """
|
||||||
|
|
||||||
world_data = [
|
def __init__(self, settings):
|
||||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
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],
|
||||||
[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, 1, 1, 1, 1, 1, 1],
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||||
] # it will be changed when miguel sends his code
|
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1]]
|
||||||
|
|
||||||
def __init__(self, settings):
|
|
||||||
self.tile_list = []
|
|
||||||
self.dirt_image = pygame.image.load('assets/images/dirt.jpeg')
|
self.dirt_image = pygame.image.load('assets/images/dirt.jpeg')
|
||||||
self.gravel_image = pygame.image.load('assets/images/gravel.jpeg')
|
self.gravel_image = pygame.image.load('assets/images/gravel.jpeg')
|
||||||
self.settings = settings
|
self.settings = settings
|
||||||
|
self.tiles = pygame.sprite.Group() # mamy tiles jako Sprite Group, to sie przyda potem do kolizji itp.
|
||||||
|
self.create_tiles()
|
||||||
|
|
||||||
|
def create_tiles(self):
|
||||||
row_count = 0
|
row_count = 0
|
||||||
for row in self.world_data:
|
for row in self.world_data:
|
||||||
col_count = 0
|
col_count = 0
|
||||||
|
|
||||||
for tile in row:
|
for tile in row:
|
||||||
if tile == 1:
|
if tile == 1:
|
||||||
img = pygame.transform.scale(self.dirt_image, (settings.tile_size, settings.tile_size))
|
img = pygame.transform.scale(self.dirt_image, (self.settings.tile_size, self.settings.tile_size))
|
||||||
|
type = 'dirt'
|
||||||
elif tile == 0:
|
elif tile == 0:
|
||||||
img = pygame.transform.scale(self.gravel_image, (settings.tile_size, settings.tile_size))
|
img = pygame.transform.scale(self.gravel_image, (self.settings.tile_size, self.settings.tile_size))
|
||||||
|
type = 'gravel'
|
||||||
img_rect = img.get_rect()
|
img_rect = img.get_rect()
|
||||||
img_rect.x = col_count * settings.tile_size
|
img_rect.x = col_count * self.settings.tile_size
|
||||||
img_rect.y = row_count * settings.tile_size
|
img_rect.y = row_count * self.settings.tile_size
|
||||||
tile = Tile(row_count, col_count, img, img_rect)
|
tile = Tile(type, row_count, col_count, img, img_rect)
|
||||||
self.tile_list.append(tile)
|
self.tiles.add(tile)
|
||||||
col_count += 1
|
col_count += 1
|
||||||
row_count += 1
|
row_count += 1
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw_tiles(self, screen):
|
||||||
for tile in self.tile_list:
|
self.tiles.draw(screen)
|
||||||
screen.blit(tile.image, tile.rect)
|
|
||||||
|
def draw_lines(self, screen):
|
||||||
|
for line in range(len(self.world_data)):
|
||||||
|
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user