added limits with the movements of the chick, added new action(interaction with the block), deleted some icons and minor changes with the titles of the functions

This commit is contained in:
Aliaksei Brown 2023-03-22 21:09:00 +01:00
parent a017bf5587
commit 01e5015b0a
12 changed files with 72 additions and 82 deletions

View File

@ -5,38 +5,43 @@ 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.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha()
self.flower_image = pygame.transform.scale(self.flower_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.stone_image = pygame.image.load(r'resources/stone.png').convert_alpha()
self.stone_image = pygame.transform.scale(self.stone_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))
self.leaf_image = pygame.image.load(r'resources/dead.png').convert_alpha()
self.leaf_image = pygame.transform.scale(self.leaf_image, (cell_size, cell_size))
self.alive_leaf_image = pygame.image.load(r'resources/alive.png').convert_alpha()
self.alive_leaf_image = pygame.transform.scale(self.alive_leaf_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)
self.pos = [self.x,self.y]
body.append(self.pos)
#block_dict.update({self.x : 1}) # for now it may lay on each other,
print(body)
#entire_block.update({self.x : 1}) # for now it may lay on each other,
#print(entire_block)
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))
x = int(block[0] * cell_size)
y = int(block[1] * cell_size)
if color == 'leaf':
self.parent_screen.blit(self.lief_image, (x, y))
if color == 'vege':
self.parent_screen.blit(self.vege_image, (x, y))
self.parent_screen.blit(self.leaf_image, (x, y))
if color == 'alive':
self.parent_screen.blit(self.alive_leaf_image, (x, y))
if color == 'stone':
self.parent_screen.blit(self.stone_image, (x, y))
if color == 'flower':
self.parent_screen.blit(self.flower_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)

View File

@ -1,18 +0,0 @@
import pygame
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): #drawing blocks
for m, posY in enumerate(field_matrix):
for n, posX in enumerate(posY):
if field_matrix[m][n] == 1:
self.parent_screen.blit(self.block, (n * cell_size, m * cell_size))
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)

58
main.py
View File

@ -1,7 +1,6 @@
import pygame
import random
import tractor
import field
import blocks
from pygame.locals import *
from datetime import datetime
@ -9,41 +8,37 @@ from datetime import datetime
class Game:
cell_size = 50
cell_number = 15
cell_number = 15 #horizontally
blocks_number = 15
block_dict = {}
def __init__(self):
self.leaf_body = []
self.vege_body = []
self.carrot_body = []
def __init__(self):
self.dead_leaf_body = []
self.green_leaf_body = []
self.stone_body = []
self.flower_body = []
self.entire_block = {}
pygame.init()
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.image.load(r'resources/grass.png').convert()
self.grass_image = pygame.transform.scale(self.grass_image, (self.cell_size, self.cell_size))
self.bad_grass_image = pygame.image.load(r'resources/bad_grass.png').convert()
self.bad_grass_image = pygame.transform.scale(self.bad_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.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.blocks.locate_blocks(self.blocks_number, self.cell_number, self.dead_leaf_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.stone_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body)
self.tractor = tractor.Tractor(self.surface, self.cell_size)
self.tractor.draw()
def run(self):
running = True
clock = pygame.time.Clock()
last_time = datetime.now()
@ -58,13 +53,15 @@ class Game:
running = False
# in case we want to use keyboard
if pygame.key.get_pressed()[K_UP]:
self.tractor.move('up',self.cell_size)
self.tractor.move('up',self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_DOWN]:
self.tractor.move('down',self.cell_size)
self.tractor.move('down',self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_LEFT]:
self.tractor.move('left',self.cell_size)
self.tractor.move('left',self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_RIGHT]:
self.tractor.move('right',self.cell_size)
self.tractor.move('right',self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_SPACE]:
self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
elif event.type == QUIT:
running = False
@ -77,11 +74,10 @@ class Game:
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.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf')
self.blocks.place_blocks(self.surface, self.cell_size, self.green_leaf_body, 'alive')
self.blocks.place_blocks(self.surface, self.cell_size, self.stone_body, 'stone')
self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower')
self.tractor.draw()

BIN
resources/alive.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
resources/bad_grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 403 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 403 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 196 KiB

View File

@ -4,24 +4,21 @@ from pygame.math import Vector2
class Tractor:
def __init__(self, parent_screen, cell_size):
self.parent_screen = parent_screen
self.up = pygame.image.load(r'resources/up.png').convert_alpha()
self.down = pygame.image.load(r'resources/down.png').convert_alpha()
self.left = pygame.image.load(r'resources/left.png').convert_alpha()
self.right = pygame.image.load(r'resources/right.png').convert_alpha()
self.parent_screen = parent_screen
#self.image = pygame.image.load(r'resources/robot3.png').convert_alpha()
self.up = pygame.transform.scale(self.up, (cell_size, cell_size))
self.up = pygame.transform.scale(self.up, (cell_size+2, cell_size))
self.down = pygame.transform.scale(self.down, (cell_size, cell_size+2))
self.left = pygame.transform.scale(self.left, (cell_size+2, cell_size+2))
self.right = pygame.transform.scale(self.right, (cell_size+3, cell_size+1))
self.right = pygame.transform.scale(self.right, (cell_size+4, cell_size+1))
self.x = cell_size*2
self.y = cell_size*2
self.pos = Vector2(self.x, self.y)
#self.pos = Vector2(self.x, self.y)
self.angle = 0
self.direction = 'up'
self.image = self.down
@ -30,23 +27,33 @@ class Tractor:
def draw(self):
self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor
def move(self, direction, cell_size):
def move(self, direction, cell_size, cell_number):
if direction == 'up':
self.y -= cell_size
if self.y != 0:
self.y -= cell_size
self.image = self.up
#self.angle = 0
if direction == 'down':
self.y += cell_size
if self.y != (cell_number-1)*cell_size:
self.y += cell_size
self.image = self.down
#self.angle = 180
if direction == 'left':
self.x -= cell_size
if self.x != 0:
self.x -= cell_size
self.image = self.left
#self.angle = 90
if direction == 'right':
self.x += cell_size
if self.x != (cell_number-1)*cell_size:
self.x += cell_size
self.image = self.right
#self.angle = 270
print(self.x, self.y)
def water(self, body_before, body_after, cell_size):
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in body_before:
body_before.remove(self.pos)
body_after.append(self.pos)
print('HERE!')
#print(body)
def walk(self):
choice = ['up', 'down', 'left', 'right']