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
136 changes: 59 additions & 77 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,25 +182,11 @@ 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)
]
ground = pygame.transform.scale(ground, (int(screen_size[0]), int(screen_size[1] // 3)))

grass = pygame.image.load('./assets/images/grass.png')
grass = pygame.transform.scale(grass, (int(screen_size[0]), int(screen_size[1] / 12)))
grass = pygame.transform.scale(grass, (int(screen_size[0]), int(screen_size[1] // 12)))
ground.blit(grass, (0,0))

points_label = self.lg_font.render('POINTS', 1, Color.BLACK)
Expand All @@ -208,20 +195,34 @@ def run(self):
sun.rect.topleft = (screen_size[0] - sun.image.get_width(), 0)

horse = Horse()
horse.rect.x = display_info.current_h / 3
horse.rect.x = display_info.current_h // 3
horse.rect.y = display_info.current_h - \
horse.image.get_height() - ground.get_height()

hurdle = pygame.image.load('./assets/images/hurdle.png')
hurdle = pygame.transform.scale(
hurdle,
(int(hurdle.get_height() / 3), int(hurdle.get_width() / 3)))
(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

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)))

question_board = pygame.Surface(
(screen_size[0] / 3, screen_size[1] / 5))
(screen_size[0] // 3, screen_size[1] // 5))
question_board = question_board.convert()
question_board.fill(Color.WHITE)

Expand Down Expand Up @@ -259,21 +260,18 @@ 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)
self.hurdle_number += 1
# start at vx=5 and accelerate towards vx=10
self.vx = 4 + (self.hurdle_number * 6) / (self.hurdle_number + 5)
self.vx = 4 + (self.hurdle_number * 6) // (self.hurdle_number + 5)
self.score_label = self.lg_font.render(
str(self.points), 1, Color.BLACK)
self.question_label = self.font.render(
Expand All @@ -292,7 +290,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 +311,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 +330,14 @@ 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)
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 @@ -365,9 +368,9 @@ def evaluate_answer(answer):

self.horse_change += 1

horse.rect.x = display_info.current_w / 3
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 All @@ -379,7 +382,7 @@ def evaluate_answer(answer):
# if not gameover, jump the hurdle
if not self.gameover:
horse.set_horse(Horse.JUMP)
horse.rect.x = display_info.current_w / 3
horse.rect.x = display_info.current_w // 3
horse.rect.y = display_info.current_h \
- horse.image.get_height() \
- ground.get_height() \
Expand All @@ -403,57 +406,36 @@ def evaluate_answer(answer):

screen.blit(
question_board,
(screen_size[0] / 4,
screen_size[1] / 5))
(screen_size[0] // 4,
screen_size[1] // 5))
question_board.blit(self.question_label, (10, 10))
question_board.blit(
self.question_text_label,
(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,
(
sun.rect.x + sun.image.get_width() / 4,
sun.rect.y + sun.image.get_height() / 3
sun.rect.x + sun.image.get_width() // 4,
sun.rect.y + sun.image.get_height() // 3
)
)

screen.blit(
points_label,
(
sun.rect.x + sun.image.get_width() / 4,
sun.rect.y + sun.image.get_height() / 3
sun.rect.x + sun.image.get_width() // 4,
sun.rect.y + sun.image.get_height() // 3
+ self.score_label.get_height()
)
)
Expand All @@ -464,8 +446,8 @@ def evaluate_answer(answer):
screen.blit(
gameover_label,
(
(screen_size[0] - gameover_label.get_width()) / 2,
(screen_size[1] - gameover_label.get_height()) / 2,
(screen_size[0] - gameover_label.get_width()) // 2,
(screen_size[1] - gameover_label.get_height()) // 2,
)
)

Expand Down Expand Up @@ -522,26 +504,26 @@ def start_game():
)

# draw menu horse
horse.rect.x = (screen_size[0] - horse.image.get_width()) / 2
horse.rect.x = (screen_size[0] - horse.image.get_width()) // 2
horse.rect.y = (
screen_size[1] - horse.image.get_height()) / 2 + 200
screen_size[1] - horse.image.get_height()) // 2 + 200

menu_sprites = pygame.sprite.RenderPlain(horse)
menu_sprites.draw(screen)

# draw play button
play_button.rect.x = (
screen_size[0] - play_button.rect.width) / 2
screen_size[0] - play_button.rect.width) // 2
play_button.rect.y = (
screen_size[1] - play_button.rect.height) / 2
screen_size[1] - play_button.rect.height) // 2
play_button.draw(screen)

# draw menu label
screen.blit(
menu_label,
(
(screen_size[0] - menu_label.get_width()) / 2,
(screen_size[1] - menu_label.get_height()) / 2 - 200,
(screen_size[0] - menu_label.get_width()) // 2,
(screen_size[1] - menu_label.get_height()) // 2 - 200,
)
)

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
66 changes: 66 additions & 0 deletions option_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright (C) 2023 Riya Jain
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


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, color):
self.color = color

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'
Loading