-
Notifications
You must be signed in to change notification settings - Fork 1
/
pygameWidgets.py
194 lines (176 loc) · 7.99 KB
/
pygameWidgets.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'''
pygameWidgets description
Button Class: class for creating an interactive button object that performs functions when clicked
__init__: takes the host screen, x and y positions, width and height, the text on button, function to be called when clicked, and if one press only
process: renders the button on screen, detects mouse clicks, and calls functions if needed
Card Class: class for creating an interactive card object that can be clicked and unclicked
__init__: takes the host screen, the card type, x and y positions, width and height, function to be called when clicked, and called when clicked again
process: renders the card on screen, detects mouse clicks, and calls functions if needed
Player Class: class for creating a player avatar object that can be used to distinguish professor and student
__init__: takes the host screen, the player object, x and y positions, width and height, and whether identity is assigned
process: renders the player avatar on screen based on the player object
Text Class: class for adding a label object to show some game info
__init__: takes the host screen, the text, x and y positions, width and height
process: renders the text on screen
Img Class: class for showing an image on the screen
__init__: takes the host screen, the player object, x and y positions, width and height
process: renders the image on screen based on player identity
'''
import pygame
# Button class
class Button():
def __init__(self, screen, x, y, width, height, buttonText='Button', onclickFunction=lambda: print("Not assigned function"), onePress=False):
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
self.onclickFunction = onclickFunction
self.onePress = onePress
self.alreadyPressed = False
font = pygame.font.SysFont('Arial', 16)
self.fillColors = {
'normal': '#ffffff',
'hover': '#666666',
'pressed': '#333333',
}
self.buttonSurface = pygame.Surface((self.width, self.height))
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
self.buttonSurf = font.render(buttonText, True, (20, 20, 20))
def process(self):
mousePos = pygame.mouse.get_pos()
self.buttonSurface.fill(self.fillColors['normal'])
if self.buttonRect.collidepoint(mousePos):
self.buttonSurface.fill(self.fillColors['hover'])
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.buttonSurface.fill(self.fillColors['pressed'])
if self.onePress:
self.onclickFunction()
elif not self.alreadyPressed:
self.onclickFunction()
self.alreadyPressed = True
else:
self.alreadyPressed = False
self.buttonSurface.blit(self.buttonSurf, [
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
])
self.screen.blit(self.buttonSurface, self.buttonRect)
pygame.draw.rect(self.screen, (0, 0, 0), self.buttonRect, 2)
# Card class
class Card:
def __init__(self, screen, cardType, x, y, width, height, onclickFunction=lambda: print("Not assigned function"), cancelFuction=lambda: print("Not assigned function")):
self.screen = screen
self.cardType = cardType
self.x = x
self.y = y
self.width = width
self.height = height
self.onclickFunction = onclickFunction
self.cancelFunction = cancelFuction
self.pressedCnt = 0
self.newY = {
'normal': self.y,
'hover': self.y-3,
'pressed': self.y-5,
}
self.cardImg = pygame.image.load(f"./imgs/pokers/{self.cardType}.png")
self.cardImg = pygame.transform.scale(
self.cardImg, (self.width, self.height))
self.cardImg.convert()
self.cardSurface = pygame.Surface((self.width, self.height))
self.cardRect = pygame.Rect(self.x, self.y, self.width, self.height)
def process(self):
condition = 'normal'
mousePos = pygame.mouse.get_pos()
if self.cardRect.collidepoint(mousePos):
condition = 'hover'
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.pressedCnt = (self.pressedCnt+1) % 2
if self.pressedCnt == 1:
self.onclickFunction()
else:
self.cancelFunction()
if self.pressedCnt == 1:
condition = 'pressed'
else:
condition = 'normal'
self.cardRect = pygame.Rect(
self.x, self.newY[condition], self.width, self.height)
self.cardSurface.blit(self.cardImg, [
self.cardRect.width/2 - self.cardSurface.get_rect().width/2,
self.cardRect.height/2 - self.cardSurface.get_rect().height/2
])
self.screen.blit(self.cardSurface, self.cardRect)
pygame.draw.rect(self.screen, (0, 0, 0), self.cardRect, 2)
# Player class
class Player:
def __init__(self, screen, playerObj, x, y, width, height, assignedIdentity=False):
self.player = playerObj
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
font = pygame.font.SysFont('Arial', 18)
self.playerSurface = pygame.Surface((self.width, self.height))
self.playerSurface.fill((225, 225, 225))
self.playerRect = pygame.Rect(self.x, self.y, self.width, self.height)
if assignedIdentity:
if self.player.identity == 's':
self.playerSurf = font.render(
self.player.name, True, (81, 4, 0))
else:
self.playerSurf = font.render(
self.player.name, True, (2, 7, 93))
else:
self.playerSurf = font.render(self.player.name, True, (80, 80, 80))
def process(self):
self.playerSurface.blit(self.playerSurf, [
self.playerRect.width/2 - self.playerSurf.get_rect().width/2,
self.playerRect.height/2 - self.playerSurf.get_rect().height/2
])
self.screen.blit(self.playerSurface, self.playerRect)
# Text class
class Text:
def __init__(self, screen, text, x, y, width, height):
self.text = text
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
font = pygame.font.SysFont('Arial', 18)
self.textSurface = pygame.Surface((self.width, self.height))
self.textRect = pygame.Rect(self.x, self.y, self.width, self.height)
self.textSurf = font.render(self.text, True, (255, 255, 255))
def process(self):
self.textSurface.blit(self.textSurf, [
self.textRect.width/2 - self.textSurf.get_rect().width/2,
self.textRect.height/2 - self.textSurf.get_rect().height/2
])
self.screen.blit(self.textSurface, self.textRect)
# Img class
class Img:
def __init__(self, screen, player, x, y, width, height):
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
self.identity = player.identity
if self.identity == 'p':
self.image = pygame.image.load(f"./imgs/professors/saquib.jpg")
else:
self.image = pygame.image.load(f"./imgs/students/tartan.png")
self.image = pygame.transform.scale(
self.image, (self.width, self.height))
self.image.convert()
self.imgSurface = pygame.Surface((self.width, self.height))
self.imgRect = pygame.Rect(self.x, self.y, self.width, self.height)
def process(self):
self.imgSurface.blit(self.image, [
self.imgRect.width/2 - self.imgSurface.get_rect().width/2,
self.imgRect.height/2 - self.imgSurface.get_rect().height/2
])
self.screen.blit(self.imgSurface, self.imgRect)