fix tractor movement, added function to tractor and plant #9

Merged
s473556 merged 2 commits from tractor_movement into main 2023-03-26 17:17:03 +02:00
10 changed files with 92 additions and 79 deletions

Binary file not shown.

17
main.py
View File

@ -9,7 +9,7 @@ from src.Plant import Plant
# pygame initialization # pygame initialization
pygame.init() pygame.init()
clock = pygame.time.Clock() clock = pygame.time.Clock()
pygame.mouse.set_visible(False) #pygame.mouse.set_visible(False)
#GAME SCREEN #GAME SCREEN
screen = pygame.display.set_mode(SIZE) screen = pygame.display.set_mode(SIZE)
@ -25,8 +25,10 @@ for line in range(26):
pygame.draw.line(background, (0, 0, 0), (line * 36, 0), (line * 36, SIZE[1])) pygame.draw.line(background, (0, 0, 0), (line * 36, 0), (line * 36, SIZE[1]))
#TRACTOR #TRACTOR
tractor = Tractor('oil','manual') tractor = Tractor('oil','manual', 'fuel', 'fertilizer1')
tractor_group = pygame.sprite.Group() tractor_group = pygame.sprite.Group()
tractor.rect.x = 0
tractor.rect.y = 0
tractor_group.add(tractor) tractor_group.add(tractor)
#PLANTS #PLANTS
@ -42,13 +44,16 @@ if __name__ == "__main__":
running = False running = False
pygame.quit() pygame.quit()
sys.exit() sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.KEYDOWN:
tractor.collect(plant_group) if event.key==pygame.K_RETURN:
tractor.collect(plant_group)
if event.key == pygame.K_ESCAPE:
running = False
pygame.display.flip() Tractor.movement(tractor)
screen.blit(background,(0,0)) screen.blit(background,(0,0))
plant_group.draw(screen) plant_group.draw(screen)
tractor_group.draw(screen) tractor_group.draw(screen)
tractor_group.update() tractor_group.update()
pygame.display.flip()
clock.tick(60) clock.tick(60)

21
src/Field.py Normal file
View File

@ -0,0 +1,21 @@
from pygame.sprite import Sprite
class Field(Sprite):
def __init__(self, type, row_id, col_id, image, cost, hydration_level , soil,
fertilizer_degree, development_degree, plant_type, fertilizer_type, to_water):
self.type = type
self.row_id = row_id
self.col_id = col_id
self.position = (row_id, col_id)
self.image = image
self.cost = cost
self.hydration_level = hydration_level
self.soil = soil
self.fertilizer_degree = fertilizer_degree
self.development_degree = development_degree
self.plant_type = plant_type
self.fertilizer_type = fertilizer_type
self.to_water = to_water

View File

@ -9,24 +9,36 @@ class Plant(pygame.sprite.Sprite):
if species=="carrot": if species=="carrot":
self.growth_time=100 self.growth_time=100
self.weight=50 self.weight=50
self.min_hydration = 30
self.max_hydration = 60
self.soil_type = "torf"
self.fertilizer="carrot_fertilizer" self.fertilizer="carrot_fertilizer"
self.pic_path="assets/Carrot.png" self.pic_path="assets/Carrot.png"
elif species=="beetroot": elif species=="beetroot":
self.growth_time=200 self.growth_time=200
self.weight=200 self.weight=200
self.min_hydration = 20
self.max_hydration = 60
self.soil_type = "piaszczyste"
self.fertilizer="beetroot_fertilizer" self.fertilizer="beetroot_fertilizer"
self.pic_path="assets/Beetroot.png" self.pic_path="assets/Beetroot.png"
elif species=="potato": elif species=="potato":
self.growth_time=100 self.growth_time=100
self.weight=100 self.weight=100
self.min_hydration = 10
self.max_hydration = 30
self.soil_type = "ilaste"
self.fertilizer="potatoe_fertilizer" self.fertilizer="potatoe_fertilizer"
self.pic_path="assets/Potato.png" self.pic_path="assets/Potato.png"
else: else:
self.growth_time=250 self.growth_time=250
self.weight=75 self.weight=75
self.min_hydration = 10
self.max_hydration = 65
self.soil_type = "gliniaste"
self.fertilizer="wheat_fertilizer" self.fertilizer="wheat_fertilizer"
self.pic_path="assets/Wheat.png" self.pic_path="assets/Wheat.png"

View File

@ -1,23 +1,60 @@
import pygame import pygame
class Tractor(pygame.sprite.Sprite): class Tractor(pygame.sprite.Sprite):
def __init__(self,engine,transmission): def __init__(self,engine,transmission,fuel,fertilizer):
super().__init__() super().__init__()
self.image=pygame.image.load("assets/tractor/tractor.png") self.image=pygame.image.load("assets/tractor/tractor.png")
self.image=pygame.transform.scale(self.image,(36,36)) self.image=pygame.transform.scale(self.image,(36,36))
self.UP = pygame.transform.rotate(self.image, 0)
self.DOWN = pygame.transform.rotate(self.image, 180)
self.LEFT = pygame.transform.rotate(self.image, 90)
self.RIGHT = pygame.transform.rotate(self.image, -90)
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.engine=engine self.engine=engine
self.transmission=transmission self.transmission=transmission
self.fuel=100 self.fuel=fuel
self.fertilizer=fertilizer
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.x>0:
self.image = self.LEFT
self.rect.x -= 36
if keys[pygame.K_RIGHT] and self.rect.x<900:
self.image = self.RIGHT
self.rect.x += 36
if keys[pygame.K_UP] and self.rect.y>0:
self.image = self.UP
self.rect.y -= 36
if keys[pygame.K_DOWN] and self.rect.y<900:
self.image = self.DOWN
self.rect.y += 36
def collect(self,plant_group): def collect(self,plant_group):
self.plant_group=plant_group self.plant_group=plant_group
print("collected plant") print("collected plant")
pygame.sprite.spritecollide(self,self.plant_group,True) pygame.sprite.spritecollide(self,self.plant_group,True)
# collected=collected+1 # collected=collected+1
# print("plants in trunk "+collected) # print("plants in trunk "+collected)
def update(self): def water_plant(self,plant_group):
self.rect.center=pygame.mouse.get_pos() self.plant_group=plant_group
print("watered plant")
# def update(self):
# self.rect.center=pygame.mouse.get_pos()
def fertilize(self, plant_group):
self.plant_group=plant_group
print("fertilize")
def plant(self, plant_group):
self.plant_group=plant_group
print("new plant")
def find_nearest_plant(self,plant_group):
self.plant_group=plant_group

Binary file not shown.

View File

@ -1,29 +0,0 @@
import pygame
# size in pixels of one tile = 36px/36px
tile = (36, 36)
# later move it to another class "barn"?
barn_img = pygame.image.load('assets/barn.png')
barn = pygame.transform.scale(barn_img, tile)
# screen settings
SIZE = (900, 900)
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Traktor_interaktor")
# screen dispaly
def set_screen(myTractor):
# setting background color
SCREEN.fill((90,50,20))
# dispaly agent icon
TRACTOR = SCREEN.blit(myTractor.IMG, (myTractor.x, myTractor.y))
# display barn
SCREEN.blit(barn, (0, 863))
# draw lines(horizontal, vertical) on the screen
for line in range(25):
pygame.draw.line(SCREEN, (0, 0, 0), (0, line * 36), (SIZE[0], line * 36))
pygame.draw.line(SCREEN, (0, 0, 0), (line * 36, 0), (line * 36, SIZE[1]))
pygame.display.update()

View File

@ -1,33 +0,0 @@
import pygame
class Tractor:
# this is where tractor spawns when program starts (center)
x=432
y=432
# it's speed -> pixels it moves after pressing arrow
speed = 36
# tractor image
tractor_img = pygame.image.load('assets/tractor/tractor.png')
IMG = pygame.transform.scale(tractor_img, (36, 36))
# tractor image rotation
UP = pygame.transform.rotate(IMG, 0)
DOWN = pygame.transform.rotate(IMG, 180)
LEFT = pygame.transform.rotate(IMG, 90)
RIGHT = pygame.transform.rotate(IMG, -90)
def movement(myTractor):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and myTractor.x>0:
myTractor.IMG = myTractor.LEFT
myTractor.x -= myTractor.speed
if keys[pygame.K_RIGHT] and myTractor.x<900-myTractor.speed:
myTractor.IMG = myTractor.RIGHT
myTractor.x += myTractor.speed
if keys[pygame.K_UP] and myTractor.y>0:
myTractor.IMG = myTractor.UP
myTractor.y -= myTractor.speed
if keys[pygame.K_DOWN] and myTractor.y<900-myTractor.speed:
myTractor.IMG = myTractor.DOWN
myTractor.y += myTractor.speed