Repository navigation
Expand file tree
/
Copy pathPygame_Utils.py
More file actions
101 lines (77 loc) · 2.77 KB
/
Copy pathPygame_Utils.py
File metadata and controls
101 lines (77 loc) · 2.77 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pygame
class Button():
def __init__(self,x,y,image,scale = 1):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image,(int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.selectable = True
self.clicked = False
self.moving = False
def drawButton(self,screen):
if not self.selectable:
return False
action = False
# get pos of mouse
pos = pygame.mouse.get_pos()
# is hovering over button
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
self.clicked = True
self.moving = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
self.moving = False
screen.blit(self.image,self.rect)
return action
def move(self,screen,x,y):
self.rect.topleft = (x,y)
screen.blit(self.image,self.rect)
def destroy(self):
self.image.fill((0,0,0,0))
self.selectable = False
def changeImage(self,image):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image,(int(width), int(height)))
class OverlaySquare(Button):
def __init__(self,coordinates, x, y, colour):
self.coordinates = coordinates
self.colour = colour
self.selectable = True
self.rect = pygame.Rect(x,y,100,100)
self.rect.topleft = (x,y)
self.clicked = False
self.moving = False
self.x = x
self.y = y
def drawButton(self, screen):
if not self.selectable:
return
action = False
# get pos of mouse
pos = pygame.mouse.get_pos()
# is hovering over button
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
self.clicked = True
self.moving = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
self.moving = False
pygame.draw.rect(screen,(self.colour),self.rect)
return action
def destroy(self):
self.rect.scale_by(0,0)
self.selectable = False
class chessPiece(Button):
def __init__(self,colour,coordinates,x,y,image,scale = 1):
self.colour = colour
self.coordinates = coordinates
Button.__init__(self,x,y,image,scale)
def move(self, screen, x, y,newcoords):
self.coordinates = newcoords
return super().move(screen, x, y)