diff --git a/App.py b/App.py new file mode 100644 index 0000000..63222d9 --- /dev/null +++ b/App.py @@ -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() + + + diff --git a/Colors.py b/Colors.py new file mode 100644 index 0000000..86f9cc1 --- /dev/null +++ b/Colors.py @@ -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] \ No newline at end of file diff --git a/Image.py b/Image.py new file mode 100644 index 0000000..c08f264 --- /dev/null +++ b/Image.py @@ -0,0 +1,29 @@ +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] + + def return_plant(self,plant_name): + return self.plants_image_dict[plant_name] \ No newline at end of file diff --git a/Pole.py b/Pole.py new file mode 100644 index 0000000..101f2ae --- /dev/null +++ b/Pole.py @@ -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) diff --git a/Slot.py b/Slot.py index 0a902dc..78d9f56 100644 --- a/Slot.py +++ b/Slot.py @@ -1,20 +1,39 @@ import pygame 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 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.y_axis=y_axis - self.color=color + self.plant=color #TODO CHANGE IT BY HOOKING PLANT CLASS 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.image_loader=image_loader + def draw(self): - pygame.draw.rect(self.screen,self.color,self.field,0) #Draw field - pygame.draw.rect(self.screen,BLACK,self.field,BORDER_THICKNESS) #Draw border + pygame.draw.rect(self.screen,Colors.BROWN,self.field,0) #Draw field + pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS) #Draw border pygame.display.update() + + def redraw_image(self): + self.set_image() + def color_change(self,color): - self.color=color + self.plant=color 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() diff --git a/Ui.py b/Ui.py new file mode 100644 index 0000000..61ffc5c --- /dev/null +++ b/Ui.py @@ -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) \ No newline at end of file diff --git a/images/plants/borowka.jpg b/images/plants/borowka.jpg new file mode 100644 index 0000000..80d8921 Binary files /dev/null and b/images/plants/borowka.jpg differ diff --git a/images/plants/kukurydza.jpg b/images/plants/kukurydza.jpg new file mode 100644 index 0000000..d5f9aea Binary files /dev/null and b/images/plants/kukurydza.jpg differ diff --git a/images/plants/pszenica.jpg b/images/plants/pszenica.jpg new file mode 100644 index 0000000..29a0867 Binary files /dev/null and b/images/plants/pszenica.jpg differ diff --git a/images/plants/slonecznik.jpg b/images/plants/slonecznik.jpg new file mode 100644 index 0000000..6d6cf5e Binary files /dev/null and b/images/plants/slonecznik.jpg differ diff --git a/images/plants/winogrono.jpg b/images/plants/winogrono.jpg new file mode 100644 index 0000000..eda0535 Binary files /dev/null and b/images/plants/winogrono.jpg differ diff --git a/images/plants/ziemniak.jpg b/images/plants/ziemniak.jpg new file mode 100644 index 0000000..4213b73 Binary files /dev/null and b/images/plants/ziemniak.jpg differ diff --git a/main.py b/main.py index b3deefc..b78b3f2 100644 --- a/main.py +++ b/main.py @@ -1,93 +1,3 @@ -import pygame -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() +import App +App.init(demo=True)#DEMO=TRUE WILL INIT DEMO MODE WITH RANDOM COLOR GEN \ No newline at end of file