Skip to content

Commit

Permalink
Allow selecting track in the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayman committed Oct 10, 2024
1 parent e92960e commit bf4a720
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
7 changes: 7 additions & 0 deletions racer/tracks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import track1
from . import track2

all_tracks = [
track1,
track2,
]
2 changes: 1 addition & 1 deletion racer/tracks/track1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pygame

name = 'track1.svg'
name = "Imposter's circuit"
background = pygame.image.load(os.path.splitext(__file__)[0] + '.jpg')
scale = 2
track_width = 15
Expand Down
2 changes: 1 addition & 1 deletion racer/tracks/track2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pygame

name = 'track2.svg'
name = 'Evergreen Ridge'
background = pygame.image.load(os.path.splitext(__file__)[0] + '.webp')
scale = 1
track_width = 30
Expand Down
34 changes: 30 additions & 4 deletions racer/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@


import asyncio
from math import degrees
import time
from math import degrees

import pygame
import pygame_widgets
from pygame import Color
from pygame.math import Vector2
from pygame_widgets.button import Button
from pygame_widgets.dropdown import Dropdown

from .constants import framerate
from .game_state import GameState
from .linear_math import Transform
from .track import Track
from .tracks import track1
from .tracks import all_tracks

starting_track = all_tracks[0]


class Window:
Expand Down Expand Up @@ -60,6 +63,13 @@ def __init__(self, game_state, app):
onClick=lambda: self.app.toggle_present()
)

map_names = [track.name for track in all_tracks]
self.map_dropdown = Dropdown(
self.window, 510, 20, 120, 30, name='Switch track',
choices=map_names, fontSize=20,
inactiveColour=(255, 0, 0), hoverColour=(255, 0, 0), pressedColour=(255, 0, 0),
)

def draw(self, clock):
# scale map to full screen
zoom = min(self.window.get_width() / self.map.get_width(), self.window.get_height() / self.map.get_height())
Expand Down Expand Up @@ -125,6 +135,16 @@ def draw(self, clock):
text = self.font.render(f'{behind:+.2f}', True, Color('white'))
self.window.blit(text, (self.window.get_width() - 50, offset_from_top + i * 20))

def selected_track(self):
name = self.map_dropdown.getSelected()
if name is None:
return None
return next(track for track in all_tracks if track.name == name)

def reload_track(self):
self.map = self.game_state.track.background.convert()
self.map_dropdown.reset()


class App:
def __init__(self):
Expand All @@ -133,7 +153,7 @@ def __init__(self):
self.fast = False
self.present = False

self.game_state = GameState(Track(track1))
self.game_state = GameState(Track(starting_track))
self.window = Window(game_state=self.game_state, app=self)
self.clock = pygame.time.Clock()

Expand Down Expand Up @@ -169,13 +189,19 @@ async def mainloop(self):
self.do_step()
elif event.key == pygame.K_f:
self.toggle_fast()

if not next_reset and self.present:
next_reset = time.time() + (60 * 2)
elif self.present and time.time() > next_reset:
self.game_state.reset()
next_reset = None

if self.window.selected_track() != None and self.window.selected_track() != self.game_state.track:
print(f'Switching to track {self.window.selected_track().name}')
self.game_state = GameState(Track(self.window.selected_track()))
self.window.game_state = self.game_state
self.window.reload_track()

if not self.paused or self.step:
# Update the game
self.game_state.update(1 / framerate)
Expand Down

0 comments on commit bf4a720

Please sign in to comment.