Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced the UI and added Keyboard Support for options #14

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
92 changes: 37 additions & 55 deletions math_hurdler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fractions import Fraction
from question import Question
from objects.button import Button
from option_button import Option_Button
from sprites.horse import Horse
from sprites.sun import Sun

Expand Down Expand Up @@ -181,23 +182,9 @@ def run(self):

screen = pygame.display.get_surface()
screen_size = screen.get_size()
button_panel = pygame.Surface((screen_size[0] / 3, screen_size[1] / 7))
ground = pygame.image.load('./assets/images/ground.png')
ground = pygame.transform.scale(ground, (int(screen_size[0]), int(screen_size[1] / 3)))

self.buttons = [
Button(
str(self.question.choices[i]),
self.lg_font,
Color.BLACK,
button_panel.get_width() / 2,
button_panel.get_height() / 2,
Color.WHITE,
Color.BLACK,
-2
) for i in range(4)
]

grass = pygame.image.load('./assets/images/grass.png')
grass = pygame.transform.scale(grass, (int(screen_size[0]), int(screen_size[1] / 12)))
ground.blit(grass, (0,0))
Expand All @@ -218,7 +205,20 @@ def run(self):
(int(hurdle.get_height() / 3), int(hurdle.get_width() / 3)))

hurdle_y = display_info.current_h - \
hurdle.get_height() - (2 * ground.get_height() / 3)
hurdle.get_height() - (2 * ground.get_height() / 3) + 70
jriyyya marked this conversation as resolved.
Show resolved Hide resolved

self.buttons = []
button_width = 200
button_height = 80
buttons_x_padding = 50
buttons_y_padding = (ground.get_rect().height - grass.get_rect().height - button_height) / 4
buttons_gap = (screen_size[0] - (buttons_x_padding * 2 + button_width * 4)) / 3

for i, choice in enumerate(self.question.choices):
choice_text = self.lg_font.render(("A", "B", "C", "D")[i] + " " + str(choice), 1, Color.BLACK)
self.buttons.append(Option_Button(buttons_x_padding + button_width * i + buttons_gap * i,
screen_size[1] - button_height - buttons_y_padding,
button_width, button_height, choice_text, str(choice)))
jriyyya marked this conversation as resolved.
Show resolved Hide resolved

question_board = pygame.Surface(
(screen_size[0] / 3, screen_size[1] / 5))
Expand Down Expand Up @@ -259,15 +259,12 @@ def reset():
def generate_question():
next(self.question)

self.buttons[0].set_text(str(self.question.choices[0]))
self.buttons[1].set_text(str(self.question.choices[1]))
self.buttons[2].set_text(str(self.question.choices[2]))
self.buttons[3].set_text(str(self.question.choices[3]))

self.buttons[0].set_color(self.buttons[0].color, False)
self.buttons[1].set_color(self.buttons[0].color, False)
self.buttons[2].set_color(self.buttons[0].color, False)
self.buttons[3].set_color(self.buttons[0].color, False)
for i in range(4):
choice_text = self.lg_font.render(("A", "B", "C", "D")[i] + " " + str(self.question.choices[i]),
1, Color.BLACK)
self.buttons[i].set_text(choice_text)
self.buttons[i].value = str(self.question.choices[i])
self.buttons[i].set_color((255, 255, 255))

self.question_text_label = self.lg_font.render(
str(self.question), 1, Color.BLACK)
Expand All @@ -292,7 +289,7 @@ def set_answer(answer_index):
self.buttons[self.last_answer_index].set_selected(False)

if answer_index >= 0:
self.last_answer = Fraction(self.buttons[answer_index].text)
self.last_answer = Fraction(self.buttons[answer_index].value)
self.buttons[answer_index].set_selected(True)
self.last_answer_index = answer_index
else:
Expand All @@ -313,11 +310,10 @@ def evaluate_answer(answer):
self.set_gameover(True)
if self.last_answer_index >= 0:
self.buttons[self.last_answer_index].set_color(
Color.RED, False)
Color.RED)

self.buttons[self.question.answer_index].set_color(
Color.GREEN, False)

Color.GREEN)

while self.running:
# Processing Gtk Events
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this wasn't your change but could you please remove redundant comments like this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes yes, sure

Expand All @@ -333,8 +329,15 @@ def evaluate_answer(answer):
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
self.paused = not self.paused
elif event.key == pygame.K_c:
set_answer(self.question.answer_index)
# Select options by 1,2,3,4 or A,B,C,D keys
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
elif event.key == pygame.K_a or event.key == pygame.K_1:
set_answer(0)
elif event.key == pygame.K_b or event.key == pygame.K_2:
set_answer(1)
elif event.key == pygame.K_c or event.key == pygame.K_3:
set_answer(2)
elif event.key == pygame.K_d or event.key == pygame.K_4:
set_answer(3)
elif event.type == pygame.MOUSEBUTTONDOWN:
if not self.gameover:
for i in range(0, 4):
Expand Down Expand Up @@ -367,7 +370,7 @@ def evaluate_answer(answer):

horse.rect.x = display_info.current_w / 3
horse.rect.y = display_info.current_h - \
hurdle.get_height() - (3 * ground.get_height() / 4)
hurdle.get_height() - (3 * ground.get_height() / 4) + 70

# Check if hurdle and horse in same spot.
if horse.rect.colliderect(hurdle_rect):
Expand Down Expand Up @@ -411,36 +414,15 @@ def evaluate_answer(answer):
(10, self.question_label.get_height() + 10))

screen.blit(ground, (0, screen_size[1] - ground.get_height()))
button_panel_x = ground.get_width() / 4
button_panel_y = screen_size[1] - \
ground.get_height() + ground.get_height() / 3 + 10
screen.blit(button_panel, (button_panel_x, button_panel_y))

self.buttons[0].rect.x = button_panel_x
self.buttons[0].rect.y = button_panel_y
self.buttons[0].draw(screen)

self.buttons[1].rect.x = button_panel_x + \
self.buttons[0].image.get_width()
self.buttons[1].rect.y = button_panel_y
self.buttons[1].draw(screen)

self.buttons[2].rect.x = button_panel_x
self.buttons[2].rect.y = button_panel_y + \
self.buttons[0].image.get_height()
self.buttons[2].draw(screen)

self.buttons[3].rect.x = button_panel_x + \
self.buttons[2].image.get_width()
self.buttons[3].rect.y = button_panel_y + \
self.buttons[2].image.get_height()
self.buttons[3].draw(screen)

screen.blit(hurdle, (self.x, hurdle_y))

allsprites = pygame.sprite.RenderPlain(sun, horse)
allsprites.draw(screen)

for btn in self.buttons:
btn.draw()

screen.blit(
self.score_label,
(
Expand Down
1 change: 1 addition & 0 deletions math_hurdlerActivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, handle):
# Note that set_canvas implicitly calls read_file when
# resuming from the Journal.
self.set_canvas(self._pygamecanvas)
self._pygamecanvas.grab_focus()

# Start the game running (self.game.run is called when the
# activity constructor returns).
Expand Down
50 changes: 50 additions & 0 deletions option_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pygame
jriyyya marked this conversation as resolved.
Show resolved Hide resolved


class Option_Button:

def __init__(self, x, y, width, height, text, value):
self.text = text
self.text_rect = text.get_rect()
self.value = value

self.rect = pygame.Rect(x, y, width, height)

self.gameDisplay = pygame.display.get_surface()

self.color = (255, 255, 255)
self.selected_color = (180, 180, 180)

self.press = False
self.draw()

def set_text(self, text):
self.text = text
self.text_rect = text.get_rect()

def draw(self):
c_radius = self.rect.height // 2
c_center = [self.rect.x, self.rect.y + c_radius]
pygame.draw.circle(self.gameDisplay, self.color, c_center, c_radius)
c_center[0] += self.rect.width
pygame.draw.circle(self.gameDisplay, self.color, c_center, c_radius)
pygame.draw.rect(self.gameDisplay, self.color, self.rect)
m_x = self.rect.x + self.rect.width // 2 - self.text_rect.width // 2
m_y = self.rect.y + self.rect.height // 2 - self.text_rect.height // 2
self.gameDisplay.blit(self.text, (m_x, m_y))

def set_color(self, clr):
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
self.color = clr

def hovered(self):
return self.rect.collidepoint(pygame.mouse.get_pos())

def mouse_click(self, mouse, action, *args):
if self.rect.collidepoint(mouse):
action(*args)

def set_selected(self, selected):
if selected:
self.set_color(self.selected_color)
else:
self.set_color((255, 255, 255))
22 changes: 22 additions & 0 deletions sugargame/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
#
# Copyright (c) 2020 Wade Brainerd
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

__version__ = '1.3'
22 changes: 22 additions & 0 deletions sugargame/canvas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
#
# Copyright (c) 2020 Wade Brainerd
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

import os
from gi.repository import Gtk
from gi.repository import GLib
Expand Down
Loading