From 88367b06860af3c4c67c88d6560250c0f3fc1d0a Mon Sep 17 00:00:00 2001 From: tafit0902 Date: Mon, 11 Mar 2024 21:24:16 +0100 Subject: [PATCH] mozliwosc maninpulowania wielkoscia kraty --- Slot.py | 6 ++---- Tractor.py | 28 +++++++++++++--------------- displayControler.py | 17 +++++++++++++++++ main.py | 15 +++++---------- 4 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 displayControler.py diff --git a/Slot.py b/Slot.py index 61645bc..0a902dc 100644 --- a/Slot.py +++ b/Slot.py @@ -1,8 +1,6 @@ import pygame +import displayControler as dCon -CUBE_SIZE=128 -WIDTH = 1024 -HEIGHT = 512 BLACK=(0,0,0) BORDER_COLOR=BLACK BORDER_THICKNESS=1 #Has to be INT value @@ -12,7 +10,7 @@ class Slot: self.y_axis=y_axis self.color=color self.screen=screen - self.field=pygame.Rect(self.x_axis*CUBE_SIZE,self.y_axis*CUBE_SIZE,CUBE_SIZE,CUBE_SIZE) + self.field=pygame.Rect(self.x_axis*dCon.CUBE_SIZE,self.y_axis*dCon.CUBE_SIZE,dCon.CUBE_SIZE,dCon.CUBE_SIZE) 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 diff --git a/Tractor.py b/Tractor.py index 1dc53a4..adc48c6 100644 --- a/Tractor.py +++ b/Tractor.py @@ -1,39 +1,37 @@ import pygame -import Slot import random -CUBE_SIZE=128 -WIDTH = 1024 -HEIGHT = 512 +import displayControler as dCon +import Slot class Tractor: def __init__(self,x_axis,y_axis,screen): self.x_axis=x_axis self.y_axis=y_axis self.tractor_image = pygame.image.load('images/traktor.png') - self.tractor_image = pygame.transform.scale(self.tractor_image, (CUBE_SIZE, CUBE_SIZE)) + self.tractor_image = pygame.transform.scale(self.tractor_image, (dCon.CUBE_SIZE, dCon.CUBE_SIZE)) self.screen=screen self.slot=None def draw_tractor(self): - self.screen.blit(self.tractor_image, (self.x_axis*CUBE_SIZE,self.y_axis*CUBE_SIZE)) + self.screen.blit(self.tractor_image, (self.x_axis*dCon.CUBE_SIZE,self.y_axis*dCon.CUBE_SIZE)) pygame.display.update() def move_tractor(self,x): if(x==0): - if(self.x_axis+1<=7): + if(dCon.isValidMove(self.x_axis + 1, self.y_axis)): print("Ruch w prawo") self.x_axis=self.x_axis+1 - if(x==1): - if(self.x_axis-1>=0): + elif(x==1): + if(dCon.isValidMove(self.x_axis - 1, self.y_axis)): print("Ruch w lewo") self.x_axis=self.x_axis-1 - if(x==2): - if(self.y_axis+1<=3): - print("Ruch w gore") - self.y_axis=self.y_axis+1 - if(x==3): - if(self.y_axis-1>=0): + elif(x==2): + if(dCon.isValidMove(self.x_axis, self.y_axis + 1)): print("Ruch w dol") + self.y_axis=self.y_axis+1 + elif(x==3): + if(dCon.isValidMove(self.x_axis, self.y_axis - 1)): + print("Ruch w gore") self.y_axis=self.y_axis-1 self.draw_tractor() diff --git a/displayControler.py b/displayControler.py new file mode 100644 index 0000000..ea90442 --- /dev/null +++ b/displayControler.py @@ -0,0 +1,17 @@ +CUBE_SIZE = 64 +NUM_X = 20 +NUM_Y = 12 + +#returns true if tractor can move to specified slot +def isValidMove(x, y): + if x < 0 or y < 0: + return False + if x > NUM_X - 1 or y > NUM_Y - 1: + return False + return True + +def getScreenWidth(): + return NUM_X * CUBE_SIZE + +def getScreenHeihgt(): + return NUM_Y * CUBE_SIZE \ No newline at end of file diff --git a/main.py b/main.py index de95bc8..b3deefc 100644 --- a/main.py +++ b/main.py @@ -3,23 +3,18 @@ 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) -CUBE_SIZE = 128 -NUM_X = 8 -NUM_Y = 4 -WIDTH = 1024 -HEIGHT = 512 -screen = pygame.display.set_mode((WIDTH, HEIGHT)) +screen = pygame.display.set_mode((dCon.getScreenWidth(), dCon.getScreenHeihgt())) screen.fill(WHITE) pygame.display.update() @@ -47,8 +42,8 @@ def draw_grid(): #Draw grid and tractor (new one) def draw_grid(): - for x in range(0,WIDTH//CUBE_SIZE): #We got 8 cubes in X axis so we use for from 0 to 7 do draw them all - for y in range(0,HEIGHT//CUBE_SIZE): #We got 4 cubes in Y axis so we use for from 0 to 3 to draw them all + 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: @@ -71,7 +66,7 @@ def randomize_colors(): font=pygame.font.Font('freesansbold.ttf',32) text=font.render('Randomizing crops',True,BLACK,WHITE) textRect=text.get_rect() - textRect.center=(WIDTH//2,HEIGHT//2) + textRect.center=(dCon.getScreenWidth() // 2,dCon.getScreenHeihgt() // 2) screen.blit(text,textRect) pygame.display.update() time.sleep(3)