forked from chacachiene/ChineseChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
114 lines (106 loc) · 3.81 KB
/
button.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
import pygame as p
import setting as s
import loadimg as l
# this class define the normal button
class Button:
isStartGame = False
def __init__(self, x, y, width, height,type,img, onClickFunction =None):
self.x = x
self.y = y
self.width = width
self.height = height
self.onClickFunction = onClickFunction
self.img = {
'normal': img[0],
'hover': img[1],
'pressed': img[2],
'active': img[3]
}
self.state = 'normal'
self.isPress = False
self.active = False
self.type = type
def process(self, screen, gs):
pos = p.mouse.get_pos()
if self.type =='re':
if gs.moveLog ==[]:
self.state = 'normal'
else:
self.state = 'active'
elif self.type =='ne':
if gs.store == []:
self.state = 'normal'
else:
self.state = 'active'
elif self.type =='ex':
if gs.moveLog ==[]:
self.state = 'normal'
else:
self.state = 'active'
elif self.type == 'st':
if gs.moveLog ==[]:
self.state = 'normal'
else:
self.state = 'active'
elif self.type == 'pa':
if gs.moveLog !=[]:
self.state = 'normal'
else:
self.state = 'active'
if self.state =='active' or self.type =='st' or self.type =='pa' or self.type =='ex':
if self.x <= pos[0] <= self.x + self.width and self.y <= pos[1] <= self.y + self.height:
self.state = 'hover'
if p.mouse.get_pressed()[0] and not self.isPress:
self.state = 'pressed'
self.isPress =True
self.onClickFunction()
if self.type == 'st':
self.active = True
Button.isStartGame = True
else:
self.isPress = False
if self.type == 'st' and self.active:
return
if self.type == 'pa' and self.state == 'active':
return
if self.type == 'ex' and self.state == 'active':
return
if self.type == 'st' and self.state == 'active':
return
if self.state != None:
screen.blit(self.img[self.state], (self.x, self.y))
#this class define a special button like the start game button
class SButton(Button):
def __init__(self, x, y, width, height,type,img, onClickFunction =None):
super().__init__( x, y, width, height,type,img, onClickFunction)
def process(self, screen, gs):
pos = p.mouse.get_pos()
if self.type =='ex':
if gs.moveLog ==[]:
self.state = 'normal'
else:
self.state = 'active'
elif self.type == 'st':
if gs.moveLog ==[]:
self.state = 'normal'
else:
self.state = 'active'
elif self.type == 'pa':
if gs.moveLog !=[]:
self.state = 'normal'
else:
self.state = 'active'
if self.state == 'active': return
if self.x <= pos[0] <= self.x + self.width and self.y <= pos[1] <= self.y + self.height:
self.state = 'hover'
if p.mouse.get_pressed()[0] and not self.isPress:
self.state = 'pressed'
self.isPress =True
self.onClickFunction()
if self.type == 'st':
self.active = True
Button.isStartGame = True
else:
self.isPress = False
if self.state != None:
screen.blit(self.img[self.state], (self.x, self.y))