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 pygame
import displayControler as dCon
CUBE_SIZE=128
WIDTH = 1024
HEIGHT = 512
BLACK=(0,0,0) BLACK=(0,0,0)
BORDER_COLOR=BLACK BORDER_COLOR=BLACK
BORDER_THICKNESS=1 #Has to be INT value BORDER_THICKNESS=1 #Has to be INT value
@ -12,7 +10,7 @@ class Slot:
self.y_axis=y_axis self.y_axis=y_axis
self.color=color self.color=color
self.screen=screen 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): def draw(self):
pygame.draw.rect(self.screen,self.color,self.field,0) #Draw field 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,BLACK,self.field,BORDER_THICKNESS) #Draw border

View File

@ -1,39 +1,37 @@
import pygame import pygame
import Slot
import random import random
CUBE_SIZE=128 import displayControler as dCon
WIDTH = 1024 import Slot
HEIGHT = 512
class Tractor: class Tractor:
def __init__(self,x_axis,y_axis,screen): def __init__(self,x_axis,y_axis,screen):
self.x_axis=x_axis self.x_axis=x_axis
self.y_axis=y_axis self.y_axis=y_axis
self.tractor_image = pygame.image.load('images/traktor.png') 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.screen=screen
self.slot=None self.slot=None
def draw_tractor(self): 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() pygame.display.update()
def move_tractor(self,x): def move_tractor(self,x):
if(x==0): if(x==0):
if(self.x_axis+1<=7): if(dCon.isValidMove(self.x_axis + 1, self.y_axis)):
print("Ruch w prawo") print("Ruch w prawo")
self.x_axis=self.x_axis+1 self.x_axis=self.x_axis+1
if(x==1): elif(x==1):
if(self.x_axis-1>=0): if(dCon.isValidMove(self.x_axis - 1, self.y_axis)):
print("Ruch w lewo") print("Ruch w lewo")
self.x_axis=self.x_axis-1 self.x_axis=self.x_axis-1
if(x==2): elif(x==2):
if(self.y_axis+1<=3): if(dCon.isValidMove(self.x_axis, self.y_axis + 1)):
print("Ruch w gore")
self.y_axis=self.y_axis+1
if(x==3):
if(self.y_axis-1>=0):
print("Ruch w dol") 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.y_axis=self.y_axis-1
self.draw_tractor() 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 Tractor
import random import random
import time import time
import displayControler as dCon
pygame.init() pygame.init()
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
BROWN = (139, 69, 19) BROWN = (139, 69, 19)
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
RED=(255,0,0) RED=(255,0,0)
GREEN=(0,255,0) GREEN=(0,255,0)
CUBE_SIZE = 128 screen = pygame.display.set_mode((dCon.getScreenWidth(), dCon.getScreenHeihgt()))
NUM_X = 8
NUM_Y = 4
WIDTH = 1024
HEIGHT = 512
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(WHITE) screen.fill(WHITE)
pygame.display.update() pygame.display.update()
@ -47,8 +42,8 @@ def draw_grid():
#Draw grid and tractor (new one) #Draw grid and tractor (new one)
def draw_grid(): 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 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,HEIGHT//CUBE_SIZE): #We got 4 cubes in Y axis so we use for from 0 to 3 to 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 new_slot=Slot.Slot(x,y,BROWN,screen) #Creation of empty slot
SLOT_DICT[(x,y)]=new_slot #Adding slots to dict SLOT_DICT[(x,y)]=new_slot #Adding slots to dict
for entity in SLOT_DICT: for entity in SLOT_DICT:
@ -71,7 +66,7 @@ def randomize_colors():
font=pygame.font.Font('freesansbold.ttf',32) font=pygame.font.Font('freesansbold.ttf',32)
text=font.render('Randomizing crops',True,BLACK,WHITE) text=font.render('Randomizing crops',True,BLACK,WHITE)
textRect=text.get_rect() textRect=text.get_rect()
textRect.center=(WIDTH//2,HEIGHT//2) textRect.center=(dCon.getScreenWidth() // 2,dCon.getScreenHeihgt() // 2)
screen.blit(text,textRect) screen.blit(text,textRect)
pygame.display.update() pygame.display.update()
time.sleep(3) time.sleep(3)