Skip to content

Commit

Permalink
Tracks images added
Browse files Browse the repository at this point in the history
  • Loading branch information
mareksmorag committed Jun 10, 2020
1 parent fd723de commit f99928b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 38 deletions.
Binary file modified resources/graphics/track1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/graphics/track2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/graphics/track3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def main_no_ui() -> None:
while True:
sleep(sleep_time)
car.tick(track, sleep_time)
car.go_brrrr()


def main() -> None:
Expand Down Expand Up @@ -85,7 +86,12 @@ def main() -> None:
"Track2": Action(ActionType.CHANGE_VIEW, 3),
"Track3": Action(ActionType.CHANGE_VIEW, 4),
}
options_menu = OptionsMenu(tracks=track_options)
track_images = [
pygame.image.load("resources/graphics/track1.jpg"),
pygame.image.load("resources/graphics/track2.jpg"),
pygame.image.load("resources/graphics/track3.jpg")
]
options_menu = OptionsMenu(tracks=track_options, tracks_thumbnails=track_images)
options_menu.background_image = pygame.image.load(
"resources/graphics/menu-background.png"
)
Expand Down
1 change: 1 addition & 0 deletions src/view/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
ORANGE = (255, 176, 21)
BIZARRE_MASKING_PURPLE = (240, 0, 240)
LIME = (50, 205, 50)
26 changes: 22 additions & 4 deletions src/view/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@


class Menu(View):
FONT_COLOR = colors.ORANGE
SELECTED_FONT_COLOR = colors.WHITE
BUTTON_COLOR = colors.WHITE
SELECTED_BUTTON_COLOR = colors.ORANGE

def __init__(self, menu_options: Dict[str, Action]):
super().__init__()
pygame.font.init()
Expand Down Expand Up @@ -47,12 +52,25 @@ def draw(
ofset_y = int(pos * 1.5 * self.button_dims[1])
shifted_button_rect = self._button_rect.move(0, ofset_y)
color = (
self.button_highlight
self.SELECTED_BUTTON_COLOR
if pos == self.selected_item
else self.button_color
else self.BUTTON_COLOR
)

if pos == self.selected_item:
shifted_button_rect = shifted_button_rect.inflate(20, 20)
else:
pygame.draw.rect(destination, colors.ORANGE, shifted_button_rect, 4)

pygame.draw.rect(destination, color, shifted_button_rect)
label = self.font.render(button, True, self.font_color)

font_color = (
self.SELECTED_FONT_COLOR
if pos == self.selected_item
else self.FONT_COLOR
)

label = self.font.render(button, True, font_color)
destination.blit(
label,
(
Expand Down Expand Up @@ -89,4 +107,4 @@ def _process_events(self, events: List[EventType]) -> bool:
self.selected_item = (self.selected_item + 1) % len(self._options)
elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
return True
return False
return False
61 changes: 28 additions & 33 deletions src/view/options_menu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Dict, Tuple
from typing import List, Optional, Dict, Tuple, Any
from collections import OrderedDict

import pygame
Expand All @@ -13,22 +13,22 @@


class OptionsMenu(View):
def __init__(self, tracks: Dict[str, Action]) -> None:
ARROW_COLOR = colors.ORANGE
FONT_COLOR = colors.WHITE
LOGO_IMAGE = pygame.image.load("resources/graphics/logo.png")
DIVIDER = 0.4

def __init__(self, tracks: Dict[str, Action], tracks_thumbnails: List[Any]) -> None:
super().__init__()
pygame.font.init()
self.font = pygame.font.SysFont("Verdana", 30)
self.main_font = pygame.font.SysFont("comicsansms", 60)
self.selected_item = 0
self._options = OrderedDict(tracks)
self.font_color = colors.WHITE
self.button_highlight = colors.GREEN
self.button_color = colors.LIGHTGRAY
self._background: Optional[Tuple[Surface, Tuple[int, int]]] = None
self.background_image: Optional[Surface] = None
self.divider = 0.4
self.selected_action: Optional[Action] = None
self.img = pygame.image.load("resources/graphics/track1.jpg")
self.logo_image = pygame.image.load("resources/graphics/logo.png")
self.thumbnails = tracks_thumbnails

def draw(self, destination: Surface, events: List[EventType], delta_time: float) -> Optional[Action]:
size = destination.get_size()
Expand All @@ -43,15 +43,16 @@ def draw(self, destination: Surface, events: List[EventType], delta_time: float)
destination.blit(*self._logo)

mini_track = self._button_rect
thumbnail_image = pygame.transform.scale(self.img, (mini_track.w, mini_track.h))
thumbnail_image = pygame.transform.scale(self.thumbnails[self.selected_item], (mini_track.w, mini_track.h))
thumbnail = (thumbnail_image, mini_track)
destination.blit(*thumbnail)
pygame.draw.rect(destination, colors.WHITE, mini_track, 4)

main_label = self.main_font.render("Select track", True, self.font_color)
main_label = self.main_font.render("Select track", True, self.FONT_COLOR)
destination.blit(main_label, (size[0] // 2 - 185, size[1] // 10))

track_label = self.font.render(
list(self._options.keys())[self.selected_item], True, self.font_color
list(self._options.keys())[self.selected_item], True, self.FONT_COLOR
)
destination.blit(
track_label,
Expand All @@ -61,42 +62,36 @@ def draw(self, destination: Surface, events: List[EventType], delta_time: float)
),
)

info_label = self.font.render("PRESS ENTER TO START", True, self.font_color)
destination.blit(info_label, (size[0] - 380, size[1] - 40))

left_arrow_points = (
(self.ofset_x - size[0] // 20, self.ofset_y + mini_track.h // 2),
(self.ofset_x - 10, self.ofset_y + mini_track.h // 2 - size[0] // 40),
(self.ofset_x - 10, self.ofset_y + mini_track.h // 2 + size[0] // 40),
(self.offset_x - size[0] // 20, self.offset_y + mini_track.h // 2),
(self.offset_x - 10, self.offset_y + mini_track.h // 2 - size[0] // 40),
(self.offset_x - 10, self.offset_y + mini_track.h // 2 + size[0] // 40),
)
right_arrow_points = (
(3 * self.ofset_x + size[0] // 20, self.ofset_y + mini_track.h // 2),
(3 * self.ofset_x + 10, self.ofset_y + mini_track.h // 2 - size[0] // 40),
(3 * self.ofset_x + 10, self.ofset_y + mini_track.h // 2 + size[0] // 40),
(3 * self.offset_x + size[0] // 20, self.offset_y + mini_track.h // 2),
(3 * self.offset_x + 10, self.offset_y + mini_track.h // 2 - size[0] // 40),
(3 * self.offset_x + 10, self.offset_y + mini_track.h // 2 + size[0] // 40),
)

if self.selected_item != 0:
pygame.draw.polygon(destination, self.font_color, left_arrow_points)
pygame.draw.polygon(destination, self.ARROW_COLOR, left_arrow_points)
if self.selected_item != len(self._options) - 1:
pygame.draw.polygon(destination, self.font_color, right_arrow_points)
pygame.draw.polygon(destination, self.ARROW_COLOR, right_arrow_points)
return None

def _update_geometry(self, size: Tuple[int, int]) -> None:
self.ofset_y = int(self.divider * size[1])
self.ofset_x = size[0] // 4
self.offset_y = int(self.DIVIDER * size[1])
self.offset_x = size[0] // 4

self.button_dims = size[0] // 2, size[1] // 2

background_shape = Rect((0, 0), size)
background_image = pygame.transform.scale(self.background_image, size)
self._background = (background_image, background_shape)
self._button_rect = Rect((self.ofset_x, self.ofset_y), self.button_dims)

if not self.logo_image:
return
self._button_rect = Rect((self.offset_x, self.offset_y), self.button_dims)

logo_image = pygame.transform.scale(
self.logo_image, (size[0] // 6, size[1] // 12)
self.LOGO_IMAGE, (size[1] // 3, size[1] // 12)
)
logo_shape = Rect((10, 10), (size[0] // 8, size[1] // 8))
self._logo = (logo_image, logo_shape)
Expand All @@ -107,15 +102,15 @@ def _process_events(self, events: List[EventType]) -> bool:
if event.key == pygame.K_LEFT and self.selected_item != 0:
self.selected_item = self.selected_item - 1
elif (
event.key == pygame.K_RIGHT
and self.selected_item != len(self._options) - 1
event.key == pygame.K_RIGHT
and self.selected_item != len(self._options) - 1
):
self.selected_item = self.selected_item + 1
elif event.key == pygame.K_a and self.selected_item != 0:
self.selected_item = self.selected_item - 1
elif (
event.key == pygame.K_d
and self.selected_item != len(self._options) - 1
event.key == pygame.K_d
and self.selected_item != len(self._options) - 1
):
self.selected_item = self.selected_item + 1
elif event.key == pygame.K_RETURN:
Expand Down

0 comments on commit f99928b

Please sign in to comment.