Główny plik uruchomieniowy

basic_grid zaktualizowany na zajęcia nr 4
This commit is contained in:
Cezary Adamczak 2021-03-28 23:49:44 +02:00
parent ec6dc2d5d3
commit 3e11cd8831

View File

@ -11,97 +11,18 @@ from pygame.locals import (
K_ESCAPE,
K_SPACE,
KEYDOWN,
QUIT,
QUIT
)
from field import *
from tractor import *
from plant import *
from colors import *
from dimensions import *
# Initialize pygame
pygame.init()
# Grid size
GSIZE = 10
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 35
HEIGHT = 35
# This sets the margin between each cell
MARGIN = 5
# Window size
SCREEN_WIDTH = GSIZE * (WIDTH + MARGIN) + MARGIN
SCREEN_HEIGHT = GSIZE * (HEIGHT + MARGIN) + MARGIN
# Set some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BROWN = (140, 95, 65)
DBROWN = (65, 50, 20)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
# Define a Tractor object by extending pygame.sprite.Sprite
# The surface drawn on the screen is now an attribute of 'tractor'
class Tractor(pygame.sprite.Sprite):
def __init__(self):
super(Tractor, self).__init__()
self.surf = pygame.Surface((WIDTH, HEIGHT))
self.surf.fill(RED)
self.rect = self.surf.get_rect(topleft=(MARGIN, MARGIN))
self.position = [0, 0]
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -(HEIGHT + MARGIN))
self.position[1] -= 1
if self.rect.top <= MARGIN:
self.rect.top = MARGIN
self.position[1] = 0
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, HEIGHT + MARGIN)
self.position[1] += 1
if self.rect.bottom >= SCREEN_HEIGHT-MARGIN:
self.rect.bottom = SCREEN_HEIGHT-MARGIN
self.position[1] = GSIZE-1
if pressed_keys[K_LEFT]:
self.rect.move_ip(-(WIDTH + MARGIN), 0)
self.position[0] -= 1
if self.rect.left < MARGIN:
self.rect.left = MARGIN
self.position[0] = 0
if pressed_keys[K_RIGHT]:
self.rect.move_ip(WIDTH + MARGIN, 0)
self.position[0] += 1
if self.rect.right > SCREEN_WIDTH-MARGIN:
self.rect.right = SCREEN_WIDTH-MARGIN
self.position[0] = GSIZE-1
def hydrate(self, field, pressed_keys):
if pressed_keys[K_SPACE]:
field[self.position[0]][self.position[1]].hydrate()
# Define a Field object by extending pygame.sprite.Sprite
# The surface drawn on the screen is now an attribute of 'field'
class Field(pygame.sprite.Sprite):
def __init__(self, row, column):
super(Field, self).__init__()
self.surf = pygame.Surface((WIDTH, HEIGHT))
self.surf.fill(BROWN)
self.rect = self.surf.get_rect(topleft=((MARGIN + WIDTH) * row + MARGIN, (MARGIN + HEIGHT) * column + MARGIN))
self.hydration = 0
def hydrate(self):
if self.hydration <= 3:
self.hydration += 1
if self.hydration == 1:
self.surf.fill(YELLOW)
if self.hydration == 2:
self.surf.fill(GREEN)
if self.hydration == 3:
self.surf.fill(BLUE)
# Name the window
pygame.display.set_caption("Inteligentny Traktor")
@ -109,9 +30,6 @@ pygame.display.set_caption("Inteligentny Traktor")
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate tractor. Right now, this is just a rectangle.
tractor = Tractor()
# Create field array
field = []
@ -121,13 +39,23 @@ for row in range(GSIZE):
fieldbit = Field(row, column)
field[row].append(fieldbit)
tractor = Tractor(field[0][0])
plant = Plant(field[1][1], "wheat")
print(tractor.rect)
print(field[0][0].rect)
# Variable to keep the main loop running
running = True
RUNNING = True
ticker = 0
clock = pygame.time.Clock()
# Main loop
while running:
while RUNNING:
# Look at every event in the queue
for event in pygame.event.get():
@ -135,11 +63,10 @@ while running:
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
running = False
RUNNING = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
running = False
RUNNING = False
# Get all keys pressed at a time
pressed_keys = pygame.key.get_pressed()
@ -156,12 +83,22 @@ while running:
for column in range(GSIZE):
screen.blit(field[row][column].surf, field[row][column].rect)
# Draw the player on the screen
screen.blit(tractor.surf, tractor.rect)
# Draw the player on the screen
screen.blit(plant.surf, plant.rect)
if ticker == 0:
for row in range(GSIZE):
for column in range(GSIZE):
field[row][column].dehydrate()
plant.grow()
ticker = (ticker + 1)%4
# Update the screen
pygame.display.flip()
# Ensure program maintains a rate of 15 frames per second
clock.tick(15)
clock.tick(4)