Basic ruch traktorka

This commit is contained in:
mikowhychuck 2024-03-09 20:02:19 +01:00
parent 282144395b
commit 044760780b
7 changed files with 35 additions and 8 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 408 B

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@ -13,9 +13,10 @@ if __name__ == "__main__":
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
field.tractor.update()
screen.fill(WHITE) screen.fill(WHITE)
field.draw(screen) field.draw(screen)
pygame.display.flip() pygame.display.flip()
pygame.time.Clock().tick(30) pygame.time.Clock().tick(10)

View File

@ -9,11 +9,17 @@ class Tile(pygame.sprite.Sprite):
y = id//16 y = id//16
self.type = type self.type = type
self.field = field self.field = field
if type=='grass': self.set_type(type)
self.image = pygame.image.load('images/grass.png').convert()
self.image = pygame.transform.scale(self.image, (64, 64))
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.topleft = (x * 64, y * 64) self.rect.topleft = (x * 64, y * 64)
def draw(self, surface): def draw(self, surface):
self.tiles.draw(surface) self.tiles.draw(surface)
def set_type(self, type):
self.type = type
if self.type == 'grass':
self.image = pygame.image.load('images/grass.png').convert()
self.image = pygame.transform.scale(self.image, (64, 64))

View File

@ -2,8 +2,7 @@ import pygame
import os import os
class Tractor(pygame.sprite.Sprite): class Tractor(pygame.sprite.Sprite):
def __init__(self, field): def __init__(self, field):
super().__init__() super().__init__
self.type = type
self.field = field self.field = field
self.image = pygame.image.load('images/tractor.png').convert_alpha() self.image = pygame.image.load('images/tractor.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (64, 64)) self.image = pygame.transform.scale(self.image, (64, 64))
@ -13,3 +12,24 @@ class Tractor(pygame.sprite.Sprite):
def draw(self, surface): def draw(self, surface):
surface.blit(self.image, self.rect) surface.blit(self.image, self.rect)
def move(self, direction):
if direction == "up":
self.rect.y -= 64
elif direction == "down":
self.rect.y += 64
elif direction == "left":
self.rect.x -= 64
elif direction == "right":
self.rect.x += 64
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.move('left')
if keys[pygame.K_RIGHT]:
self.move('right')
if keys[pygame.K_UP]:
self.move('up')
if keys[pygame.K_DOWN]:
self.move('down')