modules organazation
This commit is contained in:
parent
624a50e5d8
commit
4bb7222dff
17
field.py
Normal file
17
field.py
Normal file
@ -0,0 +1,17 @@
|
||||
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 place_field(self, field_matrix, parent_screen, cell_size):
|
||||
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)
|
121
main.py
121
main.py
@ -1,93 +1,15 @@
|
||||
import pygame
|
||||
import random
|
||||
import tractor
|
||||
import field
|
||||
from pygame.locals import *
|
||||
from datetime import datetime
|
||||
|
||||
#main variables
|
||||
class Configurations:
|
||||
cell_size = 50
|
||||
screen_size = 500
|
||||
# def _init_(self):
|
||||
|
||||
class Lines:
|
||||
def __init__(self, parent_scr):
|
||||
self.parent_scr = parent_scr
|
||||
|
||||
|
||||
def draw_lines(self): # background lines
|
||||
conf = Configurations()
|
||||
for i in range(1, 10):
|
||||
pygame.draw.line(self.parent_scr, (228, 253, 227), (conf.cell_size * i, 0), (conf.cell_size * i, conf.screen_size), 1)
|
||||
pygame.draw.line(self.parent_scr, (228, 253, 227), (0, conf.cell_size * i), (conf.screen_size, conf.cell_size * i), 1)
|
||||
#pygame.display.flip()
|
||||
|
||||
|
||||
class Tractor:
|
||||
def __init__(self, parent_screen):
|
||||
conf = Configurations()
|
||||
self.parent_screen = parent_screen
|
||||
self.image = pygame.image.load(r'resources/tractor.png').convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (conf.cell_size, conf.cell_size+5))
|
||||
#self.block = pygame.Rect(int(conf.cell_size), int(conf.cell_size), conf.cell_size, conf.cell_size)
|
||||
self.x = conf.cell_size*2
|
||||
self.y = conf.cell_size*2
|
||||
self.angle = 0
|
||||
self.direction = 'up'
|
||||
|
||||
def draw(self):
|
||||
#self.parent_screen.fill((120, 120, 0)) # background color
|
||||
#self.parent_screen.blit(self.block, (self.x, self.y))
|
||||
self.parent_screen.blit(pygame.transform.rotate(self.image, self.angle), (self.x, self.y)) # rotate tractor
|
||||
#pygame.display.flip() # updating screen
|
||||
|
||||
def move(self, direction):
|
||||
conf = Configurations()
|
||||
if direction == 'up':
|
||||
self.y -= conf.cell_size
|
||||
self.angle = 0
|
||||
if direction == 'down':
|
||||
self.y += conf.cell_size
|
||||
self.angle = 180
|
||||
if direction == 'left':
|
||||
self.x -= conf.cell_size
|
||||
self.angle = 90
|
||||
if direction == 'right':
|
||||
self.x += conf.cell_size
|
||||
self.angle = 270
|
||||
#self.draw()
|
||||
|
||||
def walk(self):
|
||||
choice = ['up', 'down', 'left', 'right']
|
||||
|
||||
if self.x == 450:
|
||||
choice.pop(3)
|
||||
if self.x == 0:
|
||||
choice.pop(2)
|
||||
if self.y == 0:
|
||||
choice.pop(0)
|
||||
if self.y == 450:
|
||||
choice.pop(1)
|
||||
|
||||
self.direction = random.choice(choice)
|
||||
self.move(self.direction)
|
||||
|
||||
|
||||
class Field:
|
||||
def __init__(self, parent_screen):
|
||||
self.parent_screen = parent_screen
|
||||
self.block = pygame.image.load(r'resources/field.png').convert()
|
||||
|
||||
def place_field(self, field_matrix):
|
||||
conf = Configurations()
|
||||
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 * conf.cell_size, m * conf.cell_size))
|
||||
|
||||
#pygame.display.flip()
|
||||
|
||||
|
||||
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:
|
||||
@ -100,26 +22,20 @@ class Game:
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.conf = Configurations()
|
||||
# self.screenWidth = 500
|
||||
# self.screenHeight = 500
|
||||
self.surface = pygame.display.set_mode((self.conf.screen_size, self.conf.screen_size)) # initialize a window
|
||||
# self.surface.fill((255, 255, 255)) # background color (overwritten by tractor)
|
||||
self.surface = pygame.display.set_mode((self.screen_size, self.screen_size)) # initialize a window
|
||||
|
||||
|
||||
|
||||
self.lines = Lines(self.surface)
|
||||
self.field = Field(self.surface)
|
||||
self.field.place_field(self.field_matrix)
|
||||
self.field = field.Field(self.surface)
|
||||
self.field.place_field(self.field_matrix, self.surface, self.cell_size)
|
||||
|
||||
self.tractor = Tractor(self.surface)
|
||||
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()
|
||||
#self.lines.draw_lines()
|
||||
|
||||
while running:
|
||||
clock.tick(60) # manual fps control not to overwork the computer
|
||||
@ -132,25 +48,26 @@ class Game:
|
||||
running = False
|
||||
# in case we want to use keyboard
|
||||
if pygame.key.get_pressed()[K_UP]:
|
||||
self.tractor.move('up')
|
||||
self.tractor.move('up',self.cell_size)
|
||||
if pygame.key.get_pressed()[K_DOWN]:
|
||||
self.tractor.move('down')
|
||||
self.tractor.move('down',self.cell_size)
|
||||
if pygame.key.get_pressed()[K_LEFT]:
|
||||
self.tractor.move('left')
|
||||
self.tractor.move('left',self.cell_size)
|
||||
if pygame.key.get_pressed()[K_RIGHT]:
|
||||
self.tractor.move('right')
|
||||
self.tractor.move('right',self.cell_size)
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
|
||||
self.surface.fill((140, 203, 97)) # background color
|
||||
self.field.place_field(self.field_matrix)
|
||||
self.lines.draw_lines()
|
||||
self.field.place_field(self.field_matrix, self.surface, self.cell_size)
|
||||
|
||||
self.field.draw_lines(self.screen_size, self.cell_size)
|
||||
self.tractor.draw()
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
# if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
|
||||
# last_time = datetime.now()
|
||||
# if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
|
||||
#last_time = datetime.now()
|
||||
#self.tractor.walk()
|
||||
#print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})')
|
||||
|
||||
|
44
tractor.py
Normal file
44
tractor.py
Normal file
@ -0,0 +1,44 @@
|
||||
import pygame
|
||||
import random
|
||||
|
||||
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.transform.scale(self.image, (cell_size, cell_size+5))
|
||||
self.x = cell_size*2
|
||||
self.y = cell_size*2
|
||||
self.angle = 0
|
||||
self.direction = 'up'
|
||||
|
||||
def draw(self):
|
||||
self.parent_screen.blit(pygame.transform.rotate(self.image, self.angle), (self.x, self.y)) # rotate tractor
|
||||
|
||||
def move(self, direction, cell_size):
|
||||
if direction == 'up':
|
||||
self.y -= cell_size
|
||||
self.angle = 0
|
||||
if direction == 'down':
|
||||
self.y += cell_size
|
||||
self.angle = 180
|
||||
if direction == 'left':
|
||||
self.x -= cell_size
|
||||
self.angle = 90
|
||||
if direction == 'right':
|
||||
self.x += cell_size
|
||||
self.angle = 270
|
||||
|
||||
def walk(self):
|
||||
choice = ['up', 'down', 'left', 'right']
|
||||
|
||||
if self.x == 450:
|
||||
choice.pop(3)
|
||||
if self.x == 0:
|
||||
choice.pop(2)
|
||||
if self.y == 0:
|
||||
choice.pop(0)
|
||||
if self.y == 450:
|
||||
choice.pop(1)
|
||||
|
||||
self.direction = random.choice(choice)
|
||||
self.move(self.direction)
|
Loading…
Reference in New Issue
Block a user