add blocks method, new graphic, rebuild the field type to block type
BIN
__pycache__/blocks.cpython-310.pyc
Normal file
BIN
__pycache__/field.cpython-310.pyc
Normal file
BIN
__pycache__/tractor.cpython-310.pyc
Normal file
43
blocks.py
Normal 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)
|
3
field.py
@ -4,8 +4,9 @@ class Field:
|
|||||||
def __init__(self, parent_screen):
|
def __init__(self, parent_screen):
|
||||||
self.parent_screen = parent_screen
|
self.parent_screen = parent_screen
|
||||||
self.block = pygame.image.load(r'resources/field.png').convert()
|
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 m, posY in enumerate(field_matrix):
|
||||||
for n, posX in enumerate(posY):
|
for n, posX in enumerate(posY):
|
||||||
if field_matrix[m][n] == 1:
|
if field_matrix[m][n] == 1:
|
||||||
|
56
main.py
@ -2,31 +2,42 @@ import pygame
|
|||||||
import random
|
import random
|
||||||
import tractor
|
import tractor
|
||||||
import field
|
import field
|
||||||
|
import blocks
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
cell_size = 50
|
cell_size = 50
|
||||||
screen_size = 500
|
cell_number = 15
|
||||||
|
blocks_number = 15
|
||||||
field_matrix = [[0 for m in range(10)] for n in range(10)]
|
block_dict = {}
|
||||||
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
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.leaf_body = []
|
||||||
|
self.vege_body = []
|
||||||
|
self.carrot_body = []
|
||||||
|
|
||||||
pygame.init()
|
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.blocks.place_blocks(self.surface, self.cell_size, self.leaf_body, 'leaf')
|
||||||
self.field.place_field(self.field_matrix, self.surface, self.cell_size)
|
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 = tractor.Tractor(self.surface, self.cell_size)
|
||||||
self.tractor.draw()
|
self.tractor.draw()
|
||||||
@ -41,7 +52,6 @@ class Game:
|
|||||||
clock.tick(60) # manual fps control not to overwork the computer
|
clock.tick(60) # manual fps control not to overwork the computer
|
||||||
time_now = datetime.now()
|
time_now = datetime.now()
|
||||||
|
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == KEYDOWN:
|
if event.type == KEYDOWN:
|
||||||
if pygame.key.get_pressed()[K_ESCAPE]:
|
if pygame.key.get_pressed()[K_ESCAPE]:
|
||||||
@ -59,9 +69,21 @@ class Game:
|
|||||||
running = False
|
running = False
|
||||||
|
|
||||||
self.surface.fill((140, 203, 97)) # background color
|
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()
|
self.tractor.draw()
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
BIN
resources/2ZAYDK.png
Normal file
After Width: | Height: | Size: 424 B |
BIN
resources/3ZcPVQ.png
Normal file
After Width: | Height: | Size: 427 B |
BIN
resources/4-gFH9.png
Normal file
After Width: | Height: | Size: 421 B |
BIN
resources/DJb-j3.png
Normal file
After Width: | Height: | Size: 492 B |
BIN
resources/NfASJh.png
Normal file
After Width: | Height: | Size: 409 B |
BIN
resources/b8H0jv.png
Normal file
After Width: | Height: | Size: 393 B |
BIN
resources/carrot.png
Normal file
After Width: | Height: | Size: 82 KiB |
BIN
resources/cldvYP.png
Normal file
After Width: | Height: | Size: 411 B |
BIN
resources/cs5N6Z.png
Normal file
After Width: | Height: | Size: 433 B |
BIN
resources/dXE2CG.png
Normal file
After Width: | Height: | Size: 449 B |
BIN
resources/dead.png
Normal file
After Width: | Height: | Size: 455 B |
BIN
resources/flower.png
Normal file
After Width: | Height: | Size: 448 B |
BIN
resources/grass.png
Normal file
After Width: | Height: | Size: 121 KiB |
BIN
resources/grass1.png
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
resources/grass2.png
Normal file
After Width: | Height: | Size: 140 KiB |
BIN
resources/grass3.png
Normal file
After Width: | Height: | Size: 403 B |
BIN
resources/grass4.png
Normal file
After Width: | Height: | Size: 482 B |
BIN
resources/jRdXIV.png
Normal file
After Width: | Height: | Size: 457 B |
BIN
resources/leaf.png
Normal file
After Width: | Height: | Size: 140 KiB |
BIN
resources/red.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
resources/robot2.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
resources/sKlv-b.png
Normal file
After Width: | Height: | Size: 446 B |
BIN
resources/stone.png
Normal file
After Width: | Height: | Size: 412 B |
BIN
resources/tmxa28.png
Normal file
After Width: | Height: | Size: 385 B |
BIN
resources/vege.png
Normal file
After Width: | Height: | Size: 196 KiB |
12
tractor.py
@ -1,13 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
from pygame.math import Vector2
|
||||||
|
|
||||||
class Tractor:
|
class Tractor:
|
||||||
def __init__(self, parent_screen, cell_size):
|
def __init__(self, parent_screen, cell_size):
|
||||||
self.parent_screen = parent_screen
|
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.image = pygame.transform.scale(self.image, (cell_size, cell_size+5))
|
||||||
self.x = cell_size*2
|
self.x = cell_size*2
|
||||||
self.y = cell_size*2
|
self.y = cell_size*2
|
||||||
|
self.pos = Vector2(self.x, self.y)
|
||||||
self.angle = 0
|
self.angle = 0
|
||||||
self.direction = 'up'
|
self.direction = 'up'
|
||||||
|
|
||||||
@ -17,16 +19,16 @@ class Tractor:
|
|||||||
def move(self, direction, cell_size):
|
def move(self, direction, cell_size):
|
||||||
if direction == 'up':
|
if direction == 'up':
|
||||||
self.y -= cell_size
|
self.y -= cell_size
|
||||||
self.angle = 0
|
#self.angle = 0
|
||||||
if direction == 'down':
|
if direction == 'down':
|
||||||
self.y += cell_size
|
self.y += cell_size
|
||||||
self.angle = 180
|
#self.angle = 180
|
||||||
if direction == 'left':
|
if direction == 'left':
|
||||||
self.x -= cell_size
|
self.x -= cell_size
|
||||||
self.angle = 90
|
#self.angle = 90
|
||||||
if direction == 'right':
|
if direction == 'right':
|
||||||
self.x += cell_size
|
self.x += cell_size
|
||||||
self.angle = 270
|
#self.angle = 270
|
||||||
|
|
||||||
def walk(self):
|
def walk(self):
|
||||||
choice = ['up', 'down', 'left', 'right']
|
choice = ['up', 'down', 'left', 'right']
|
||||||
|