-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStevenBattleBoxButtons.py
209 lines (172 loc) · 8.79 KB
/
StevenBattleBoxButtons.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pygame
from settings import *
# Initialize game
pygame.init()
# Create Game Window (pixels x-axis, pixels y-axis)
screen = pygame.display.set_mode((800, 600))
#Text Stuff
oswfont32 = pygame.font.Font("Fonts/Oswald-VariableFont_wght.ttf", 32)
class Button:
"""Creation and methods for handling buttons"""
def __init__(self, color, x, y, width, height, text='', font = oswfont32, text_color = BLACK):
"""
Initializes a button of 'color' color, with the upper left corner at x,y
width and height determine size of button
text is what should be said inside the button
"""
self.color = color
self.original_color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.rectangle = pygame.Rect(x, y, width, height)
self.text = text
self.font = font
self.text_color = text_color
# def set_text_color(self, color):
# """Changes the color of the text. RGB"""
# self.text_color = color
def draw_button(self, surface):
"""Call this method to draw the button onto the given surface"""
pygame.draw.rect(surface, self.color, self.rectangle)
if self.text != '':
text = self.font.render(self.text, True, self.text_color)
surface.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def mouse_hover_color(self, hover_color):
"""Changes the color of the button when the mouse is hovering over it"""
mouse_position = pygame.mouse.get_pos()
if mouse_position[0] > self.x and mouse_position[0] < (self.x + self.width) and mouse_position[1] > self.y and mouse_position[1] < (self.y + self.height):
self.color = hover_color
else:
self.color = self.original_color
class MultipleChoiceBox:
"""Creates a MultipleChoiceBox object (add better docstring)"""
def __init__(self, box_color, border_color, x, y, width, height, surface, text_font):
"""
Initializes a MultipleChoiceBox
upper left corner at coordinates x, y
width = # of pixels wide
height = # of pixels tall
box_color = color of the box
border_color = color of the border
surface = on which surface the MultipleChoiceBox should appear
"""
self.x = x
self.y = y
self.width = width
self.height = height
self.box_color = box_color
self.border_color = border_color
self.surface = surface
self.font = text_font
self.rectangle = pygame.Rect(x, y, width, height)
self.rectangle_outline = pygame.Rect(x-2, y-2, width+4, height+4)
self.response_A = ''
self.response_B = ''
self.response_C = ''
self.response_D = ''
self.response_E = ''
self.button_A = Button(BLACK, self.x + 10, self.y + 120, self.width -20, height = 50, text = self.response_A, text_color = WHITE)
self.button_B = Button(BLACK, self.x + 10, self.y + 180, self.width -20, height = 50, text = self.response_B, text_color = WHITE)
self.button_C = Button(BLACK, self.x + 10, self.y + 240, self.width -20, height = 50, text = self.response_C, text_color = WHITE)
self.button_D = Button(BLACK, self.x + 10, self.y + 300, self.width -20, height = 50, text = self.response_D, text_color = WHITE)
self.button_E = Button(BLACK, self.x + 10, self.y + 360, self.width -20, height = 50, text = self.response_D, text_color = WHITE)
self.buttons_list = [self.button_A, self.button_B, self.button_C, self.button_D, self.button_E]
def draw_background(self):
"""Draws the background text box on top of given surface"""
#pygame.draw.rect(<name of surface>, <color of fill>, <dimensions (x,y,width,height)>, <optional: rounded corner radius>)
pygame.draw.rect(self.surface, self.border_color, self.rectangle_outline)
pygame.draw.rect(self.surface, self.box_color, self.rectangle)
def draw_text(self, surface, text, color, rectangle, font, aa = True, bkg = None): #https://www.pygame.org/wiki/TextWrap
"""
Will draw text and wrap it to following line if text is larger than container
surface = surface for text to be drawn on
text = a string of text to be displayed
color = color of text
rectangle = size of textbox as (x, y, width, height) with x, y marking upper left corner
font = font style
aa = antialiasing, determines if text is smoothed or not. Used for the font.render method
bkg = background
"""
rectangle = pygame.Rect(rectangle)
y = rectangle.top
line_spacing = -2
#get the height of the font. 'font.size(<text>)' returns the height and width of the text as a tuple ('T' and 'g' are tall and deep letters)
font_height = font.size("Tg")[1]
while text:
i = 1
#determine if the row of text will be outside the area
if y + font_height > rectangle.bottom:
break
#determine maximum width of line
while font.size(text[:i])[0] < rectangle.width and i < len(text):
i += 1
#if text is wrapped, adjust the wrap to the last word
if i < len(text):
i = text.rfind(" ", 0, i) + 1 #rfind is a string method. It finds the index of the last instance of a value string.rfind(<value>, <startindex>, <endindex>)
#render the line and blit it to the surface
text_render = font.render(text[:i], aa, color)
surface.blit(text_render, (rectangle.left, y))
y += font_height + line_spacing
#remove the text we just blitted
text = text[i:]
return text
def set_read_text(self, text):
"""Sets the text which will be displayed at the top of the box"""
self.read_text = text
def set_response_A(self, text):
"""Sets the text which will appear in button A"""
self.response_A = f'A) {text}'
self.button_A = Button(BLACK, self.x + 10, self.y + 120, self.width -20, 50, text = self.response_A, text_color = WHITE)
def set_response_B(self, text):
"""Sets the text which will appear in button B"""
self.response_B = f'B) {text}'
self.button_B = Button(BLACK, self.x + 10, self.y + 180, self.width -20, 50, text = self.response_B, text_color = WHITE)
def set_response_C(self, text):
"""Sets the text which will appear in button C"""
self.response_C = f'C) {text}'
self.button_C = Button(BLACK, self.x + 10, self.y + 240, self.width -20, 50, text = self.response_C, text_color = WHITE)
def set_response_D(self, text):
"""Sets the text which will appear in button D"""
self.response_D = f'D) {text}'
self.button_D = Button(BLACK, self.x + 10, self.y + 300, self.width -20, 50, text = self.response_D, text_color = WHITE)
def set_response_E(self, text):
"""Sets the text which will apper in button E"""
self.response_E = text
self.button_E = Button(BLACK, self.x + 10, self.y + 360, self.width -20, 50, text = self.response_E, text_color = WHITE)
def draw_read_text(self):
"""Draws the question text at the top of the MultipleChoiceBox"""
text_boundary = pygame.Rect(self.x + 10, self.y + 10, self.width - 20, self.height)
self.draw_text(self.surface, self.read_text, BLACK, text_boundary, self.font)
def display_MultipleChoiceBox(self):
"""A function to display the MultipleChoiceBox and all of its contents in the game loop"""
self.draw_background()
self.draw_read_text()
self.button_A.draw_button(screen)
self.button_B.draw_button(screen)
self.button_C.draw_button(screen)
self.button_D.draw_button(screen)
self.button_E.draw_button(screen)
question = MultipleChoiceBox(WHITE, BLACK, 100, 70, 400, 420, screen, oswfont32) #coordinates to center MultipleChoiceBox on room
question.set_read_text("What is the atomic number for Helium?")
question.set_response_A("2")
question.set_response_B("4")
question.set_response_C("3")
question.set_response_D("10")
question.set_response_E("Use a powerup")
# Game Loop
running = True
while running:
screen.fill(WHITE)
question.display_MultipleChoiceBox()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
question.button_A.mouse_hover_color(LIGHTGREY)
question.button_B.mouse_hover_color(LIGHTGREY)
question.button_C.mouse_hover_color(LIGHTGREY)
question.button_D.mouse_hover_color(LIGHTGREY)
question.button_E.mouse_hover_color(LIGHTGREY)
pygame.display.update()