Cleaned up main.py (by adding files app,colors,ui), added images for plants, added support for plant image in slot class, changed image loading by scaling it once at the start

This commit is contained in:
jakzar 2024-03-23 13:46:48 +01:00
parent 7a03e2f254
commit b345f827b3
13 changed files with 179 additions and 99 deletions

49
App.py Normal file
View File

@ -0,0 +1,49 @@
import pygame
import Colors
import Tractor
import Pole
import time
import displayControler as dCon
import Image
pygame.init()
screen = pygame.display.set_mode((dCon.getScreenWidth(), dCon.getScreenHeihgt()))
#Tractor creation
traktor=Tractor.Tractor(0,0,screen)
image_loader=Image.Image()
image_loader.load_images()
pole=Pole.Pole(screen,image_loader)
def init_demo(): #Demo purpose
pole.draw_grid()
traktor.draw_tractor()
time.sleep(2)
pole.randomize_colors()
traktor.draw_tractor()
while True:
time.sleep(0.5)
demo_move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
def init(demo):
screen.fill(Colors.WHITE)
pygame.display.update()
if(demo==True):
init_demo()
#TODO: Implement
def demo_move():
pole.get_slot_from_cord((traktor.x_axis,traktor.y_axis)).redraw_image()
traktor.random_move()

14
Colors.py Normal file
View File

@ -0,0 +1,14 @@
import random
BLACK = (0, 0, 0)
BROWN = (139, 69, 19)
WHITE = (255, 255, 255)
RED=(255,0,0)
GREEN=(0,255,0)
def random_color():
x=random.randint(0,3)
switcher={0:BROWN,
1:GREEN,
2:RED,
3:WHITE}
return switcher[x]

26
Image.py Normal file
View File

@ -0,0 +1,26 @@
import pygame
import displayControler as dCon
import random
class Image:
def __init__(self):
self.plants_image_dict={}
self.tractor_image=None
def load_images(self):
files_plants={0:"borowka",
1:"kukurydza",
2:"pszenica",
3:"slonecznik",
4:"winogrono",
5:"ziemniak"}
for index in files_plants:
plant_image=pygame.image.load("images/plants/"+files_plants[index]+".jpg")
plant_image=pygame.transform.scale(plant_image,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
self.plants_image_dict[files_plants[index]]=plant_image
tractor_image=pygame.image.load("images/traktor.png")
tractor_image=pygame.transform.scale(tractor_image,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
def return_random_plant(self):
x=random.randint(0,5)
keys=list(self.plants_image_dict.keys())
plant=keys[x]
return self.plants_image_dict[plant]

45
Pole.py Normal file
View File

@ -0,0 +1,45 @@
import displayControler as dCon
import Slot
import Colors
import pygame
import time
import Ui
class Pole:
def __init__(self,screen,image_loader):
self.screen=screen
self.slot_dict={} #Slot are stored in dictionary with key being a Tuple of x and y coordinates so top left slot key is (0,0) and value is slot object
self.ui=Ui.Ui(screen)
self.image_loader=image_loader
def get_slot_from_cord(self,coordinates):
(x_axis,y_axis)=coordinates
return self.slot_dict[(x_axis,y_axis)]
def set_slot(self,coodrinates,slot): #set slot in these coordinates
(x_axis,y_axis)=coodrinates
self.slot_dict[(x_axis,y_axis)]=slot
def get_slot_dict(self): #returns whole slot_dict
return self.slot_dict
#Draw grid and tractor (new one)
def draw_grid(self):
for x in range(0,dCon.NUM_X): #Draw all cubes in X axis
for y in range(0,dCon.NUM_Y): #Draw all cubes in Y axis
new_slot=Slot.Slot(x,y,Colors.BROWN,self.screen,self.image_loader) #Creation of empty slot
self.set_slot((x,y),new_slot) #Adding slots to dict
slot_dict=self.get_slot_dict()
for coordinates in slot_dict:
slot_dict[coordinates].draw()
def randomize_colors(self):
pygame.display.update()
time.sleep(3)
self.ui.render_text("Randomizing Crops")
for coordinates in self.slot_dict:
self.slot_dict[coordinates].set_random_plant()
def change_color_of_slot(self,coordinates,color): #Coordinates must be tuple (x,y) (left top slot has cord (0,0) ), color has to be from defined in Colors.py or custom in RGB value (R,G,B)
self.get_slot_from_cord(coordinates).color_change(color)

34
Slot.py
View File

@ -1,20 +1,40 @@
import pygame import pygame
import displayControler as dCon import displayControler as dCon
import Colors
import random
import Image
BLACK=(0,0,0)
BORDER_COLOR=BLACK
BORDER_THICKNESS=1 #Has to be INT value BORDER_THICKNESS=1 #Has to be INT value
class Slot: class Slot:
def __init__(self,x_axis,y_axis,color,screen): #TODO crop and related to it things handling. for now as a place holder crop=>color def __init__(self,x_axis,y_axis,color,screen,image_loader):
self.x_axis=x_axis self.x_axis=x_axis
self.y_axis=y_axis self.y_axis=y_axis
self.color=color self.plant=color #TODO CHANGE IT BY HOOKING PLANT CLASS
self.condition=""#TODO HOOK CONDITION CLASS NOW COMPLETLY UNUSED
self.screen=screen self.screen=screen
self.field=pygame.Rect(self.x_axis*dCon.CUBE_SIZE,self.y_axis*dCon.CUBE_SIZE,dCon.CUBE_SIZE,dCon.CUBE_SIZE) self.field=pygame.Rect(self.x_axis*dCon.CUBE_SIZE,self.y_axis*dCon.CUBE_SIZE,dCon.CUBE_SIZE,dCon.CUBE_SIZE)
self.image_loader=image_loader
def draw(self): def draw(self):
pygame.draw.rect(self.screen,self.color,self.field,0) #Draw field pygame.draw.rect(self.screen,Colors.BROWN,self.field,0) #Draw field
pygame.draw.rect(self.screen,BLACK,self.field,BORDER_THICKNESS) #Draw border pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS) #Draw border
pygame.display.update() pygame.display.update()
def redraw_image(self):
self.set_image()
def color_change(self,color): def color_change(self,color):
self.color=color self.plant=color
self.draw() self.draw()
def set_random_plant(self):
self.plant=self.random_plant()
self.set_image()
def set_image(self):
self.screen.blit(self.plant,(self.x_axis*dCon.CUBE_SIZE,self.y_axis*dCon.CUBE_SIZE))
pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS)
def random_plant(self): #Probably will not be used later only for demo purpouse
return self.image_loader.return_random_plant()

16
Ui.py Normal file
View File

@ -0,0 +1,16 @@
import pygame
import displayControler as dCon
import Colors
class Ui:
def __init__(self,screen):
self.screen=screen
self.font='freesansbold.ttf' #Feel free to change it :D
self.font_size=int(32)
def render_text(self,string_to_print):
font=pygame.font.Font(self.font,self.font_size)
text=font.render(string_to_print,True,Colors.BLACK,Colors.WHITE)
textRect=text.get_rect()
textRect.center=(dCon.getScreenWidth() // 2,dCon.getScreenHeihgt() // 2)
self.screen.blit(text,textRect)

BIN
images/plants/borowka.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

BIN
images/plants/kukurydza.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 MiB

BIN
images/plants/pszenica.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 672 KiB

BIN
images/plants/winogrono.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

BIN
images/plants/ziemniak.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 MiB

94
main.py
View File

@ -1,93 +1,3 @@
import pygame import App
import Slot
import Tractor
import random
import time
import displayControler as dCon
pygame.init()
BLACK = (0, 0, 0)
BROWN = (139, 69, 19)
WHITE = (255, 255, 255)
RED=(255,0,0)
GREEN=(0,255,0)
screen = pygame.display.set_mode((dCon.getScreenWidth(), dCon.getScreenHeihgt()))
screen.fill(WHITE)
pygame.display.update()
#Tractor creation
traktor=Tractor.Tractor(0,0,screen)
SLOT_DICT={} #Slot are stored in dictionary with key being a Tuple of x and y coordinates so top left slot key is (0,0) and value is slot object
"""#Draw grid and tractor (old one)
def draw_grid():
for x in range(0, 8):
pygame.draw.line(screen, BLACK, (x*CUBE_SIZE, 0), (x*CUBE_SIZE, HEIGHT)) #We got 8 lines in Y axis so to draw them we use x from range <0,7) to make slot manage easier
for y in range(0, 4):
pygame.draw.line(screen, BLACK, (0, y*CUBE_SIZE), (WIDTH, y*CUBE_SIZE)) #We got 4 lines in X axis so to draw them we use y from range <0,4) to make slot manage easier
# Draw tractor
tractor_image = pygame.image.load('images/traktor.png')
tractor_image = pygame.transform.scale(tractor_image, (CUBE_SIZE, CUBE_SIZE))
screen.blit(tractor_image, (CUBE_SIZE - 128, CUBE_SIZE - 128))"""
#Draw grid and tractor (new one)
def draw_grid():
for x in range(0,dCon.NUM_X): #We got 8 cubes in X axis so we use for from 0 to 7 do draw them all
for y in range(0,dCon.NUM_Y): #We got 4 cubes in Y axis so we use for from 0 to 3 to draw them all
new_slot=Slot.Slot(x,y,BROWN,screen) #Creation of empty slot
SLOT_DICT[(x,y)]=new_slot #Adding slots to dict
for entity in SLOT_DICT:
SLOT_DICT[entity].draw()
traktor.draw_tractor()
def change_color_of_slot(coordinates,color): #Coordinates must be tuple (x,y) x from range 0,7 and y in range 0,3 (left top slot has cord (0,0) ), color has to be from defined or custom in RGB value (R,G,B)
SLOT_DICT[coordinates].color_change(color)
def random_color():
x=random.randint(0,3)
switcher={0:BROWN,
1:GREEN,
2:RED,
3:WHITE}
return switcher[x]
#Demo purpose, can be reused for photos of crops in the future(?)
def randomize_colors():
font=pygame.font.Font('freesansbold.ttf',32)
text=font.render('Randomizing crops',True,BLACK,WHITE)
textRect=text.get_rect()
textRect.center=(dCon.getScreenWidth() // 2,dCon.getScreenHeihgt() // 2)
screen.blit(text,textRect)
pygame.display.update()
time.sleep(3)
for coordinates in SLOT_DICT:
SLOT_DICT[coordinates].color_change(random_color())
traktor.draw_tractor()
def init_demo(): #Demo purpose
draw_grid()
time.sleep(2)
randomize_colors()
def demo_move():
SLOT_DICT[(traktor.x_axis,traktor.y_axis)].draw()
traktor.random_move()
init_demo()
while True:
time.sleep(1)
demo_move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
App.init(demo=True)#DEMO=TRUE WILL INIT DEMO MODE WITH RANDOM COLOR GEN