Główny plik uruchomieniowy
basic_grid zaktualizowany na zajęcia nr 4
This commit is contained in:
parent
ec6dc2d5d3
commit
3e11cd8831
131
basic_grid.py
131
basic_grid.py
@ -11,97 +11,18 @@ from pygame.locals import (
|
|||||||
K_ESCAPE,
|
K_ESCAPE,
|
||||||
K_SPACE,
|
K_SPACE,
|
||||||
KEYDOWN,
|
KEYDOWN,
|
||||||
QUIT,
|
QUIT
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from field import *
|
||||||
|
from tractor import *
|
||||||
|
from plant import *
|
||||||
|
from colors import *
|
||||||
|
from dimensions import *
|
||||||
|
|
||||||
# Initialize pygame
|
# Initialize pygame
|
||||||
pygame.init()
|
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
|
# Name the window
|
||||||
pygame.display.set_caption("Inteligentny Traktor")
|
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
|
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, 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
|
# Create field array
|
||||||
field = []
|
field = []
|
||||||
|
|
||||||
@ -121,13 +39,23 @@ for row in range(GSIZE):
|
|||||||
fieldbit = Field(row, column)
|
fieldbit = Field(row, column)
|
||||||
field[row].append(fieldbit)
|
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
|
# Variable to keep the main loop running
|
||||||
running = True
|
RUNNING = True
|
||||||
|
|
||||||
|
ticker = 0
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
while running:
|
while RUNNING:
|
||||||
|
|
||||||
# Look at every event in the queue
|
# Look at every event in the queue
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
@ -135,11 +63,10 @@ while running:
|
|||||||
if event.type == KEYDOWN:
|
if event.type == KEYDOWN:
|
||||||
# Was it the Escape key? If so, stop the loop.
|
# Was it the Escape key? If so, stop the loop.
|
||||||
if event.key == K_ESCAPE:
|
if event.key == K_ESCAPE:
|
||||||
running = False
|
RUNNING = False
|
||||||
|
|
||||||
# Did the user click the window close button? If so, stop the loop.
|
# Did the user click the window close button? If so, stop the loop.
|
||||||
elif event.type == QUIT:
|
elif event.type == QUIT:
|
||||||
running = False
|
RUNNING = False
|
||||||
|
|
||||||
# Get all keys pressed at a time
|
# Get all keys pressed at a time
|
||||||
pressed_keys = pygame.key.get_pressed()
|
pressed_keys = pygame.key.get_pressed()
|
||||||
@ -156,12 +83,22 @@ while running:
|
|||||||
for column in range(GSIZE):
|
for column in range(GSIZE):
|
||||||
screen.blit(field[row][column].surf, field[row][column].rect)
|
screen.blit(field[row][column].surf, field[row][column].rect)
|
||||||
|
|
||||||
# Draw the player on the screen
|
|
||||||
screen.blit(tractor.surf, tractor.rect)
|
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
|
# Update the screen
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
# Ensure program maintains a rate of 15 frames per second
|
# Ensure program maintains a rate of 15 frames per second
|
||||||
clock.tick(15)
|
clock.tick(4)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user