2023-03-10 22:52:29 +01:00
|
|
|
import pygame
|
|
|
|
import random
|
2023-03-13 10:36:49 +01:00
|
|
|
from pygame.locals import *
|
2023-03-10 22:52:29 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-03-13 10:36:49 +01:00
|
|
|
#main variables
|
|
|
|
class Configurations:
|
|
|
|
cell_size = 50
|
|
|
|
screen_size = 500
|
|
|
|
# def _init_(self):
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
class Lines:
|
|
|
|
def __init__(self, parent_scr):
|
|
|
|
self.parent_scr = parent_scr
|
2023-03-13 10:36:49 +01:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
def draw_lines(self): # background lines
|
2023-03-13 10:36:49 +01:00
|
|
|
conf = Configurations()
|
2023-03-10 22:52:29 +01:00
|
|
|
for i in range(1, 10):
|
2023-03-13 10:36:49 +01:00
|
|
|
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)
|
2023-03-13 14:38:22 +01:00
|
|
|
#pygame.display.flip()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Tractor:
|
|
|
|
def __init__(self, parent_screen):
|
2023-03-13 14:38:22 +01:00
|
|
|
conf = Configurations()
|
2023-03-10 22:52:29 +01:00
|
|
|
self.parent_screen = parent_screen
|
2023-03-13 14:38:22 +01:00
|
|
|
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
|
2023-03-10 22:52:29 +01:00
|
|
|
self.angle = 0
|
|
|
|
self.direction = 'up'
|
|
|
|
|
|
|
|
def draw(self):
|
2023-03-13 10:36:49 +01:00
|
|
|
#self.parent_screen.fill((120, 120, 0)) # background color
|
2023-03-13 14:38:22 +01:00
|
|
|
#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
|
2023-03-10 22:52:29 +01:00
|
|
|
|
2023-03-11 20:43:06 +01:00
|
|
|
def move(self, direction):
|
2023-03-13 10:36:49 +01:00
|
|
|
conf = Configurations()
|
2023-03-11 20:43:06 +01:00
|
|
|
if direction == 'up':
|
2023-03-13 10:36:49 +01:00
|
|
|
self.y -= conf.cell_size
|
2023-03-11 20:43:06 +01:00
|
|
|
self.angle = 0
|
|
|
|
if direction == 'down':
|
2023-03-13 10:36:49 +01:00
|
|
|
self.y += conf.cell_size
|
2023-03-11 20:43:06 +01:00
|
|
|
self.angle = 180
|
|
|
|
if direction == 'left':
|
2023-03-13 10:36:49 +01:00
|
|
|
self.x -= conf.cell_size
|
2023-03-11 20:43:06 +01:00
|
|
|
self.angle = 90
|
|
|
|
if direction == 'right':
|
2023-03-13 10:36:49 +01:00
|
|
|
self.x += conf.cell_size
|
2023-03-11 20:43:06 +01:00
|
|
|
self.angle = 270
|
2023-03-13 10:36:49 +01:00
|
|
|
#self.draw()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
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)
|
2023-03-11 20:43:06 +01:00
|
|
|
self.move(self.direction)
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
|
2023-03-12 20:22:10 +01:00
|
|
|
class Field:
|
|
|
|
def __init__(self, parent_screen):
|
|
|
|
self.parent_screen = parent_screen
|
2023-03-13 10:36:49 +01:00
|
|
|
self.block = pygame.image.load(r'resources/field.png').convert()
|
2023-03-12 20:22:10 +01:00
|
|
|
|
|
|
|
def place_field(self, field_matrix):
|
2023-03-13 10:36:49 +01:00
|
|
|
conf = Configurations()
|
2023-03-12 20:22:10 +01:00
|
|
|
for m, posY in enumerate(field_matrix):
|
|
|
|
for n, posX in enumerate(posY):
|
|
|
|
if field_matrix[m][n] == 1:
|
2023-03-13 10:36:49 +01:00
|
|
|
self.parent_screen.blit(self.block, (n * conf.cell_size, m * conf.cell_size))
|
2023-03-12 20:22:10 +01:00
|
|
|
|
2023-03-13 14:38:22 +01:00
|
|
|
#pygame.display.flip()
|
2023-03-12 20:22:10 +01:00
|
|
|
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
class Game:
|
2023-03-12 20:22:10 +01:00
|
|
|
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
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
def __init__(self):
|
|
|
|
pygame.init()
|
2023-03-13 10:36:49 +01:00
|
|
|
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
|
2023-03-10 22:52:29 +01:00
|
|
|
# self.surface.fill((255, 255, 255)) # background color (overwritten by tractor)
|
|
|
|
|
2023-03-13 10:36:49 +01:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
self.lines = Lines(self.surface)
|
2023-03-12 20:22:10 +01:00
|
|
|
self.field = Field(self.surface)
|
|
|
|
self.field.place_field(self.field_matrix)
|
|
|
|
|
2023-03-13 10:36:49 +01:00
|
|
|
self.tractor = Tractor(self.surface)
|
|
|
|
self.tractor.draw()
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
def run(self):
|
|
|
|
running = True
|
2023-03-13 10:36:49 +01:00
|
|
|
clock = pygame.time.Clock()
|
2023-03-10 22:52:29 +01:00
|
|
|
last_time = datetime.now()
|
2023-03-13 10:36:49 +01:00
|
|
|
#self.lines.draw_lines()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
while running:
|
2023-03-13 10:36:49 +01:00
|
|
|
clock.tick(60) # manual fps control not to overwork the computer
|
2023-03-10 22:52:29 +01:00
|
|
|
time_now = datetime.now()
|
|
|
|
|
2023-03-13 14:38:22 +01:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == KEYDOWN:
|
|
|
|
if pygame.key.get_pressed()[K_ESCAPE]:
|
|
|
|
running = False
|
|
|
|
# in case we want to use keyboard
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_UP]:
|
|
|
|
self.tractor.move('up')
|
|
|
|
if pygame.key.get_pressed()[K_DOWN]:
|
|
|
|
self.tractor.move('down')
|
|
|
|
if pygame.key.get_pressed()[K_LEFT]:
|
|
|
|
self.tractor.move('left')
|
|
|
|
if pygame.key.get_pressed()[K_RIGHT]:
|
|
|
|
self.tractor.move('right')
|
2023-03-10 22:52:29 +01:00
|
|
|
elif event.type == QUIT:
|
|
|
|
running = False
|
|
|
|
|
2023-03-13 14:38:22 +01:00
|
|
|
self.surface.fill((140, 203, 97)) # background color
|
2023-03-12 20:22:10 +01:00
|
|
|
self.field.place_field(self.field_matrix)
|
2023-03-10 22:52:29 +01:00
|
|
|
self.lines.draw_lines()
|
2023-03-13 10:36:49 +01:00
|
|
|
self.tractor.draw()
|
2023-03-13 14:38:22 +01:00
|
|
|
pygame.display.update()
|
2023-03-13 10:36:49 +01:00
|
|
|
|
|
|
|
|
2023-03-13 14:38:22 +01:00
|
|
|
# if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
|
|
|
|
# last_time = datetime.now()
|
2023-03-13 10:36:49 +01:00
|
|
|
#self.tractor.walk()
|
|
|
|
#print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})')
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
game = Game()
|
|
|
|
game.run()
|