mozliwosc maninpulowania wielkoscia kraty

This commit is contained in:
tafit0902 2024-03-11 21:24:16 +01:00
parent 4e98ae6e87
commit 88367b0686
4 changed files with 37 additions and 29 deletions

View File

@ -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

View File

@ -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()

17
displayControler.py Normal file
View File

@ -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

15
main.py
View File

@ -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)