feat: adjust window size based on TILE_SIZE

This commit is contained in:
Wojciech Kubicki 2024-03-11 01:55:45 +01:00
parent f8e5d9574f
commit d9f07cdfe6
4 changed files with 31 additions and 9 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
# adjust TILE_SIZE to fit your screen well
TILE_SIZE=64

View File

@ -1,12 +1,16 @@
import sys
import pygame
from field import Field
import os
from dotenv import find_dotenv, load_dotenv
if __name__ == "__main__":
pygame.init()
WHITE = (255, 255, 255)
SCREEN_WIDTH, SCREEN_HEIGHT = 1024, 1024
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# loading TILE_SIZE to adjust window size
load_dotenv(find_dotenv())
WINDOW_SIZE = int(os.getenv("TILE_SIZE")) * 16
screen = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
field = Field()
running = True
while running:

View File

@ -1,5 +1,6 @@
import os
import pygame
from dotenv import find_dotenv, load_dotenv
class Tile(pygame.sprite.Sprite):
def __init__(self, id, type, field):
@ -11,7 +12,10 @@ class Tile(pygame.sprite.Sprite):
self.field = field
self.set_type(type)
self.rect = self.image.get_rect()
self.rect.topleft = (x * 64, y * 64)
# you can set TILE_SIZE in the .env file to adjust the window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
self.rect.topleft = (x * TILE_SIZE, y * TILE_SIZE)
def draw(self, surface):
self.tiles.draw(surface)
@ -20,6 +24,9 @@ class Tile(pygame.sprite.Sprite):
self.type = type
if self.type == 'grass':
self.image = pygame.image.load('src/images/grass.png').convert()
self.image = pygame.transform.scale(self.image, (64, 64))
# you can set TILE_SIZE in the .env file to adjust the window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))

View File

@ -1,11 +1,16 @@
import pygame
import os
from dotenv import find_dotenv, load_dotenv
class Tractor(pygame.sprite.Sprite):
def __init__(self, field):
super().__init__
self.field = field
self.image = pygame.image.load('src/images/tractor.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (64, 64))
# you can set TILE_SIZE in the .env to adjust window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.rect = self.image.get_rect()
x, y = 0, 0
self.rect.topleft = (x, y)
@ -14,14 +19,18 @@ class Tractor(pygame.sprite.Sprite):
surface.blit(self.image, self.rect)
def move(self, direction):
# you can set TILE_SIZE in the .env to adjust window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
if direction == "up":
self.rect.y -= 64
self.rect.y -= TILE_SIZE
elif direction == "down":
self.rect.y += 64
self.rect.y += TILE_SIZE
elif direction == "left":
self.rect.x -= 64
self.rect.x -= TILE_SIZE
elif direction == "right":
self.rect.x += 64
self.rect.x += TILE_SIZE
def update(self):
keys = pygame.key.get_pressed()