add blocks method, new graphic, rebuild the field type to block type

This commit is contained in:
Aliaksei Brown 2023-03-22 17:13:24 +01:00
parent 99d031f641
commit 9813875062
32 changed files with 91 additions and 23 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

43
blocks.py Normal file
View File

@ -0,0 +1,43 @@
import pygame
import random
from pygame.math import Vector2
class Blocks:
def __init__(self, parent_screen,cell_size):
self.parent_screen = parent_screen
self.vege_image = pygame.image.load(r'resources/flower.png').convert_alpha()
self.vege_image = pygame.transform.scale(self.vege_image, (cell_size, cell_size))
self.lief_image = pygame.image.load(r'resources/stone.png').convert_alpha()
self.lief_image = pygame.transform.scale(self.lief_image, (cell_size, cell_size))
self.carrot_image = pygame.image.load(r'resources/dead.png').convert_alpha()
self.carrot_image = pygame.transform.scale(self.carrot_image, (cell_size, cell_size))
def locate_blocks(self, blocks_number, cell_number, body):
for i in range(blocks_number):
self.x = random.randint(0, cell_number-1)
self.y = random.randint(0, cell_number-1)
self.pos = Vector2(self.x, self.y)
body.append(self.pos)
#block_dict.update({self.x : 1}) # for now it may lay on each other,
print(body)
def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks
for block in body:
x = int(block.x * cell_size)
y = int(block.y * cell_size)
if color == 'carrot':
self.parent_screen.blit(self.carrot_image, (x, y))
if color == 'leaf':
self.parent_screen.blit(self.lief_image, (x, y))
if color == 'vege':
self.parent_screen.blit(self.vege_image, (x, y))
def draw_lines(self, parent_screen, cell_size): # background lines
for i in range(1, 10):
pygame.draw.line(self.parent_screen, (228, 253, 227), (cell_size * i, 0), (cell_size * i, parent_screen), 1)
pygame.draw.line(self.parent_screen, (228, 253, 227), (0, cell_size * i), (parent_screen, cell_size * i), 1)

View File

@ -4,8 +4,9 @@ class Field:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
self.block = pygame.image.load(r'resources/field.png').convert()
#def draw_blocks():
def place_field(self, field_matrix, parent_screen, cell_size):
def place_field(self, field_matrix, parent_screen, cell_size): #drawing blocks
for m, posY in enumerate(field_matrix):
for n, posX in enumerate(posY):
if field_matrix[m][n] == 1:

56
main.py
View File

@ -2,31 +2,42 @@ import pygame
import random
import tractor
import field
import blocks
from pygame.locals import *
from datetime import datetime
class Game:
cell_size = 50
screen_size = 500
field_matrix = [[0 for m in range(10)] for n in range(10)]
for i in range(10):
while True:
field_posX = random.randint(0, 9)
field_posY = random.randint(0, 9)
if field_matrix[field_posY][field_posX] == 0:
field_matrix[field_posY][field_posX] = 1
break
cell_number = 15
blocks_number = 15
block_dict = {}
def __init__(self):
self.leaf_body = []
self.vege_body = []
self.carrot_body = []
pygame.init()
self.surface = pygame.display.set_mode((self.screen_size, self.screen_size)) # initialize a window
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number)) # initialize a window
self.grass_image = pygame.image.load(r'resources/grass3.png').convert()
self.grass_image = pygame.transform.scale(self.grass_image, (self.cell_size, self.cell_size))
self.blocks = blocks.Blocks(self.surface,self.cell_size)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.leaf_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.vege_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.carrot_body)
self.field = field.Field(self.surface)
self.field.place_field(self.field_matrix, self.surface, self.cell_size)
self.blocks.place_blocks(self.surface, self.cell_size, self.leaf_body, 'leaf')
self.blocks.place_blocks(self.surface, self.cell_size, self.vege_body, 'vege')
self.blocks.place_blocks(self.surface, self.cell_size, self.carrot_body, 'carrot')
for i in range(0, self.cell_number*self.cell_number):
x = int(i * self.cell_size)
y = int(i * self.cell_size)
self.surface.blit(self.grass_image, (x, y))
self.tractor = tractor.Tractor(self.surface, self.cell_size)
self.tractor.draw()
@ -41,7 +52,6 @@ class Game:
clock.tick(60) # manual fps control not to overwork the computer
time_now = datetime.now()
for event in pygame.event.get():
if event.type == KEYDOWN:
if pygame.key.get_pressed()[K_ESCAPE]:
@ -59,9 +69,21 @@ class Game:
running = False
self.surface.fill((140, 203, 97)) # background color
self.field.place_field(self.field_matrix, self.surface, self.cell_size)
self.field.draw_lines(self.screen_size, self.cell_size)
for i in range(0, self.cell_number):
for k in range(0, self.cell_number):
x = int(i * self.cell_size)
y = int(k * self.cell_size)
self.surface.blit(self.grass_image, (x, y))
#self.blocks.draw_lines(self.cell_number*self.cell_size, self.cell_size)
self.blocks.place_blocks(self.surface, self.cell_size, self.leaf_body, 'leaf')
self.blocks.place_blocks(self.surface, self.cell_size, self.vege_body, 'vege')
self.blocks.place_blocks(self.surface, self.cell_size, self.carrot_body, 'carrot')
self.tractor.draw()
pygame.display.update()

BIN
resources/2ZAYDK.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

BIN
resources/3ZcPVQ.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 B

BIN
resources/4-gFH9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

BIN
resources/DJb-j3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 B

BIN
resources/NfASJh.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 B

BIN
resources/b8H0jv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

BIN
resources/carrot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

BIN
resources/cldvYP.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 B

BIN
resources/cs5N6Z.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

BIN
resources/dXE2CG.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 B

BIN
resources/dead.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 455 B

BIN
resources/flower.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 448 B

BIN
resources/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

BIN
resources/grass1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

BIN
resources/grass2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

BIN
resources/grass3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 B

BIN
resources/grass4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 B

BIN
resources/jRdXIV.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 B

BIN
resources/leaf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

BIN
resources/red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
resources/robot2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
resources/sKlv-b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

BIN
resources/stone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

BIN
resources/tmxa28.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 385 B

BIN
resources/vege.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View File

@ -1,13 +1,15 @@
import pygame
import random
from pygame.math import Vector2
class Tractor:
def __init__(self, parent_screen, cell_size):
self.parent_screen = parent_screen
self.image = pygame.image.load(r'resources/tractor.png').convert_alpha()
self.image = pygame.image.load(r'resources/robot2.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (cell_size, cell_size+5))
self.x = cell_size*2
self.y = cell_size*2
self.pos = Vector2(self.x, self.y)
self.angle = 0
self.direction = 'up'
@ -17,16 +19,16 @@ class Tractor:
def move(self, direction, cell_size):
if direction == 'up':
self.y -= cell_size
self.angle = 0
#self.angle = 0
if direction == 'down':
self.y += cell_size
self.angle = 180
#self.angle = 180
if direction == 'left':
self.x -= cell_size
self.angle = 90
#self.angle = 90
if direction == 'right':
self.x += cell_size
self.angle = 270
#self.angle = 270
def walk(self):
choice = ['up', 'down', 'left', 'right']