automatyczny_kelner/models/Object.py

40 lines
1.1 KiB
Python
Raw Normal View History

2023-03-31 00:48:17 +02:00
import pygame
class Object:
def __init__(self, role, square_size, screen_size, left_square, top_square):
self.role = role
self.image = pygame.transform.scale(pygame.image.load(
'images/{0}.png'.format(role)), (square_size, square_size))
self.square_size = square_size
self.screen_size = screen_size
left = left_square * square_size
top = top_square * square_size
self.rect = pygame.Rect(left, top, square_size, square_size)
def up(self):
if self.rect.top > 0:
self.rect.top -= self.square_size
def down(self):
if self.rect.bottom < self.screen_size[1]:
self.rect.top += self.square_size
def left(self):
if self.rect.left > 0:
self.rect.left -= self.square_size
def right(self):
if self.rect.right < self.screen_size[0]:
self.rect.left += self.square_size
def blit(self, screen):
screen.blit(self.image, self.rect)
def collision(self, obj):
x = self.rect.left == obj.rect.left
y = self.rect.top == obj.rect.top
return x and y