1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import pygame
from color import Color
class Image:
def __init__(self, x, y, image_path, border_color=Color.BLACK, border_width=0):
self.x = x
self.y = y
self.image_path = image_path
self.image = pygame.image.load(self.image_path)
self.width = self.image.get_width()
self.height = self.image.get_height()
self.border_color = border_color
self.border_width = border_width
def get_width(self):
return self.width
def get_height(self):
return self.height
def center(self, x=None, y=None):
if x is None:
x = self.x
if y is None:
y = self.y
self.set_position(x - self.width / 2, y - self.height / 2)
def set_position(self, x, y):
self.x = x
self.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
def set_image(self, image_path):
self.image_path = image_path
self.image = pygame.image.load(self.image_path)
self.width = self.image.get_width()
self.height = self.image.get_height()
def set_border_color(self, border_color):
self.border_color = border_color
def resize(self, width, height):
self.image = pygame.transform.scale(self.image, (width, height))
self.width = width
self.height = height
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
pygame.draw.rect(screen, self.border_color, (self.x, self.y, self.width, self.height), self.border_width)
|