Zaktualizuj 'tractor.py'

This commit is contained in:
Cezary Adamczak 2021-05-10 14:37:52 +02:00
parent 23a91cea55
commit 144d6a8746

View File

@ -1,60 +1,90 @@
import pygame import pygame
from pygame.locals import ( from pygame.locals import (
K_UP, K_UP,
K_DOWN, K_DOWN,
K_LEFT, K_LEFT,
K_RIGHT, K_RIGHT,
K_ESCAPE, K_ESCAPE,
K_SPACE, K_SPACE,
K_c, K_c,
KEYDOWN, KEYDOWN,
QUIT QUIT
) )
from dimensions import * from dimensions import *
from colors import * from colors import *
from sprites import *
class Tractor(pygame.sprite.Sprite):
def __init__(self, field): class Tractor(pygame.sprite.Sprite):
super(Tractor, self).__init__() def __init__(self, field):
self.surf = pygame.Surface((WIDTH, HEIGHT)) super(Tractor, self).__init__()
self.surf.fill(RED) self.surf = pygame.Surface((WIDTH, HEIGHT))
self.position = field.position self.surf = tractor_img_0
self.rect = self.surf.get_rect( self.position = field.position
topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN)) self.rect = self.surf.get_rect(
self.field = field topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN))
self.direction = [1, 0]
def update(self, pressed_keys):
if pressed_keys[K_UP]: def move(self):
self.rect.move_ip(0, -(HEIGHT + MARGIN)) self.rect.move_ip(self.direction[0]*(WIDTH + MARGIN), self.direction[1]*(HEIGHT + MARGIN))
self.position[1] -= 1 self.position[0] += self.direction[0]
if self.rect.top <= MARGIN: self.position[1] += self.direction[1]
self.rect.top = MARGIN if self.position[0] >= GSIZE:
self.position[1] = 0 self.position[0] = GSIZE-1
if pressed_keys[K_DOWN]: if self.position[1] >= GSIZE:
self.rect.move_ip(0, HEIGHT + MARGIN) self.position[1] = GSIZE-1
self.position[1] += 1 if self.position[0] < 0:
if self.rect.bottom >= SCREEN_HEIGHT-MARGIN: self.position[0] = 0
self.rect.bottom = SCREEN_HEIGHT-MARGIN if self.position[1] < 0:
self.position[1] = GSIZE-1 self.position[1] = 0
if pressed_keys[K_LEFT]:
self.rect.move_ip(-(WIDTH + MARGIN), 0) if self.rect.top <= MARGIN:
self.position[0] -= 1 self.rect.top = MARGIN
if self.rect.left < MARGIN: if self.rect.bottom >= SCREEN_HEIGHT-MARGIN:
self.rect.left = MARGIN self.rect.bottom = SCREEN_HEIGHT-MARGIN
self.position[0] = 0 if self.rect.left < MARGIN:
if pressed_keys[K_RIGHT]: self.rect.left = MARGIN
self.rect.move_ip(WIDTH + MARGIN, 0) if self.rect.right > SCREEN_WIDTH-MARGIN:
self.position[0] += 1 self.rect.right = SCREEN_WIDTH-MARGIN
if self.rect.right > SCREEN_WIDTH-MARGIN:
self.rect.right = SCREEN_WIDTH-MARGIN
self.position[0] = GSIZE-1 def rotate_right(self):
if self.direction == [1, 0]:
def hydrate(self, field, pressed_keys): self.direction = [0, 1]
if pressed_keys[K_SPACE]: self.surf = tractor_img_2
field[self.position[0]][self.position[1]].hydrate() elif self.direction == [0, 1]:
self.direction = [-1, 0]
def cut(self, field, pressed_keys): self.surf = tractor_img_1
if pressed_keys[K_c]: elif self.direction == [-1, 0]:
field[self.position[0]][self.position[1]].free() self.direction = [0, -1]
self.surf = tractor_img_3
elif self.direction == [0, -1]:
self.direction = [1, 0]
self.surf = tractor_img_0
def rotate_left(self):
if self.direction == [1, 0]:
self.direction = [0, -1]
self.surf = tractor_img_3
elif self.direction == [0, -1]:
self.direction = [-1, 0]
self.surf = tractor_img_1
elif self.direction == [-1, 0]:
self.direction = [0, 1]
self.surf = tractor_img_2
elif self.direction == [0, 1]:
self.direction = [1, 0]
self.surf = tractor_img_0
def hydrate(self, field):
field[self.position[0]][self.position[1]].hydrate()
def cut(self, field, pressed_keys):
if pressed_keys[K_c]:
field[self.position[0]][self.position[1]].free()
def plant(self, field, plant, pressed_keys):
if field.planted == 0:
field.planted = plant
plant.field = field